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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
ltofanelli
Frequent Visitor

List accumulate in groups

Hi!!

I need to get the previous value and sum with the current row, but for each group of index.
if index = 1 then i need to get him

like the example below.

I find way to do using list.accumulate, but i dont know how to do in groups

= Table.AddColumn(#"Source", "Range", each List.Accumulate(List.Range(#"Source"[Value],0,[Index]),0,(state,current)=>if (state+current) > 60.0 then 60.0 else state+current))

 

Its possible to calculate this on power query?

 

 ltofanelli_0-1603331577332.png

 

thank you!

1 ACCEPTED SOLUTION
PhilipTreacy
Super User
Super User

Hi @ltofanelli 

Please see this example file for solution Grouped Running Total in PQ

Using a custom function and List.Generate you can create grouped running totals like this.

 

Custom Function fxGroupedRunningTotal

 

 

(values as list, grouping as list) as list =>

let
    GRTList = List.Generate
    ( 
        ()=> [ GRT = values{0}, i = 0 ],

        each [i] < List.Count(values),

        each try 
                 if grouping{[i]} = grouping{[i] + 1} 
                 then [GRT = [GRT] + values{[i] + 1}, i = [i] + 1]
                 else [GRT = values{[i] + 1}, i = [i] + 1]
        
             otherwise [i = [i] + 1]
    ,
        each [GRT]
    )
in
    GRTList

 

 

 

This following query loads your data from a table and then calls the function, passing in the ID and Values columns.

 

 

let
    Source = Excel.CurrentWorkbook(){[Name="SourceData"]}[Content],
    BufferedValues = List.Buffer(Source[Value]),
    BufferedGroup = List.Buffer(Source[ID]),

    RT = Table.FromColumns(
    {
      Source[ID], Source[Index], Source[Value],
      fxGroupedRunningTotal(BufferedValues, BufferedGroup)
    },
    {
      "ID",
      "Index",
      "Value",
      "Running Total"
    })
in
    RT

 

 

Phil


If I answered your question please mark my post as the solution.

If my answer helped solve your problem, give it a kudos by clicking on the Thumbs Up.



Did I answer your question? Then please mark my post as the solution.
If I helped you, click on the Thumbs Up to give Kudos.


Blog :: YouTube Channel :: Connect on Linkedin


Proud to be a Super User!


View solution in original post

6 REPLIES 6
Jimmy801
Community Champion
Community Champion

Hello @ltofanelli 

 

here maybe a straightforward solution

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("RczBDcAwCAPAXXinUoDSDhPl0Wb/HYppBA8/TrI9Bj3UiD2iNNtP8RxFBTl5Ft+9vXoytncytpbEVqtsEWjtJ+5JPEkRT+acHw==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [ID = _t, Index = _t, Value = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"ID", type text}, {"Index", Int64.Type}, {"Value", Int64.Type}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"ID"}, {{"AllRows", (tbl)=> Table.AddColumn(tbl,"running total", (add)=> List.Sum(Table.SelectRows(tbl, each [Index]<= add[Index])[Value]))}}),
    #"Expanded AllRows" = Table.ExpandTableColumn(#"Grouped Rows", "AllRows", {"Index", "Value", "running total"}, {"Index", "Value", "running total"})
in
    #"Expanded AllRows"

 

Copy paste this code to the advanced editor in a new blank query to see how the solution works.

If this post helps or solves your problem, please mark it as solution (to help other users find useful content and to acknowledge the work of users that helped you)
Kudoes are nice too

Have fun

Jimmy

PhilipTreacy
Super User
Super User

Hi @ltofanelli 

Please see this example file for solution Grouped Running Total in PQ

Using a custom function and List.Generate you can create grouped running totals like this.

 

Custom Function fxGroupedRunningTotal

 

 

(values as list, grouping as list) as list =>

let
    GRTList = List.Generate
    ( 
        ()=> [ GRT = values{0}, i = 0 ],

        each [i] < List.Count(values),

        each try 
                 if grouping{[i]} = grouping{[i] + 1} 
                 then [GRT = [GRT] + values{[i] + 1}, i = [i] + 1]
                 else [GRT = values{[i] + 1}, i = [i] + 1]
        
             otherwise [i = [i] + 1]
    ,
        each [GRT]
    )
in
    GRTList

 

 

 

This following query loads your data from a table and then calls the function, passing in the ID and Values columns.

 

 

let
    Source = Excel.CurrentWorkbook(){[Name="SourceData"]}[Content],
    BufferedValues = List.Buffer(Source[Value]),
    BufferedGroup = List.Buffer(Source[ID]),

    RT = Table.FromColumns(
    {
      Source[ID], Source[Index], Source[Value],
      fxGroupedRunningTotal(BufferedValues, BufferedGroup)
    },
    {
      "ID",
      "Index",
      "Value",
      "Running Total"
    })
in
    RT

 

 

Phil


If I answered your question please mark my post as the solution.

If my answer helped solve your problem, give it a kudos by clicking on the Thumbs Up.



Did I answer your question? Then please mark my post as the solution.
If I helped you, click on the Thumbs Up to give Kudos.


Blog :: YouTube Channel :: Connect on Linkedin


Proud to be a Super User!


Hi,

this code work in power bi?

 

Yes



Did I answer your question? Then please mark my post as the solution.
If I helped you, click on the Thumbs Up to give Kudos.


Blog :: YouTube Channel :: Connect on Linkedin


Proud to be a Super User!


@PhilipTreacy , thank you so much!
You save my sleep haha
Tomorrow i ll study your code to learn more.

@ltofanelli 

no worries 🙂



Did I answer your question? Then please mark my post as the solution.
If I helped you, click on the Thumbs Up to give Kudos.


Blog :: YouTube Channel :: Connect on Linkedin


Proud to be a Super User!


Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

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.

Top Solution Authors