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.
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:
Solved! Go to Solution.
@amitchandak Thank you very much. I modified the code and its alerady working.
[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
Thank you for the reply. May i know if this will work with filter?
@JWick1969 , 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))
@amitchandak Thank you very much. I modified the code and its alerady working.
@JWick1969 , Can you accept the appropriate solution?
@JWick1969 , 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 %]))))
@JWick1969 , 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.
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.
Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City
Check out the April 2024 Power BI update to learn about new features.
User | Count |
---|---|
47 | |
24 | |
20 | |
15 | |
13 |
User | Count |
---|---|
51 | |
46 | |
39 | |
19 | |
19 |