多年来比较产品利润的功率查询

发布于 2025-02-04 22:29:29 字数 810 浏览 1 评论 0原文

我有一个具有公司,产品,利润和年份的数据集。每年,公司都会出售几种产品并获得利润。该公司不必在明年出售相同的生产。它们可能被遗漏了以前的产品,并添加了新的几种产品。我只想将苹果与以下两年产品进行比较。

我的数据集是,

Company Product Profit  Year
X       Soap     20     2020
X       Shampoo  30     2020
X       Noodles  25     2021
X       Shampoo  40     2021
X       Coffee   60     2022
X       Shampoo  34     2022
Y       Coffee   20     2020
Y       Noodles  30     2020
Y       Shampoo  25     2021
Y       Switch   40     2021
Y       Soap     60     2022
Y       Shampoo  34     2022

我想与每个公司的上一年的产品相比,在下面的每个公司的产品中,

​t在2021年出售任何肥皂。我只想将其视为0或NULL,并想改变下面的桌子。在电源查询中有什么方法可以做到这一点?

I have a dataset which having Company, Product, Profit and Year. Each year the company will sell few products and obtain the profits. Its not necessary the company should sale the same production in the next year. They may be left out previous products and add new few productions. I just want to apple to apple comparison of two year products like below.

My dataset is,

Company Product Profit  Year
X       Soap     20     2020
X       Shampoo  30     2020
X       Noodles  25     2021
X       Shampoo  40     2021
X       Coffee   60     2022
X       Shampoo  34     2022
Y       Coffee   20     2020
Y       Noodles  30     2020
Y       Shampoo  25     2021
Y       Switch   40     2021
Y       Soap     60     2022
Y       Shampoo  34     2022

enter image description here

I wanted to transform the table compared with the previous year's product for each respective year of every company like below,

enter image description here

Look at the above, the company didn't sell any soap in the year 2021. I just want to consider it as 0 or null and wanted to transform the table like below. Is there any way to do this in the Power query?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

探春 2025-02-11 22:29:29

这是一种方法。

我今天晚上没有时间进行详细的解释,但是如果您阅读M代码和评论;并且还遵循应用步骤,您应该能够弄清楚它。

基础知识

  • 每年
  • 使用产品组合一对表,作为
  • 展开配对表
  • 填充在空白年中,并
  • 在每个表对后插入空白行

m代码

let

//Read in Data
//Change next two lines to reflect your actual data source
    Source = Excel.CurrentWorkbook(){[Name="Table13"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{
        {"Company", type text}, {"Product", type text}, {"Profit", Int64.Type}, {"Year", Int64.Type}}),

//create yearly table pairs
    years = List.Sort(List.Distinct(#"Changed Type"[Year])),
    tables = List.Generate(
        ()=>[t=Table.ExpandTableColumn(
                Table.NestedJoin(
                    Table.SelectRows(#"Changed Type", (ft)=>ft[Year]=years{0}), "Product",
                    Table.SelectRows(#"Changed Type", (ft)=>ft[Year]=years{1}), "Product",
                    "Joined", JoinKind.FullOuter),"Joined",
                     {"Company","Product","Profit","Year"},{"Company1","Product1","Profit1","Year1"}),               
                idx=0],

            each [idx] < List.Count(years)-1,
            each [t=Table.ExpandTableColumn(
                Table.NestedJoin(
                    Table.SelectRows(#"Changed Type", (ft)=>ft[Year]=years{[idx]+1}),"Product",
                    Table.SelectRows(#"Changed Type", (ft)=>ft[Year]=years{[idx]+2}), "Product",
                    "Joined", JoinKind.FullOuter),"Joined",
                    {"Company","Product","Profit","Year"},{"Company1","Product1","Profit1","Year1"}),               
                idx=[idx]+1],
            each Table.InsertRows(            
                        Table.FillDown(Table.FillUp([t],{"Year", "Year1"}),{"Year","Year1"}),
                    Table.RowCount([t]),
                    {[Company="", Product="", Profit=null, Year=null, Company1=null, Product1="", Profit1=null, Year1=null]})
    ),
    #"Converted to Table" = Table.FromList(tables, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
    #"Expanded Column1" = Table.ExpandTableColumn(#"Converted to Table", "Column1", 
        {"Company", "Product", "Profit", "Year", "Company1", "Product1", "Profit1", "Year1"}),

    //Fill in the blank company and products
    #"Fill in blanks" = Table.FromRecords(
        Table.TransformRows(#"Expanded Column1",
            (r)=> Record.TransformFields(r,{
                {"Company", each if _ = null then r[Company1] else _},
                {"Product", each if _ = null then r[Product1] else _},
                {"Product1", each if _ = null then r[Product] else _}

             } ))),
    #"Removed Columns" = Table.RemoveColumns(#"Fill in blanks",{"Company1"}),
    #"Removed Bottom Rows" = Table.RemoveLastN(#"Removed Columns",1),
    #"Changed Type1" = Table.TransformColumnTypes(#"Removed Bottom Rows",{{"Company", type text}, {"Product", type text}, {"Profit", Int64.Type}, {"Year", Int64.Type}, {"Product1", type text}, {"Profit1", Int64.Type}, {"Year1", Int64.Type}})
in
    #"Changed Type1"

”在此处输入图像说明”

Here's one method.

I don't have time this evening to do a detailed explanation, but if you read the M Code and comments; and also follow along the Applied Steps, you should be able to figure it out.

Basics

  • Create separate tables for each year
  • Join pairs of tables using Product as the Key
  • Expand the paired tables
  • Fill in the blank years and products
  • Insert a blank row after each table pair

M Code

let

//Read in Data
//Change next two lines to reflect your actual data source
    Source = Excel.CurrentWorkbook(){[Name="Table13"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{
        {"Company", type text}, {"Product", type text}, {"Profit", Int64.Type}, {"Year", Int64.Type}}),

//create yearly table pairs
    years = List.Sort(List.Distinct(#"Changed Type"[Year])),
    tables = List.Generate(
        ()=>[t=Table.ExpandTableColumn(
                Table.NestedJoin(
                    Table.SelectRows(#"Changed Type", (ft)=>ft[Year]=years{0}), "Product",
                    Table.SelectRows(#"Changed Type", (ft)=>ft[Year]=years{1}), "Product",
                    "Joined", JoinKind.FullOuter),"Joined",
                     {"Company","Product","Profit","Year"},{"Company1","Product1","Profit1","Year1"}),               
                idx=0],

            each [idx] < List.Count(years)-1,
            each [t=Table.ExpandTableColumn(
                Table.NestedJoin(
                    Table.SelectRows(#"Changed Type", (ft)=>ft[Year]=years{[idx]+1}),"Product",
                    Table.SelectRows(#"Changed Type", (ft)=>ft[Year]=years{[idx]+2}), "Product",
                    "Joined", JoinKind.FullOuter),"Joined",
                    {"Company","Product","Profit","Year"},{"Company1","Product1","Profit1","Year1"}),               
                idx=[idx]+1],
            each Table.InsertRows(            
                        Table.FillDown(Table.FillUp([t],{"Year", "Year1"}),{"Year","Year1"}),
                    Table.RowCount([t]),
                    {[Company="", Product="", Profit=null, Year=null, Company1=null, Product1="", Profit1=null, Year1=null]})
    ),
    #"Converted to Table" = Table.FromList(tables, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
    #"Expanded Column1" = Table.ExpandTableColumn(#"Converted to Table", "Column1", 
        {"Company", "Product", "Profit", "Year", "Company1", "Product1", "Profit1", "Year1"}),

    //Fill in the blank company and products
    #"Fill in blanks" = Table.FromRecords(
        Table.TransformRows(#"Expanded Column1",
            (r)=> Record.TransformFields(r,{
                {"Company", each if _ = null then r[Company1] else _},
                {"Product", each if _ = null then r[Product1] else _},
                {"Product1", each if _ = null then r[Product] else _}

             } ))),
    #"Removed Columns" = Table.RemoveColumns(#"Fill in blanks",{"Company1"}),
    #"Removed Bottom Rows" = Table.RemoveLastN(#"Removed Columns",1),
    #"Changed Type1" = Table.TransformColumnTypes(#"Removed Bottom Rows",{{"Company", type text}, {"Product", type text}, {"Profit", Int64.Type}, {"Year", Int64.Type}, {"Product1", type text}, {"Profit1", Int64.Type}, {"Year1", Int64.Type}})
in
    #"Changed Type1"

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文