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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
gluizqueiroz
Resolver I
Resolver I

How to create a column using DAX that is recursive and use the own column to calculate the next rows

I need to create the column "ValueCalculated" and the column "TotalValueCalculated" for the next example:

MonthYearValuePercentValueCalculatedTotalValueCalculated
jan/13150001,20015000
feb/1385000,8012023620
mar/13-25000,051221132
apr/130-0,03-621125
may/1300,388021206
jun/1300,112321229
jul/1300,357421303
aug/1300,439221395
sep/1300,7415821553
oct/1300,00021553
nov/1300,8117521728
dec/1300,398521813
jan/1400,102221834
feb/1401,2026222096
mar/1400,5211522211
apr/1400,5211522327
may/1400,143122358


I have created the previous table on Excel, using the following formulas:

 

ValueCalculated: PreviousLine of TotalValueCalculated * Percent

image.png

 

TotalValueCalculated: PreviousLine of TotalValueCalculated + Value + ValueCalculated

image.png

 

For the line 1 (jan/13), the Previous Row is 0, for the line 2 onwards use the Previous Line.

 

The values from MonthYear, Value and Percent I have on my database.

I started this 2 columns with this:

 

Table = 
VAR __table1 = ALL(Calendar[MonthYear]; Calendar[NumberMonthYear])
VAR __table2 = 
    ADDCOLUMNS(
        __table1; 
        "Value"; ROUND(CALCULATE([Values]; BI_Sales[ID] = 12345); 0);
        "Percent"; CALCULATE([Percent]; FILTER(BI_Percents; BI_Percents[NumberMonthYear] = [NumberMonthYear]))
    )
VAR __table3 = FILTER(__table2; Calendar[NumberMonthYear] >= MINX(FILTER(__table2; [Value] > 0); [NumberMonthYear]))
VAR __table4 = ADDCOLUMNS(__table3; "Index"; RANKX(__table3; [NumberMonthYear]; ; ASC))
RETURN
__table4

 

The previous DAX returns me this:

 

MonthYearNumberMonthYearValuePercentIndex
jan/13201301150001,201
feb/1320130285000,802
mar/13201303-25000,053
apr/132013040-0,034
may/1320130500,385
jun/1320130600,116
jul/1320130700,357
aug/1320130800,438
sep/1320130900,749
oct/1320131000,0010
nov/1320131100,8111
dec/1320131200,3912
jan/1420140100,1013
feb/1420140201,2014
mar/1420140300,5215
apr/1420140400,5216
may/1420140500,1417

 

Is there any way to create these two colums that I need? 
For you can try, you can select the previous table and paste it on Power BI and start to use the magical DAX functions. 
Just for remember, I canno't use Power Query functions to achieve this, just DAX functions.

4 REPLIES 4
Greg_Deckler
Super User
Super User

I'm not sure I am 100% on the requirements here. You can refer to earlier rows like this:

 

Column =

  VAR __Index = [Index]

RETURN

  SUMX(FILTER('Table',[Index] <= __Index),[Value])

 

You cannot do true recursion but sometimes you can sort of emulate it:

https://community.powerbi.com/t5/Quick-Measures-Gallery/Previous-Value-aka-quot-Recursion-quot/m-p/6...

https://community.powerbi.com/t5/Quick-Measures-Gallery/Runge-Kutta/m-p/411280#M149

 


@ 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!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

Hey @Greg_Deckler .

But, how can I reference the columns while it doesn't exist yet, for example:
For I create the column "ValueCalculated", I need to reference the previous line from "TotalValueCalculated", but, for I create the column "TotalValueCalculated", I need to use the column "ValueCalculated", right?
I cannot create the columns these 2 column in the same ADDCOLUMNS, because I cannot reference a column that is being created at the same time, I need to create one column first, but, I need reference eachother.
It's a little bit hard to explain.

I get it. The answer is "there is no true recursion in DAX"

 

Trust me, I have attacked this problem dozens and times and the answer is still the same.


@ 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!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

@gluizqueiroz - One thing I thought of. Power Query (M) does support recursion.  Here is an implementation of using recursion in Power Query:

 

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

 


@ 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!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

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

April Fabric Community Update

Fabric Community Update - April 2024

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