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
androo235
Advocate I
Advocate I

Running Total by Category with Compound Interest. List.Accumulate/Generate

Data and XL solution and attempts to solve with Power Query 

 

I have been trying to solve this problem with power query. I started by using refer to previous row methods but thought these would likely be "too expensive" when used on my production dataset (about 500k rows) judging by how slow they were even with the sample data. I have then tried to make it work with list.accumulate and/or list.generate but, without success. The sample data and my attempts with power query along with a standard (structured table) XL solution which does actually work , even with 500k rows, and shows the result I'm aiming at (screenshot below) are in the XL file linked to above. But, if I can do this in PQ that's where I'd rather have it done.

CompoundIntbyCatinXL.JPG

4 REPLIES 4
ImkeF
Community Champion
Community Champion

Hi @androo235 ,
this function calculates the compound interest:

 

( RTColumnName as text, MyTable as table, ValueColumn as text, InterestColumn as text) =>
let
    Source = MyTable,
    BuffValues = Table.Buffer( MyTable ),
    RunningTotal = 
      List.Skip(
        List.Generate ( 
        () => [ RT = 0, ValueColumn), RowIndex = 0 ],
        each  [RowIndex] <= Table.RowCount(BuffValues),
        each  [ RT = ([RT] + Record.Field(BuffValues{[RowIndex]}, ValueColumn) )  * (1 + Record.Field(BuffValues{[RowIndex]}, InterestColumn)),
                RowIndex = [RowIndex] + 1 ],
        each  [RT] 
        )
      ),
    #"Combined Table + RT" = 
      Table.FromColumns( 
        Table.ToColumns( MyTable )   
           & { Value.ReplaceType( RunningTotal, type {Int64.Type} ) } , 
        Table.ColumnNames( MyTable ) & { RTColumnName } )
in
    #"Combined Table + RT"

 

also check the solution with "fnCompounding" in the file attached.

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

androo235
Advocate I
Advocate I

thanks Imke. Here's the link again (a fresh one even if it looks and is the same) It's to my personal one-drive and says it's an "anyone can view" link and is set to open in a new page. I'm assuming "can view" includes being able to download and then edit it. If it isn't possible to download and edit please tell me and I'll just have to post a "can edit" link. Data and XL Solution and attempt to solve with Power Query  Thanks.

ImkeF
Community Champion
Community Champion

Hi @androo235 ,
it should be possible to do this fast in PQ using the right method:
Memory efficient clustered running total in Power BI – (thebiccountant.com)
I have been using this on millions of rows without any issues.
I you are having trouble applying it to your case, please re-open the file you had shared.


Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

androo235
Advocate I
Advocate I

Seems no-one has  an answer to this.

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!

November Power BI Update Carousel

Power BI Monthly Update - November 2025

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

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.