Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

The 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

Reply
Anonymous
Not applicable

calculate multiple filtered values from same column

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:

weekproduct_idstocksales
1b_4017325
2b_4019300
1f_35532
2f_323425

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

2 ACCEPTED SOLUTIONS
Jimmy801
Community Champion
Community Champion

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"

Jimmy801_1-1606217354249.png

 

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

 

View solution in original post

v-alq-msft
Community Support
Community Support

Hi, @Anonymous 

 

Based on your description, I created data to reproduce your scenario. The pbix file is attached in the end.

Table:

c1.png

 

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

c2.png

 

Result:

c3.png

 

Best Regards

Allan

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

 

View solution in original post

3 REPLIES 3
v-alq-msft
Community Support
Community Support

Hi, @Anonymous 

 

Based on your description, I created data to reproduce your scenario. The pbix file is attached in the end.

Table:

c1.png

 

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

c2.png

 

Result:

c3.png

 

Best Regards

Allan

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

 

Anonymous
Not 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)

Jimmy801
Community Champion
Community Champion

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"

Jimmy801_1-1606217354249.png

 

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

 

Helpful resources

Announcements
New to Fabric survey Carousel

New to Fabric Survey

If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.

Join our Fabric User Panel

Join our Fabric User Panel

Share feedback directly with Fabric product managers, participate in targeted research studies and influence the Fabric roadmap.

March Power BI Update Carousel

Power BI Community Update - March 2026

Check out the March 2026 Power BI update to learn about new features.