Forum Discussion
rhys1987
6 years agoFrequent Visitor
Subtract running total based on multiple conditions
Hi, I've been trying to do calculate a running total that subtracts based on multiple conditions in power query, im thinking DAX might be more suitable. The product might change but it need...
- Anonymous6 years ago
assuming there are no duplicate rows, we can make use of the Table.PositionOf function and avoid the steps to introduce the auxiliary index column and then to eliminate it.
let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjQyVtJRcgwI8HEF0oZAbAqiDcCMWB1sCkyAGMzELm0IkTbAIW2MLG1uYYkkbQTEZiBlICdY4lEANgavND7jiZKOBQA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Store = _t, Product = _t, war = _t, QTY = _t, TOTAL = _t, CheckSub = _t]), #"Changed Type" = Table.TransformColumnTypes(Source,{{"TOTAL", type number}, {"QTY", type number}}), grp= Table.Group(#"Changed Type", "TOTAL", {"mmm", (r)=> Table.AddColumn(r,"sub", each List.Max({r[TOTAL]{0}-List.Accumulate({0..Table.PositionOf(r,_)},0,(s,c)=>s+r[QTY]{c}),0}))},GroupKind.Local, (x,y)=>Number.From( x=y or y<>null)), #"Expanded mmm" = Table.ExpandTableColumn(grp, "mmm", {"Store", "Product", "war", "QTY", "CheckSub", "sub"}, {"Store", "Product", "war", "QTY", "CheckSub", "sub"}) in #"Expanded mmm"
Anonymous
6 years agoNot applicable
of course there are simpler and perhaps direct solutions, but just to use some particular functions that MS developers offer us, ...
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjQyVtJRcgwI8HEF0oZAbAqiDcCMWB1sCkyAGMzELm0IkTbAIW2MLG1uYYkkbQTEZiBlICdY4lEANgavND7jiZKOBQA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Store = _t, Product = _t, war = _t, QTY = _t, TOTAL = _t, CheckSub = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"TOTAL", type number}, {"QTY", type number}}),
grp= Table.Group(#"Changed Type", "TOTAL", {"mmm", (r)=> Table.AddColumn(Table.AddIndexColumn(r,"idx",0,1),"sub", each List.Max({r[TOTAL]{0}-List.Accumulate({0.._[idx]},0,(s,c)=>s+r[QTY]{c}),0}))
},GroupKind.Local, (x,y)=>Number.From( x=y or y<>null)),
#"Expanded mmm" = Table.ExpandTableColumn(grp, "mmm", {"Store", "Product", "war", "QTY", "CheckSub", "idx", "sub"}, {"Store", "Product", "war", "QTY", "CheckSub", "idx", "sub"}),
#"Removed Columns" = Table.RemoveColumns(#"Expanded mmm",{"idx"})
in
#"Removed Columns"
this solution is based on the fact that the empty cells of the TOTAL column are set to null.
If this value is different, for example empty, you have to change the corresponding value in the expression in the table.group
Anonymous
6 years agoNot applicable
assuming there are no duplicate rows, we can make use of the Table.PositionOf function and avoid the steps to introduce the auxiliary index column and then to eliminate it.
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjQyVtJRcgwI8HEF0oZAbAqiDcCMWB1sCkyAGMzELm0IkTbAIW2MLG1uYYkkbQTEZiBlICdY4lEANgavND7jiZKOBQA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Store = _t, Product = _t, war = _t, QTY = _t, TOTAL = _t, CheckSub = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"TOTAL", type number}, {"QTY", type number}}),
grp= Table.Group(#"Changed Type", "TOTAL", {"mmm", (r)=> Table.AddColumn(r,"sub", each List.Max({r[TOTAL]{0}-List.Accumulate({0..Table.PositionOf(r,_)},0,(s,c)=>s+r[QTY]{c}),0}))},GroupKind.Local, (x,y)=>Number.From( x=y or y<>null)),
#"Expanded mmm" = Table.ExpandTableColumn(grp, "mmm", {"Store", "Product", "war", "QTY", "CheckSub", "sub"}, {"Store", "Product", "war", "QTY", "CheckSub", "sub"})
in
#"Expanded mmm"