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

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Request now

Reply
ccast
Frequent Visitor

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. 

 

1 ACCEPTED SOLUTION

source

ssss.png

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

rst.png

 

View solution in original post

5 REPLIES 5
dufoq3
Super User
Super User

Hi, what is expected output based on Plain Data?


Note: Check this link to learn how to use my query.
Check this link if you don't know how to provide sample data.

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

 

source

ssss.png

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

rst.png

 

ccast
Frequent Visitor

It worked! thank you so much!

dufoq3
Super User
Super User

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


Note: Check this link to learn how to use my query.
Check this link if you don't know how to provide sample data.

Helpful resources

Announcements
November Power BI Update Carousel

Power BI Monthly Update - November 2025

Check out the November 2025 Power BI update to learn about new features.

Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.