Forum Discussion
SOLVED.DAX Measure previous 3 months average
Hi Everyone,
I'm having an issue on my DAX measure code that getting the average of previous 3 months. presently i'm getting incorrect result in my Visualization. The Correct values For April, the avg value of (Jan-Mar) should be 84.14%, May, the avg value (Feb-Apr) should be 85.45%. for Nov, the avg value of (Aug-Oct) is 84.99%. I'm using a Date Table. May I Know what is the problem with my DAX Code? Is there any other approach to get the average 3 mos.? any help is very much appreciated. Thank you.
DAX Code:
- Anonymous5 years ago
amitchandak Thank you very much. I modified the code and its alerady working.
CALCULATE (AVERAGEX ( VALUES ( DateTable[MonthYear] ), [FG %] ),DATESINPERIOD ( DateTable[Date], ENDOFMONTH(DATEADD(DateTable[Date],-1, MONTH )), -3,MONTH))
9 Replies
- amitchandak
Super User
Anonymous , Try like this example
Rolling 3 till last month = CALCULATE(sum(Sales[Sales Amount]),DATESINPERIOD('Date'[Date],ENDOFMONTH(dateadd(Sales[Sales Date],-1,month)),-3,MONTH))
- AnonymousNot applicable
amitchandak Thank you very much. I modified the code and its alerady working.
CALCULATE (AVERAGEX ( VALUES ( DateTable[MonthYear] ), [FG %] ),DATESINPERIOD ( DateTable[Date], ENDOFMONTH(DATEADD(DateTable[Date],-1, MONTH )), -3,MONTH))- amitchandak
Super User
Anonymous , Can you accept the appropriate solution?
- amitchandak
Super User
Anonymous , try like the given example. Prefer a date table
Rolling 3 = divide( CALCULATE(sum(Sales[Sales]),DATESINPERIOD('Date'[Date ],MAX('Date'[Date]),-12,MONTH)) ,
CALCULATE(distinctCOUNT('Date'[Month Year]),DATESINPERIOD('Date'[Date],MAX('Date'[Date]),-12,MONTH), not(isblank((Sales[Sales])))))Dividing by no of months. Also using a column from the fact table to make sure it does not count month from date table without data.
To get the best of the time intelligence function. Make sure you have a date calendar and it has been marked as the date in model view. Also, join it with the date column of your fact/s. Refer :radacad sqlbi My Video Series Appreciate your Kudos.
- AnonymousNot applicable
Hi amitchandak , the FG% is a measure.
- AnonymousNot applicable
I tried this code and its working. the problem is the current month was included plus the previous 2 month. How can i exclude the current month and extend the calculation to previous 3 month.
Average of previous 3 months =CALCULATE (AVERAGEX ( VALUES ( DateTable[MonthYear] ), [FG %] ),DATESINPERIOD ( DateTable[Date], MAX ( DateTable[Date] ), -3, MONTH ))
- amitchandak
Super User
Anonymous , Try like
AvgPrv3Months =
divide( CALCULATE([FG %],DATESINPERIOD('DateTable'[Date ],MAX('DateTable'[Date]),-12,MONTH)) ,
CALCULATE(distinctCOUNT('DateTable'[Month Year]),DATESINPERIOD('DateTable'[Date],MAX('DateTable'[Date]),-12,MONTH), not(isblank([FG %])))) - AnonymousNot applicable
[3M Avg] = // __monthCount tells you how many months // you want to use to get the average; // you can change it to any number you want; // to get a 6M average you just change to 6 var __monthCount = 3 var __anchorDate = MAX( DateTable[Date] ) var __periodToAvgOver = DATESINPERIOD( DateTable[Date], EOMONTH( __anchorDate, -1 ), (-1) * __monthCount, MONTH ) var __canCalculate = // check if there are enough months // to go back; works only if DateTable // is marked as a Date table in the model CALCULATE( DISTINCTCOUNT( DateTable[MonthYear] ) = __monthCount, __periodToAvgOver ) var __output = if( __canCalculate, CALCULATE( AVERAGEX( VALUES( DateTable[MonthYear] ), [FG %] ), __periodToAvgOver ) ) return __output- AnonymousNot applicable
Thank you for the reply. May i know if this will work with filter?