Power Query、Power BI、Dax 中分组列的累计和

Cumulative sum of grouped columns in Powerquery, PowerBI, Dax

我有一个列名称日期、产品名称、客户名称、销售价值、基于产品名称和日期的销售价值总和、销售百分比,即销售价值/销售价值总和。我想要的是函数 = if(product name = product name, percentage of sales 让我们说 F1 + F2, F1)

我需要使用 Dax 或 power query 将附加图像中的最后一列重新创建到 power bi 中

在 powerquery/M 中,您要查找的是 sale% 列在 date 分组后的累计总和,并且产品类型

let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"date", type date}, {"Product type", type text}, {"cm name", type text}, {"sales", type number}, {"sum of sale", type number}, {"sale%", type number}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"date", "Product type"}, {
    {"data", each 
    let x=Table.AddIndexColumn(_, "Index", 1, 1) ,
    y=Table.AddColumn(x, "Rank", each List.Sum(List.FirstN(x[#"sale%"],[Index])))
    in y , type table [date=nullable date, Product type=nullable text, cm name=nullable text, sales=nullable number, sum of sale=nullable number, #"sale%"=nullable Percentage.Type, Rank=nullable Percentage.Type]}
    }),
#"Expanded data" = Table.ExpandTableColumn(#"Grouped Rows", "data", {"cm name", "sales", "sum of sale", "sale%", "Rank"}, {"cm name", "sales", "sum of sale", "sale%", "Rank"})
in #"Expanded data"

然后您可以使用顶部的箭头将排名列过滤到您想要的任何范围,例如 75%

====

您还可以在前 4 列中同时生成三个额外的列作为组的一部分

let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Grouped Rows" = Table.Group(Source, {"date", "Product type"}, {
    {"sum of sale", each List.Sum([sales]), type number}, 
    {"data", each 
    let x=Table.AddIndexColumn( Table.AddColumn(_, "sale%", each [sales]/List.Sum(Source[sales])), "Index", 1, 1) ,
    y=Table.AddColumn(x, "Rank", each List.Sum(List.FirstN(x[#"sale%"],[Index])))
    in y , type table [date=nullable date, Product type=nullable text, cm name=nullable text, sales=nullable number, sum of sale=nullable number, #"sale%"=nullable Percentage.Type, Rank=nullable Percentage.Type]}
    }),
#"Expanded data" = Table.ExpandTableColumn(#"Grouped Rows", "data", {"cm name", "sales", "sale%", "Rank"}, {"cm name", "sales", "sale%", "Rank"}),
#"Changed Type" = Table.TransformColumnTypes(#"Expanded data",{{"date", type date}, {"Product type", type text}, {"sum of sale", type number}, {"sales", type number}, {"sale%", Percentage.Type}, {"Rank", Percentage.Type}, {"cm name", type text}})
in  #"Changed Type"