Forum Discussion
Recursive Calculation and Forecast measure
- 3 years ago
Hi murillocosta
Such fully recursive problems cannot be handled by DAX straight forward. A helper table that suits your case shall be needed to handle the recursion. The solution is a bet difficult to understand and it should be carefully design to suit each case independently (cannot be generalized) nevertheless, what ever the chosen details of the solution, the general idea remains the same.You need to import the Fibonacci table as is and use the DAX as is. However, your real data might not be as I expected, therefore some changes might be required.
Please note that such reports should have limited flexibility in terms of the columns used to slice. Slicing by different column(s) might require changing the code. You need to understand that this is a limitation of the DAX language for the time being, hopping future updates will curry new features of the language such as loops that can help performing recursive calculations much more easier.
Also Please note that there is a rounding error with this method and the results shall not be 100% accurate.
Please refer to the sample file with the solutionForecast Count = VAR W = CALCULATE ( MAX ( 'Table'[Year Week Sort] ), REMOVEFILTERS ( ) ) -- with real data would be VAR LastDateWithData 'Table'[Date] and then VAR W = YEAR ( LastDateWithData ) * 100 + WEEKNUM ( LastDateWithData ) VAR CW = MAX ( 'Date'[Year Week Sort] ) VAR S4 = CALCULATE ( [CounRef], 'Date'[Year Week Sort] = W, ALLSELECTED ( ) ) VAR S3 = CALCULATE ( [CounRef], 'Date'[Year Week Sort] = W - 1, ALLSELECTED ( ) ) VAR S2 = CALCULATE ( [CounRef], 'Date'[Year Week Sort] = W - 2, ALLSELECTED ( ) ) VAR S1 = CALCULATE ( [CounRef], 'Date'[Year Week Sort] = W - 3, ALLSELECTED ( ) ) VAR SelectedWeeks = ALLSELECTED ( 'Date'[Year Week Sort] ) VAR WeeksOnAndBefore = FILTER ( SelectedWeeks, 'Date'[Year Week Sort] > W && 'Date'[Year Week Sort] <= CW ) VAR Ranking = COUNTROWS ( WeeksOnAndBefore ) RETURN SUMX ( FILTER ( FibonacciTable, FibonacciTable[Index] = Ranking ), FibonacciTable[Value1] * S1 + FibonacciTable[Value2] * S2 + FibonacciTable[Value3] * S3 + FibonacciTable[Value3] * S4 ) - 3 years ago
murillocosta
Here you go
Hi tamerj1, thanks for that.
I've been tryingo to get it done for hours but still couldn't figure out why on my scenary is not working.
Would you be able to check my file below and help me out?
https://easyupload.io/4r0p0e
murillocosta
Here you go
- murillocosta3 years agoHelper I
Hey tamerj1, just one more question. If I need to change the average from 4 to n.....how would you update the fibonacci table values????is there any place you can get it from?
- tamerj13 years agoCommunity Champion
Hi murillocosta
Attached your sample file updated with the dynamiuc solutionTotal Outbound Final 2 = VAR Periods = [Number of Periods Value] VAR TotalOutBound = [Total Outbound] VAR W = CALCULATE ( MAX ( 'SUMMARIZE'[Week Rank] ), 'SUMMARIZE'[Total Outbound] <> 0, REMOVEFILTERS ( ) ) VAR CW = MAX ( 'SUMMARIZE'[Week Rank] ) VAR SelectedWeeks = CALCULATETABLE ( VALUES ( 'SUMMARIZE'[Week Rank] ), ALLSELECTED ( ) ) VAR WeeksOnAndBefore = FILTER( SelectedWeeks, 'SUMMARIZE'[Week Rank] > W && 'SUMMARIZE'[Week Rank] <= CW ) VAR CurrentIndex = COUNTROWS ( WeeksOnAndBefore ) VAR T1 = SELECTCOLUMNS ( GENERATESERIES ( 1, Periods, 1 ), "@ValueInex", [Value] ) VAR T2 = ADDCOLUMNS ( T1, "@Value", VAR ValueIndex = [@ValueInex] RETURN CALCULATE ( [Total Outbound], 'SUMMARIZE'[Week Rank] = W + ValueIndex - Periods, ALLSELECTED ( ) ) ) VAR Result = SUMX ( T2, VAR CurrentValue = [@Value] VAR FibTable = FILTER ( Fibonacci_Table, Fibonacci_Table[Index] = CurrentIndex && Fibonacci_Table[Period Index] = Periods && Fibonacci_Table[Value Index] = [@ValueInex] ) VAR FibValue = MAXX ( FibTable, Fibonacci_Table[Value] ) RETURN CurrentValue * FibValue ) RETURN Result + TotalOutBound- vitorcampos2 years agoFrequent Visitor
Hello, tamerj1
I stumbled upon this post while searching for a solution similar to the one you provided. I need to calculate data based on previously calculated data using the same metric. Your solution is truly impressive! Thank you for sharing it.
Now, I'm working on adapting it to my needs, and I'm hopeful that I'll manage to make it work. Regarding the Fibonacci_Table, is that the only method available for performing such calculations?
- tamerj13 years agoCommunity Champion
Check my 2nd solution based on dynamic selection of the number of periods. I will apply over your sample file and share it with you. Probably not today as today I'm out of office the whole day
- tamerj13 years agoCommunity Champion
I have updated the solution, have you checked? Actually, you csnnot find this table anywhere else. This is something I have created out of a mathematical research and reasoning inspired by the Fibonacci numerical series. Perhaps you are the first one on earth to have a real business Power Bi report that performs real recursive calculations.
- aj19733 years agoCommunity Champion
- tamerj13 years agoCommunity Champion
Hi aj1973
The whole idea is complex. Even I don't remember all details. It is even more complex to explain. I was planning to leave an article about this subject in the forum but then I did not see much demand to such approach. Perhaps I'll think again. I'll keep you updated.
- aj19733 years agoCommunity Champion
Thank you very much, I should admit that I am really impressed.
Good work,