cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Pdecis
Frequent Visitor

Recursive Measure PowerBI

Hi All,

 

I would like to calcul : Stock Month -1+ Entrées Prév - Sorties Prév for each row(row = month)

The Column "Sorties Prev" is a measure in link with few measures.
I add a measure : ForecastCalculation =
VAR PrevInv = CALCULATE(SUMX('Simulation StockDP (2)'; 'Simulation StockDP (2)'[Stock]); DATEADD(DateTable[Date]; -1; MONTH))
RETURN (PrevInv + SUMX('Simulation StockDP (2)'; [Entrées Prév - Sorties Prév]))
The line for February is good, but for March the resulat is note good because the stock egals 0.
For March, i would like to calculate : Stock February + Entrées Prev - Sorties Prév (6673+2576-2693)
For April, i would like to calculate : Stock March+ Entrées Prev - Sorties Prév (6556+1730-2474)

For May, ....

PBI_Community.png

 

Please , help me.

Thanks a lot.

5 REPLIES 5
Greg_Deckler
Super User
Super User

DAX does not generally like recursion. See the article:

 

https://community.powerbi.com/t5/Community-Blog/Runge-Kutta-and-the-Limits-of-DAX/ba-p/357501

 

However, I wrote a blog article recently that might help with your situation:

 

https://community.powerbi.com/t5/Community-Blog/For-and-While-Loops-in-DAX/ba-p/636314

 

Could you post sample data as text and expected output? Would help tremendously.


@ 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!:
Mastering Power BI 2nd Edition

DAX is easy, CALCULATE makes DAX hard...

I would like this :
Stock = 7316 (january)
Stock for February = Stock January (7316)+Entrées-Sorties
Stock for March = Stock February + Entrées + Sorties

In Power BI, Sorties ia a measure link with few measures.
....

 

 

 

PBI_Community2_EXCEL.png

 

 

 

 



Thanks

Hmm, well I gave this a lot of thought. Not sure I have what I need from a sample data perspective to give you a detailed solution, but I did come up with a way to emulate recursion in DAX that might work for your purposes. See here:

 

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

 


@ 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!:
Mastering Power BI 2nd Edition

DAX is easy, CALCULATE makes DAX hard...

While recursion is elegant from a coding point of view there are usually also non-recursive solutions for most problems.

 

I'm not sure if I understand your issue correctly, but it looks to me like you are trying to calculate stock on hand based on looking at the Incoming (Entrées) and Outgoing (Sortes) stock. If this is the case and your period is based off a Date column in your model you could do this by calculating a lifetime to date for Incoming and Outgoing and subtract one from the other.

 

eg

 

Stock = 
VAR _prevEntrées = SUMX( Filter('table1'[DateColumn], 'table1'[DateColumn] <= MAX('table1'[DateColumn]), [Entrées] )
VAR _prevSorties = SUMX( Filter('table1'[DateColumn], 'table1'[DateColumn] <= MAX('table1'[DateColumn]), [Sorties] )
RETURN _prevEntrées - _prevSorties

Hi,

My measure Forecast2  is good pour January 2019 but not good results for others month.
The measure "Sorties2" included several measures and What If Paramater (Tx de controle).




The result for the Measures "Sorties2" is good .

If i add "-Sorties2" in formula "Forecast2", the result is bad.

Why ?
Thanks for your help.

Pierre

Helpful resources

Announcements
collabdays lisbon

CollabDays Lisbon - 26 November 2022

Sessions include practical, hands-on experience that will help you take the next step in your career and know-how.

Difinity Conference

Difinity Conference 2022

Difinity is the largest Microsoft Data, AI, Power BI, Power Platform and Business Applications Conference in New Zealand.

European Share Point Conference

European Share Point Conference

This conference returns live and in-person in Copenhagen this 28 November-01 December with 4 Microsoft Keynotes, 9 Tutorials, and 120 Sessions.

Microsoft 365 Conference â__ December 6-8, 2022

Microsoft 365 Conference - 06-08 December

Join us in Las Vegas to experience community, incredible learning opportunities, and connections that will help grow skills, know-how, and more.