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

Get certified in Microsoft Fabric—for free! For a limited time, get a free DP-600 exam voucher to use by the end of 2024. Register now

Reply
PQRookie
Frequent Visitor

Recursive running totals

Hi,

 

I have a challenge where I have landed on that I have to conduct multiple running totals (RT) over a data set of some 1-2000 records (growing).
The issues I have are:

1. The number of recursions are manually decided based on the data, not nice

2. It is very slow, even worse...


Key questions:
1. Is there a way to recursively call the RT function based on a global counter variable? 
2. Can multiple function calls reuse a table in memory (each RT call creates a buffer) , I am assuming that this would speed up things.

3. Are there other ways to speed this up, any ideas of applicable reference sites would be appreciated.

The RT function I found on internet and looks like this, works like a sharm:

= ( New_Column as text, MyTable as table, Sum_Column as text) =>

let

    Source = MyTable,

    AmountBuffer= List.Buffer( Table.Column( MyTable, Sum_Column)),

    SumAV = List.Generate(

                   () => [ RunningTotal = AmountBuffer{0}, Counter = 0 ],

                   each [Counter] < List.Count(AmountBuffer),

                   each [ RunningTotal = List.Sum({[ RunningTotal ], AmountBuffer{[Counter]+1}}), Counter = [Counter] + 1 ],

                   each [RunningTotal]

    ),

    Consolidation = Table.FromColumns(

        Table.ToColumns(Source) & {Value.ReplaceType( SumAV, type {Currency.Type})},

        Table.ColumnNames(Source) & {New_Column}

    )

in

    Consolidation

 

5 REPLIES 5
PQRookie
Frequent Visitor

Hi and thanks for this; the case is a bit complicated, but I make an attempt.

 

I am collecting fund transactions from various sites and trying to consolidate these. For some sites the challenge is to translate funds with foreign currency into my domestic one.

 

The key issue appears when shares are sold and to calculate the change to the domestic total acquisition cost for a specific fund.

In the example below the sales amount (Amt YYY) and the result (Res YYY) is correct in the foregin currency. Just calculating the acquisition value impact using the the exchange rate for the sales date will give a skewed figure as the domestic acquisition value is agregated over time with different exchange rates (see pink cells)

 

What I have tried to do is to calculate the domestic acquisition value per share for each date (AV/Share ZZZ) and then use that to calculate the domestic acquisition value (AV ZZZ) by using the AV/Share and transaction quantity and then then calculating the total acquisition value (Tot AV ZZZ).

 

Link to data attached Calculating domestic AV 

 

As mentioned a bit complicated, but hopefully possible to grasp.

Omid_Motamedise
Memorable Member
Memorable Member

List.Generate is the best way to use for running total and you use it in the best way but do not use it as a custom function and use it directly as a part of your code, additionally instead of using List.Sum like List.Sum({[ RunningTotal ], AmountBuffer{[Counter]+1}}), use simple sum operation as 

 

[ RunningTotal ]+ AmountBuffer{[Counter]+1}

to make it more efficient and if you whant to apply it over several columns do it at once by adding field value over List.Generate

lbendlin
Super User
Super User

Recursions are very seldom really necessary. Oftentimes you can use simple list operations to achieve the same goal.

 

Please provide sample data that covers your issue or question completely, in a usable format (not as a screenshot).

Do not include sensitive information. Do not include anything that is unrelated to the issue or question.

Need help uploading data? https://community.fabric.microsoft.com/t5/Community-Blog/How-to-provide-sample-data-in-the-Power-BI-...

Please show the expected outcome based on the sample data you provided.

Want faster answers? https://community.fabric.microsoft.com/t5/Desktop/How-to-Get-Your-Question-Answered-Quickly/m-p/1447...

Hi and thanks for this; the case is a bit complicated, but I make an attempt.

 

I am collecting fund transactions from various sites and trying to consolidate these. For some sites the challenge is to translate funds with foreign currency into my domestic one.

 

The key issue appears when shares are sold and to calculate the change to the domestic total acquisition cost for a specific fund.

In the example below the sales amount (Amt YYY) and the result (Res YYY) translates correctly into the acquisition value in the foreign currency. Just calculating the acquisition value impact using the the exchange rate for the sales date will give a skewed figure as the domestic acquisition value is aggregated over time with different exchange rates (see pink cells).

 

What I have tried to do is to calculate the domestic acquisition value per share for each date (AV/Share ZZZ) and then use that to calculate the domestic acquisition value (AV ZZZ) by using the AV/Share and transaction quantity and then calculating the total acquisition value (Tot AV ZZZ).

 

See attached link for the data. Calculating domestic AV 

 

As mentioned a bit complicated, but hopefully possible to grasp.

Where's the recursive part in your process?

Helpful resources

Announcements
November Carousel

Fabric Community Update - November 2024

Find out what's new and trending in the Fabric Community.

Live Sessions with Fabric DB

Be one of the first to start using Fabric Databases

Starting December 3, join live sessions with database experts and the Fabric product team to learn just how easy it is to get started.

Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Early Bird pricing ends December 9th.

Nov PBI Update Carousel

Power BI Monthly Update - November 2024

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