Forum Discussion
calculate multiple filtered values from same column
- 5 years ago
Hello Anonymous
in power query you can work with index-column and grouping. Here an example that shows you how to handle your puzzle
let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUUqKNzEAUobmQMLYyFQpVidayQhJ3BIkbmAAFgepT4s3BpKmIGxsBFcNETUCESYgQ2IB", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [week = _t, product_id = _t, stock = _t, sales = _t]), #"Sorted Rows" = Table.Sort(Source,{{"product_id", Order.Ascending},{"week", Order.Ascending}}), #"Changed Type" = Table.TransformColumnTypes(#"Sorted Rows",{{"week", Int64.Type}, {"product_id", type text}, {"stock", Int64.Type}, {"sales", Int64.Type}}), #"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 1, 1), #"Grouped Rows" = Table.Group ( #"Added Index", {"product_id"}, { { "AllRows", (tbl)=> Table.RemoveColumns(Table.AddColumn(tbl, "TotalBought", (add)=> let stockpreviousweek = try Table.SelectRows(tbl, each [Index]= add[Index]-1)[stock]{0} otherwise 0 in add[stock]- stockpreviousweek + add[sales]), {"Index"}) } } ), #"Expanded AllRows" = Table.ExpandTableColumn(#"Grouped Rows", "AllRows", {"week", "stock", "sales", "TotalBought"}, {"week", "stock", "sales", "TotalBought"}) in #"Expanded AllRows"Copy paste this code to the advanced editor in a new blank query to see how the solution works.
If this post helps or solves your problem, please mark it as solution (to help other users find useful content and to acknowledge the work of users that helped you)
Kudoes are nice too
Have fun
Jimmy - 5 years ago
Hi, Anonymous
Based on your description, I created data to reproduce your scenario. The pbix file is attached in the end.
Table:
You may create a custom column with the following m codes.
let week = [Week],pid = [Product_id], laststock = try Table.SelectRows(#"Changed Type",each [Product_id]=pid and [Week]=week-1)[Stock]{0} otherwise 0 in [Sales]+[Stock]-laststockResult:
Best Regards
Allan
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Hi, Anonymous
Based on your description, I created data to reproduce your scenario. The pbix file is attached in the end.
Table:
You may create a custom column with the following m codes.
let
week = [Week],pid = [Product_id],
laststock =
try
Table.SelectRows(#"Changed Type",each [Product_id]=pid and [Week]=week-1)[Stock]{0}
otherwise
0
in
[Sales]+[Stock]-laststock
Result:
Best Regards
Allan
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
- Anonymous5 years agoNot applicable
hi, Thanks for the help.
This solution worked, albeit with a bit of naggling. powerBI complained about the "changed type". It was remedied when I manually did a step before this where i changed the type from whole numbers to whole numbers on one of the columns.
Also the solution took a really long time to figure out, on my pc it took from mondayafternoon to somewhere between 2-7AM. (the full tableis 3.5M rows long)