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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
AdamPBIDev
Regular Visitor

Recursive Dax Calculation

Dear Dax Gurus,

 

Would appreciate some help - I'm looking to create a calculated column forecast which at index 0 simply takes the (Activity + (Activity * Activity Change %)), but for all other indexes takes the previous row calculated forecast and applies the current row activity change %. 

Note the index resets at each change in (Cost Centre)"Code".

 

Screenshot 1

Question 3.png

 

Sample Data

 

Month YearCodeActivity Change %ActivityIndexForecast Formula
01/03/20221002.0%304803108 =D2+(D2*C2)
01/04/2022100-4.0% 12984 =F2+(F2*C3)
01/05/20221005.0% 23134  
01/06/20221007.0% 33353  
01/07/2022100-4.0% 43219  
01/08/20221008.0% 53476  
01/09/2022100-3.0% 63371  
01/10/20221002.0% 73439  
01/11/20221004.0% 83576  
01/12/2022100-6.0% 93363  
01/03/20222001.7%344803506 =D12+(D12*C12)
01/04/2022200-3.4% 13387 =F12+(F12*C13)
01/05/20222004.2% 23530  
01/06/20222005.9% 33739  
01/07/2022200-3.4% 43611  
01/08/20222006.8% 53857  
01/09/2022200-2.6% 63757  
01/10/20222001.7% 73822  
01/11/20222003.4% 83952  
01/12/2022200-5.2% 93749  

 

I did try Gerhard Brueckl's interesting approach for creating this entirely in DAX Link  but unfortunately I couldn't get this to work at Cost Centre Code level (works perfectly at an overall level) so have gone down the path of creating the above table instead

 

Thanks in advance

Adam

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi @AdamPBIDev,

AFAIK, current power bi data model tables do not include row and column index AND DAX expression does not support doing recursive calculations.

Previous Value (“Recursion”) in DAX - Microsoft Power BI Community

You can enter to query editor to do these calculations but it will be poor performance on recursive calculations.

Recursive Functions in Power BI / Power Query — The Power User
Regards,

Xiaoxin Sheng

View solution in original post

6 REPLIES 6
AntrikshSharma
Super User
Super User

@AdamPBIDev @regnig This should be done in PQ as List.Accumulate allows for easy recursion.

 

let
    Source = Table,
    Group = Table.Group (
        Source,
        { "Code" },
        {
            {
                "All",
                each
                    let
                        Source = _,
                        SortByIndex = Table.Sort ( Source, { { "Index", Order.Ascending } } ),
                        RemovedOtherColumns = Table.SelectColumns (
                            SortByIndex,
                            { "Activity Change %", "Activity" }
                        ),
                        ToRows = Table.ToRows ( RemovedOtherColumns ),
                        Transform = List.Accumulate (
                            ToRows,
                            {},
                            ( s, c ) =>
                                let
                                    a = if c{1} = null then List.Last ( s ) else c{1},
                                    b = s & { a + ( c{0} * a ) }
                                in
                                    b
                        ),
                        JoinColumns = Table.FromColumns (
                            Table.ToColumns ( Source ) & { Transform },
                            Table.ColumnNames ( Source ) & { "Forecast" }
                        )
                    in
                        JoinColumns
            }
        }
    )[All],
    Combine = Table.Combine ( Group ),
    ChangedType = Table.TransformColumnTypes ( Combine, { { "Forecast", Currency.Type } } )
in
    ChangedType

 

AntrikshSharma_0-1739171577164.png

PBIX is attached below.

Can we used DAX instead of PQ? I use DAX table to build the data table which mean I cannot use PQ. here my lates Calculated Column.

 

Value2 =

Var init =
MAXX(
    filter(
        Sheet1,
        Sheet1[Date] = DATE(2025,1,2)
    ),
    [Activity] + Sheet1[Value]
)

Var o =
PRODUCTX(
    Filter(
        Sheet1,
        Sheet1[Date]  <= EARLIER(Sheet1[Date])
    ),
    (IF(
        Sheet1[Date] = MINX(Sheet1, Sheet1[Date]),
        init,
        1 + Sheet1[Value]
    ) * Sheet1[Percetage])
)

return o
 
but the result return (F2+F2*[Value])*[Percentage]. I expecting to get (F2+[Value])*[Percentage] actualy.
regnig_0-1739249232144.png

 

Thank you.

@regnig no, while iteration, temporarily computed values of the previous rows aren't available in DAX and the calculation depends on the previous row during current row. You can import the table create in DAX by using PQ and then use the M code.

Anonymous
Not applicable

Hi @AdamPBIDev,

AFAIK, current power bi data model tables do not include row and column index AND DAX expression does not support doing recursive calculations.

Previous Value (“Recursion”) in DAX - Microsoft Power BI Community

You can enter to query editor to do these calculations but it will be poor performance on recursive calculations.

Recursive Functions in Power BI / Power Query — The Power User
Regards,

Xiaoxin Sheng

amitchandak
Super User
Super User

@AdamPBIDev , Try a new column,

 

Value = MAXX(filter(Data, [Index] =0 && [Code] =EARLIER(Data[Code])),[Activity])* PRODUCTX(Filter(DATA, [Code] =EARLIER(Data[Code]) && [Index]  <=EARLIER(Data[Index])), 1+[Activity Change %])

 

File attached after signature

 

 

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

Hi, @amitchandak 

I'm facing similar problem with recursive.

lets say i have an additional column called Cons. Also the formula is described bellow.

F2=C2*(D2 + Cons)

F3=D2*(F2 + Cons)

I've been struggling for that formula. would you like to give some advice. Thank you

Helpful resources

Announcements
July PBI25 Carousel

Power BI Monthly Update - July 2025

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

Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 community update carousel

Fabric Community Update - June 2025

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