Forum Discussion
calculate a running total based on a filter
- 1 year ago
VJ_Lsf Hi! Here the Advanced Editor Codefor your scope:
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WcnR0VNJRMgFiQwOlWB0UAUMkASMjkIgRkogZSMAYScACJGACFnBycgJyjEECpkgCYFPNkASAdgIJcwwRC6XYWAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Customer = _t, Balance = _t, Week = _t]),
// Convert column types
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Customer", type text}, {"Balance", Int64.Type}, {"Week", Int64.Type}}),// Sort by Customer, then Week ascending
#"Sorted Rows" = Table.Sort(#"Changed Type",{{"Customer", Order.Ascending}, {"Week", Order.Ascending}}),// Add Index by Customer
#"Grouped" = Table.Group(#"Sorted Rows", {"Customer"}, {
{"AllData", each
Table.AddIndexColumn(_, "Index", 0, 1, Int64.Type)
}
}),
// Expand the grouped table
#"Expanded Table" = Table.ExpandTableColumn(#"Grouped", "AllData", { "Balance", "Week", "Index"}),// Add Running Total using List.Sum over previous rows
#"Added Running Total" = Table.AddColumn(#"Expanded Table", "RunningBalance", (row) =>
let
customer = row[Customer],
index = row[Index],
balances = Table.SelectRows(#"Expanded Table", each [Customer] = customer and [Index] <= index)[Balance]
in
List.Sum(balances)
),// Remove the index if not needed
#"Removed Columns" = Table.RemoveColumns(#"Added Running Total",{"Index"})in
#"Removed Columns"You'll obtain the Running Balance for each Customer by Week ASC:
If it's ok, please accept my answer as solution!
BBF
VJ_Lsf Hi! Here the Advanced Editor Codefor your scope:
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WcnR0VNJRMgFiQwOlWB0UAUMkASMjkIgRkogZSMAYScACJGACFnBycgJyjEECpkgCYFPNkASAdgIJcwwRC6XYWAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Customer = _t, Balance = _t, Week = _t]),
// Convert column types
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Customer", type text}, {"Balance", Int64.Type}, {"Week", Int64.Type}}),
// Sort by Customer, then Week ascending
#"Sorted Rows" = Table.Sort(#"Changed Type",{{"Customer", Order.Ascending}, {"Week", Order.Ascending}}),
// Add Index by Customer
#"Grouped" = Table.Group(#"Sorted Rows", {"Customer"}, {
{"AllData", each
Table.AddIndexColumn(_, "Index", 0, 1, Int64.Type)
}
}),
// Expand the grouped table
#"Expanded Table" = Table.ExpandTableColumn(#"Grouped", "AllData", { "Balance", "Week", "Index"}),
// Add Running Total using List.Sum over previous rows
#"Added Running Total" = Table.AddColumn(#"Expanded Table", "RunningBalance", (row) =>
let
customer = row[Customer],
index = row[Index],
balances = Table.SelectRows(#"Expanded Table", each [Customer] = customer and [Index] <= index)[Balance]
in
List.Sum(balances)
),
// Remove the index if not needed
#"Removed Columns" = Table.RemoveColumns(#"Added Running Total",{"Index"})
in
#"Removed Columns"
You'll obtain the Running Balance for each Customer by Week ASC:
If it's ok, please accept my answer as solution!
BBF