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

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.

Reply
JWick1969
Helper IV
Helper IV

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:

AvgPrv3Months =
var avprev3mos = if(ISBLANK([FG %]),BLANK(),IF(COUNTROWS(DATESBETWEEN(DateTable[Date],EDATE(MIN(DateTable[Date]),-3),MIN(DateTable[Date])-1))>61,CALCULATE(AVERAGEX(values(DateTable[Date]),[FG %]),DATESBETWEEN(DateTable[Date],EDATE(MIN(DateTable[Date]),-3),MIN(DateTable[Date])-1)),BLANK()))
return avprev3mos
 
Incorrect Value:
Sample2.jpg
1 ACCEPTED SOLUTION

@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))

View solution in original post

9 REPLIES 9
Anonymous
Not 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

 Thank you for the reply. May i know if this will work with filter?

amitchandak
Super User
Super User

@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.

CALCULATE (
AVERAGEX ( VALUES ( DateTable[MonthYear] ), [FG %] ),
DATESINPERIOD ( DateTable[Date], ENDOFMONTH(DATEADD(DateTable[Date],-1, MONTH )), -3,MONTH))

@JWick1969 , Can you accept the appropriate solution? 

amitchandak
Super User
Super User

@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 %]))))

amitchandak
Super User
Super User

@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.

 

Average of previous 3 months =

CALCULATE (
AVERAGEX ( VALUES ( DateTable[MonthYear] ), [FG %] ),
DATESINPERIOD ( DateTable[Date], MAX ( DateTable[Date] ), -3, MONTH )
)
 
Sample3.jpg

Hi @amitchandak , the FG% is  a measure.  

 

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

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

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.

Top Solution Authors