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

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
bonus1
Frequent Visitor

Recursive Measure Calculation Problem in Power BI

Hey guys,

 

I am struggling with the following problem.

 

I want to create a measure which calculates recursively the Value for ne current Month.

 

I just want to use the recursion formular:

 

X(Month) = X(Month-1) + Y(Month) - Z(Month)

 

For example for this table:

iterative.png

 

 

Means, the measure should start by February.

 

X(February) = X(January) + Y(February) - Z(February)

X(February) = 295 + 374 - 454

X(February) = 215

 

For March:

 

X(March) = X(February) + Y(March) - Z(March)

X(February) = 215 + 580 - 782

 

etc.

 

Can anyone help me please to solve this problem within Power BI?

 

Thank you very much in advance.

 

 

2 ACCEPTED SOLUTIONS
Stachu
Community Champion
Community Champion

to my knowledge there really is no recursive functionality in DAX
still you can achieve the similar result with iterator
try this code (calculated column)

Column = 
VAR CurrentMonth = 'Table'[Date]
VAR InitialValueDate = MIN('Table'[Date])
VAR TableYZ = FILTER('Table', 'Table'[Date]<=CurrentMonth && 'Table'[Date] > InitialValueDate)
RETURN
CALCULATE(SUM('Table'[X]),FILTER(ALL('Table'),'Table'[Date]<CurrentMonth))
+ SUMX(TableYZ,[Y]-[Z])


Did I answer your question? Mark my post as a solution!
Thank you for the kudos 🙂

View solution in original post

Hi @bonus1

Stachu's formula helps a lot.

You need to create a calculated column in Modeling->calculated column in Data Model view

2.png

Then write the formula to get [Column], then get [Column2] based on [Column], finally, get the measure as you like

My test 

Column = 
VAR CurrentMonth = Sheet1[date]
VAR InitialValueDate = MIN(Sheet1[date])
VAR TableYZ = FILTER(Sheet1,Sheet1[date]<=CurrentMonth && Sheet1[date] > InitialValueDate)
RETURN
CALCULATE(SUM(Sheet1[x]),FILTER(ALL(Sheet1),Sheet1[date]<CurrentMonth))
+ SUMX(TableYZ,[Y]-[Z])

Column 2 = IF([x]<>BLANK(),[x],[Column])

Measure = MAX([Column 2])

3.png

 

 

Best Regards

Maggie

 

View solution in original post

6 REPLIES 6
bonus1
Frequent Visitor

Hey @v-juanli-msft, @Stachu

 

I have a nother problem in this relation.

 

If the X column has in each row a value, the formular adds this value as well in X(Month-1). But that should not be the case, it should just takes the new calculated value in X(Month-1)

 

 

bonus1
Frequent Visitor

Does anyone have an idea?
bonus1
Frequent Visitor

Thank you very much guys.

 

It works!

 

 

Stachu
Community Champion
Community Champion

to my knowledge there really is no recursive functionality in DAX
still you can achieve the similar result with iterator
try this code (calculated column)

Column = 
VAR CurrentMonth = 'Table'[Date]
VAR InitialValueDate = MIN('Table'[Date])
VAR TableYZ = FILTER('Table', 'Table'[Date]<=CurrentMonth && 'Table'[Date] > InitialValueDate)
RETURN
CALCULATE(SUM('Table'[X]),FILTER(ALL('Table'),'Table'[Date]<CurrentMonth))
+ SUMX(TableYZ,[Y]-[Z])


Did I answer your question? Mark my post as a solution!
Thank you for the kudos 🙂

bonus1
Frequent Visitor

Thank you very much Stachu.

 

I tried this code, but I get the error message: Token Eof expected

Hi @bonus1

Stachu's formula helps a lot.

You need to create a calculated column in Modeling->calculated column in Data Model view

2.png

Then write the formula to get [Column], then get [Column2] based on [Column], finally, get the measure as you like

My test 

Column = 
VAR CurrentMonth = Sheet1[date]
VAR InitialValueDate = MIN(Sheet1[date])
VAR TableYZ = FILTER(Sheet1,Sheet1[date]<=CurrentMonth && Sheet1[date] > InitialValueDate)
RETURN
CALCULATE(SUM(Sheet1[x]),FILTER(ALL(Sheet1),Sheet1[date]<CurrentMonth))
+ SUMX(TableYZ,[Y]-[Z])

Column 2 = IF([x]<>BLANK(),[x],[Column])

Measure = MAX([Column 2])

3.png

 

 

Best Regards

Maggie

 

Helpful resources

Announcements
RTI Forums Carousel3

New forum boards available in Real-Time Intelligence.

Ask questions in Eventhouse and KQL, Eventstream, and Reflex.

MayPowerBICarousel

Power BI Monthly Update - May 2024

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

LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.