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
brunoiadocicco
Frequent Visitor

Simple recursive calculation - Power Query

Hello,

I'm trying to build the following calculation in Power Query:

dtpt' = t x s (d-1)s = s (d-1) + p + t'
0                        -                         3.600.000
11,20%         (200.000)                 43.200                      3.443.200
20,60%         (300.000)                 20.659                      3.163.859
30,40%         (700.000)                 12.655                      2.476.515
40,50%         (650.000)                 12.383                      1.838.897


Values in black are inputs and values in red are the results I want to get.

In excel is very simple, basically I want to get the previous row (s{d-1}), calculate the correction (t' = s{d-1} x t) and then calculate the next s (s{d-1} + p + t'), and repeat. But, I'm having problems building this logic into M language in Power Query. Someone could help, please?

1 ACCEPTED SOLUTION

@Greg_DecklerHello Greg

Thank you for your reply and for sharing those articles, it help me a lot!

Still struggle a little to refer the values I wanted to use in calculations, but I manage to solve this problem creating a new column of lists instead (if you know a better way to do it, I would love to hear it).

Here is the code I used, in case someone else want to know:

let
    fxBalance = (InitialValue,Payments,InterestRate,Counter,Index) =>

    let

    Correction = if InterestRate{Counter-1} = null then 0 else InitialValue*InterestRate{Counter-1},

    FinalBalance = if InitialValue - Payments{Counter-1} + Correction < 0 then 0 else InitialValue - Payments{Counter-1} + Correction,

    Return = if FinalBalance < 0 or Counter >= Index then FinalBalance else @fxBalance(FinalBalance,Payments,InterestRate,Counter+1,Index)

    in

    Return

in
    fxBalance


Using the function:

Table.AddColumn(#"Personalização Adicionada6", "Personalizar.1", each fxBalance([Initial Value],[Payments List],[Interest Rate List],1,[Index]))


The results:
2.png

ty

View solution in original post

2 REPLIES 2
Greg_Deckler
Community Champion
Community Champion

@brunoiadocicco Last time I was messing around with recursion in Power Query, it had to do with fractals. Attaching PBIX below. Here is a link to the articles. The articles have links to download additional PBIX file examples. Should be something in there to help your particular situation, I think I did a different solution for recursion in each article. 

 

https://www.linkedin.com/pulse/fun-graphing-power-bi-part-3i-greg-deckler-microsoft-mvp-/?msgConvers...

 

https://www.linkedin.com/pulse/fun-graphing-power-bi-part-sqrtpowersqrt82-deckler-microsoft-mvp-/?ms...

 

https://www.linkedin.com/pulse/fun-graphing-power-bi-part-5-by-5-greg-deckler-microsoft-mvp-/?msgCon...

 



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...

@Greg_DecklerHello Greg

Thank you for your reply and for sharing those articles, it help me a lot!

Still struggle a little to refer the values I wanted to use in calculations, but I manage to solve this problem creating a new column of lists instead (if you know a better way to do it, I would love to hear it).

Here is the code I used, in case someone else want to know:

let
    fxBalance = (InitialValue,Payments,InterestRate,Counter,Index) =>

    let

    Correction = if InterestRate{Counter-1} = null then 0 else InitialValue*InterestRate{Counter-1},

    FinalBalance = if InitialValue - Payments{Counter-1} + Correction < 0 then 0 else InitialValue - Payments{Counter-1} + Correction,

    Return = if FinalBalance < 0 or Counter >= Index then FinalBalance else @fxBalance(FinalBalance,Payments,InterestRate,Counter+1,Index)

    in

    Return

in
    fxBalance


Using the function:

Table.AddColumn(#"Personalização Adicionada6", "Personalizar.1", each fxBalance([Initial Value],[Payments List],[Interest Rate List],1,[Index]))


The results:
2.png

ty

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.