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 ...
  • AlienSx's avatar
    AlienSx
    2 years ago

    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