Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
5 years ago
Solved

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

  • 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.

     

3 Replies

  • Jimmy801's avatar
    Jimmy801
    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"

     

    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

     

  • v-alq-msft's avatar
    v-alq-msft
    Community Support

    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.

     

    • Anonymous's avatar
      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)