Don't miss your chance to take the Fabric Data Engineer (DP-700) exam on us!
Learn moreThe FabCon + SQLCon recap series starts April 14th at 8am Pacific. If you’re tracking where AI is going inside Fabric, this first session is a can't miss. Register now
I do not know if my previous post went through, it looks like it vanished, so here it is again.
I have an excerpt of a table:
| week | product_id | stock | sales |
| 1 | b_40 | 17 | 325 |
| 2 | b_40 | 19 | 300 |
| 1 | f_3 | 5 | 532 |
| 2 | f_3 | 23 | 425 |
I need to calculate the buy amount by the store. To calculate this i need to do Stock week X - stock week X-1, then add sales amount for week X.
as example: buys week 2 for product b_40:
19(stock week 2) - 17(stock week 1) = 2.
2+ 300(sales week 2) = 302.
so in week 2 the store bought 302 units of product b_40.
my problem is that I do not know how to calculate the 19-17 part. for every product and every week doing this calculation
I was thinking about making a new column with something like: NewColumn = [stock]-[stock-1] which obviously wouldn't work.
help would be most appreciated
Solved! Go to Solution.
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
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.
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.
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)
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
If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.
A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.
Share feedback directly with Fabric product managers, participate in targeted research studies and influence the Fabric roadmap.
| User | Count |
|---|---|
| 5 | |
| 4 | |
| 3 | |
| 2 | |
| 2 |
| User | Count |
|---|---|
| 8 | |
| 6 | |
| 6 | |
| 6 | |
| 5 |