计算 PowerBI 桌面中每个组的 运行 行总和的措施
Measure to calculate Running sum of n rows for each group in PowerBI desktop
我有此数据以及组和值。我想对按“日期”排序的 4 行的滚动 window 求和“值”,如“结果”所示。我想为每个“组”分别进行此计算。
你需要一个简单的运行总和:
RunningSum =
var currRow = SELECTEDVALUE('ValByGroup'[Date])
return
CALCULATE( sum('ValByGroup'[Value]), filter(ALLEXCEPT('ValByGroup', 'ValByGroup'[Group]), 'ValByGroup'[Date] <= currRow && 'ValByGroup'[Date] > currRow-4 ))
编辑:
我们可以向 table 添加额外的索引列。
举个例子。
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjIwMtI1MAQiJR2lRCA2UIrVwSJsiCpshF3YBLshpthVg8xOwm42FmET7MKm2IWNEcKxAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Date = _t, Group = _t, Value = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type date}, {"Group", type text}, {"Value", Int64.Type}}),
#"Sorted Rows" = Table.Sort(#"Changed Type",{{"Date", Order.Ascending}}),
#"Grouped Rows" = Table.Group(#"Sorted Rows", {"Group"}, {{"Count", each _, type table [Date=nullable date, Group=nullable text, Value=nullable number]}}),
#"Added Custom" = Table.AddColumn(#"Grouped Rows", "Custom", each Table.AddIndexColumn([Count],"index",1)),
#"Expanded Custom" = Table.ExpandTableColumn(#"Added Custom", "Custom", {"Date", "Group", "Value", "index"}, {"Custom.Date", "Custom.Group", "Custom.Value", "Custom.index"}),
#"Removed Columns" = Table.RemoveColumns(#"Expanded Custom",{"Count", "Group"}),
#"Renamed Columns" = Table.RenameColumns(#"Removed Columns",{{"Custom.Date", "Date"}, {"Custom.Group", "Group"}, {"Custom.Value", "Value"}, {"Custom.index", "index"}})
in
#"Renamed Columns"
然后我们可以在我们的度量中使用 IndexColumn 而不是 [Date]
我有此数据以及组和值。我想对按“日期”排序的 4 行的滚动 window 求和“值”,如“结果”所示。我想为每个“组”分别进行此计算。
你需要一个简单的运行总和:
RunningSum =
var currRow = SELECTEDVALUE('ValByGroup'[Date])
return
CALCULATE( sum('ValByGroup'[Value]), filter(ALLEXCEPT('ValByGroup', 'ValByGroup'[Group]), 'ValByGroup'[Date] <= currRow && 'ValByGroup'[Date] > currRow-4 ))
编辑:
我们可以向 table 添加额外的索引列。 举个例子。
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjIwMtI1MAQiJR2lRCA2UIrVwSJsiCpshF3YBLshpthVg8xOwm42FmET7MKm2IWNEcKxAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Date = _t, Group = _t, Value = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type date}, {"Group", type text}, {"Value", Int64.Type}}),
#"Sorted Rows" = Table.Sort(#"Changed Type",{{"Date", Order.Ascending}}),
#"Grouped Rows" = Table.Group(#"Sorted Rows", {"Group"}, {{"Count", each _, type table [Date=nullable date, Group=nullable text, Value=nullable number]}}),
#"Added Custom" = Table.AddColumn(#"Grouped Rows", "Custom", each Table.AddIndexColumn([Count],"index",1)),
#"Expanded Custom" = Table.ExpandTableColumn(#"Added Custom", "Custom", {"Date", "Group", "Value", "index"}, {"Custom.Date", "Custom.Group", "Custom.Value", "Custom.index"}),
#"Removed Columns" = Table.RemoveColumns(#"Expanded Custom",{"Count", "Group"}),
#"Renamed Columns" = Table.RenameColumns(#"Removed Columns",{{"Custom.Date", "Date"}, {"Custom.Group", "Group"}, {"Custom.Value", "Value"}, {"Custom.index", "index"}})
in
#"Renamed Columns"
然后我们可以在我们的度量中使用 IndexColumn 而不是 [Date]