Forum Discussion

ccast's avatar
ccast
Frequent Visitor
2 years ago
Solved

Acumulative Totals suming previous row

Hi everybody!

 

I need your help with the following

I am allocating stock of different products according to the availability of each store for each product.

 

The problem is that when I do the grouping considering Product and Total to allocate, Power Query uses the availability for the previous product in the same store instead of using the one for that product. 

 

  • source

    code

    let
        Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
        fx = (tbl) => 
            [lst = List.Buffer(Table.ToRows(tbl)),
            excess = tbl[Total to Allocate]{0} - List.Sum(tbl[Store Needs]),
            gen = List.Generate(
                () => 
                    [
                        i = 0, 
                        alloc = List.Min({lst{0}{2}, lst{0}{3}}), 
                        stock = lst{0}{3} - alloc
                    ],
                (x) => x[i] < List.Count(lst),
                (x) => 
                    [
                        i = x[i] + 1, 
                        alloc = List.Min({lst{i}{2}, x[stock]}),
                        stock = x[stock] - alloc
                    ],
                (x) => lst{x[i]} & {x[alloc], if x[i] = List.Count(lst) - 1 then excess else 0}
            )][gen],
        group = Table.Group(
            Source, 
            "Product", 
            {"x", fx}
        ),
        tbl = Table.FromRows(List.Combine(group[x]), Table.ColumnNames(Source) & {"Allocated", "Excess/Missing"})
    in
        tbl

     result

     

5 Replies

  • dufoq3's avatar
    dufoq3
    Icon for Community Champion rankCommunity Champion

    Hi ccast, provide sample data in usable format and expected result based on that.

  • ccast's avatar
    ccast
    Frequent Visitor

    Hi dufoq3 

     

    See below: the total to allocate should be applied from store A to B and to C applying the remaining total following a particular order (A, B and C). 

     

    Plain Data:

    ProductStoreStore NeedsTotal to AllocateAllocationExcess/Missing
    JIDH1A2003502000
    JIDH1B1003501000
    JIDH1C100350 50-50
    JIDH3A0500000
    JIDH3B2000500020000
    JIDH3C

    30

    5000302700

     

    At the moment, I am getting the data incorrectly as Power Query uses the needs for the first product on the second one:

     

    Error: 

    ProductStoreStore NeedsTotal to AllocateAllocationExcess/Missing
    JIDH1A2003502000
    JIDH1B1003501000
    JIDH1C100350 50-50
    JIDH3A05000200-200
    JIDH3B20005000100-100
    JIDH3C

    30

    5000100-100

     

    • AlienSx's avatar
      AlienSx
      Icon for Super User rankSuper User

      source

      code

      let
          Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
          fx = (tbl) => 
              [lst = List.Buffer(Table.ToRows(tbl)),
              excess = tbl[Total to Allocate]{0} - List.Sum(tbl[Store Needs]),
              gen = List.Generate(
                  () => 
                      [
                          i = 0, 
                          alloc = List.Min({lst{0}{2}, lst{0}{3}}), 
                          stock = lst{0}{3} - alloc
                      ],
                  (x) => x[i] < List.Count(lst),
                  (x) => 
                      [
                          i = x[i] + 1, 
                          alloc = List.Min({lst{i}{2}, x[stock]}),
                          stock = x[stock] - alloc
                      ],
                  (x) => lst{x[i]} & {x[alloc], if x[i] = List.Count(lst) - 1 then excess else 0}
              )][gen],
          group = Table.Group(
              Source, 
              "Product", 
              {"x", fx}
          ),
          tbl = Table.FromRows(List.Combine(group[x]), Table.ColumnNames(Source) & {"Allocated", "Excess/Missing"})
      in
          tbl

       result

       

      • ccast's avatar
        ccast
        Frequent Visitor

        It worked! thank you so much!

  • dufoq3's avatar
    dufoq3
    Icon for Community Champion rankCommunity Champion

    Hi, what is expected output based on Plain Data?