[POWER QUERY] Grouped Running Totals with a maximum condition and a verifier

SunProj3cT

New Member
Joined
Nov 2, 2021
Messages
3
Office Version
  1. 365
Platform
  1. Windows
Good evening, people.

I'm in need of a function on power query that sums the running total across its categories, but compares the running total value to a previously defined maximum for its category. If running total is greater than its maximum, it subtracts its own value and remains the same as (i-1). In addition, it should add 0 to a verification column.

I used this code from myonlinetraininghub (https://www.myonlinetraininghub.com/grouped-running-totals-in-power-query) and it solves part of my problem, but not the entirety of it

Here's a screenshot of an expected result:
runningtotal.png

I would be very pleased if anyone could help me with this. It doesn't seem to be difficult to solve, but I've tried several different solutions and none have worked, so I'm having a hard time with this.

Thanks in advance
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Power Query:
let
    tbl1 = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    Type1 = Table.TransformColumnTypes(tbl1,{{"Group", type text}, {"Value", Int64.Type}}),
    tbl2 = Excel.CurrentWorkbook(){[Name="Table2"]}[Content],
    Type2 = Table.TransformColumnTypes(tbl2,{{"Group", type text}, {"Max", Int64.Type}}),
    tbl4 = Table.AddColumn(Type1,"Max", each Type2{[Group = _[Group]]}[Max]),
    tbl5 = Table.Group(tbl4, {"Group"}, {{"All", each _, type table [Group=nullable text, Value=nullable number, Max=number]}}),
    tbl6 = Table.TransformColumns(tbl5,{{"All", each 
        Table.AddIndexColumn(_,"Index")}}),
    tbl7 = Table.TransformColumns(tbl6,{{"All", (ttc)=>
        let 
            lst1 = List.Skip(List.Generate(()=> [x=0,y=0], each [x]<= Table.RowCount(ttc), each [x = [x] +1, y = 
                if [y] + ttc[Value]{[x]} <= ttc[Max]{0} then [y] + ttc[Value]{[x]} else [y]
            ], each [y]),1)
        in
            Table.AddColumn(ttc,"Running", (z)=> 
                lst1{z[Index]}
            )}}),
    tbl8 = Table.ExpandTableColumn(tbl7, "All", {"Value", "Running"}, {"Value", "Running"}),
    tbl9 = Table.AddIndexColumn(tbl8, "Index", 0, 1, Int64.Type),
    tbl10 = Table.AddColumn(tbl9, "Verifier", each try if _[Running] = tbl9[Running]{_[Index]-1} and 
                                                            _[Group] = tbl9[Group]{_[Index]-1} and 
                                                            _[Value]<>0
                                                        then 0 else 1 otherwise 1),
    Result = Table.RemoveColumns(tbl10,{"Index"})
in
    Result


PQ Running Total.xlsm
ABCDEFGHIJK
1Table1Table2PQ Output
2
3GroupValueGroupMaxGroupValueRunningVerifier
4A4A47A441
5A15B49A15191
6A3C120A3221
7A27A27220
8A12A12341
9A11A11451
10A23A23450
11B10B10101
12B20B20301
13B33B33300
14B2B2321
15B45B45320
16C1C111
17C54C54551
18C5C5601
19C43C431031
20C32C321030
21C1C11041
22C1C11051
23
24
Sheet1
 
Upvote 0
Please try
Power Query:
let
    Source = Table.Join(Excel.CurrentWorkbook(){[Name="Table1"]}[Content],"Group", Excel.CurrentWorkbook(){[Name="Table2"]}[Content],"Group"),
    Runing = Table.Group(Source,"Group",{"A", each let 
      A = Table.AddIndexColumn(_,"i")
      in Table.AddColumn(A,"R", each 
        List.Accumulate(Table.SelectRows(A, (a)=> a[i]<=[i])[Value], [Running=0, Verifier = 1], 
          (s,l)=> [Running = if s[Running]+l >[Max] then s[Running] else s[Running]+l  , Verifier =Number.From(s[Running]+l <=[Max])]  ))
    }),
    ExpandedR = Table.ExpandRecordColumn( Table.ExpandTableColumn(Runing, "A", {"Value", "R"}), "R", {"Running", "Verifier"})
in
    ExpandedR


Book1
ABCDEFGHIJ
1GroupValueGroupMaxGroupValueRunningVerifier
2A4A47A441
3A15B49A15191
4A3C120A3221
5A27A27220
6A12A12341
7A11A11451
8A23A23450
9B10B10101
10B20B20301
11B33B33300
12B2B2321
13B45B45320
14C1C111
15C54C54551
16C5C5601
17C43C431031
18C32C321030
19C1C11041
20C1C11051
Sheet1
 
Upvote 0
Hi, guys. First of all: Thank you so much for helping me! Both solutions seem to work, but @JGordon11 's had some performance issues when I used the real database.

Second: I kind of underestimated the complexity between the operational database and this "dummy" example that I've shown to clarify my problem. So, when I tried to adapt it to my operational database it lacked some important data (such as request#id and sorting those requests with SLA and Total $) which made the result imprecise. I know I might be a lot sassy, but... is there any chance that I could send you a MP with a sanitized (but with real variables) database and a complete explanation for you to help me a little further @Bo_Ry ?
 
Upvote 0

Forum statistics

Threads
1,224,822
Messages
6,181,165
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top