Forum Discussion

PolinaOr's avatar
PolinaOr
Icon for Helper I rankHelper I
5 months ago
Solved

% of difference from previous rows in a group for large dataset for Power query

Good day, everyone.   As posts, related to this question, are old or require essentially duplicating the dataset and re-merging it, which i think will greatly impact the productivity due to the siz...
  • ronrsnfld's avatar
    5 months ago

    You can add a shifted column within the Item Group to do the math:

    let
        Source = myTable,
    
    //Group by Item
        #"Grouped Rows" = Table.Group(Source, {"Item"}, {
            {"Change", (t)=>
    
        //within each Item, add a shifted column to do the math
                [a=Table.FromColumns(
                    Table.ToColumns(t) &
                    {{null} & List.RemoveLastN(t[Price on PO],1)}),
                 b=Table.AddColumn(a,"Change from last PO", each ([Column2]-[Column3])/[Column3]??0, Percentage.Type),
                 c=Table.RemoveColumns(b,{"Column3"})
    
                ][c],
                type table[Column1=number, Column2=number, Change from last PO=Percentage.Type]}
            }),
    
    //clean up and expand
        #"Removed Columns" = Table.RemoveColumns(#"Grouped Rows",{"Item"}),
        #"Expanded Change" = Table.ExpandTableColumn(#"Removed Columns", "Change", {"Column1", "Column2", "Change from last PO"}, {"Item", "Price on PO", "Change from last PO"})
    in
        #"Expanded Change"

     

     

    By the way, for your cumulative change it seems you are computing a running total of the row by row changes. Are you sure that is what you want to do? I would think it more appropriate to calculate the cumulative change as the percentage change between the current row and the first row in each item group.

     

    If that is what you really want, it may be easier to use List.Generate to create both of those columns as shown below:

    let
        Source = myTable,
    
    //Group by Item
        #"Grouped Rows" = Table.Group(Source, {"Item"}, {
            {"Change", (t)=> 
                [a=List.Generate(
                    ()=>[w=t{0}[Price on PO], x=0, y=0, idx=0],
                    each [idx] < Table.RowCount(t),
                    each [idx=[idx]+1, w=t{idx}[Price on PO],
                                       x=(t{idx}[Price on PO]-t{idx-1}[Price on PO]) / t{idx-1}[Price on PO],
                                       y=(t{idx}[Price on PO] - t{0}[Price on PO]) / t{0}[Price on PO]],
                    each {[w],[x],[y]}),
                b=List.Transform(a, each Record.FromList(_,{"Price on PO","Change from last PO","Cumulative Change"} ))
                    
                    ][b], type table[Item=Int64.Type, Price on PO=number, Change from last PO=Percentage.Type, Cumulative Change=Percentage.Type]
            
    
            }}),
        #"Expanded Change" = Table.ExpandListColumn(#"Grouped Rows", "Change"),
        #"Expanded Change1" = Table.ExpandRecordColumn(#"Expanded Change", "Change", {"Price on PO", "Change from last PO", "Cumulative Change"})
    in
        #"Expanded Change1"