cancel
Showing results for
Did you mean:

Find everything you need to get certified on Fabric—skills challenges, live sessions, exam prep, role guidance, and more. Get started

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:
1 ACCEPTED SOLUTION
Helper IV

@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 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``````
Helper IV

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

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

Helper IV

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

@JWick1969 , Can you accept the appropriate solution?

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

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.

Helper IV

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

Helper IV

Hi @amitchandak , the FG% is  a measure.

Announcements

#### Europe’s largest Microsoft Fabric Community Conference

Join the community in Stockholm for expert Microsoft Fabric learning including a very exciting keynote from Arun Ulag, Corporate Vice President, Azure Data.

#### Power BI Monthly Update - June 2024

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

#### Fabric Community Update - June 2024

Get the latest Fabric updates from Build 2024, key Skills Challenge voucher deadlines, top blogs, forum posts, and product ideas.

Top Solution Authors
Top Kudoed Authors