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
karthikeyan9395
Frequent Visitor

Rolling average for last 3 custom months

Hi all,

I am struck up with calculating the rolling average for previous 3 custom months. Here we follow 13 month Fiscal calendar. So it is hard for me to achieve the moving average concept in Power BI measure.

 

This is my aggregarated table 'Sheet1'.

karthikeyan9395_3-1604388036461.png

 

Expected output needs to be like this in a table visual. 

karthikeyan9395_1-1604387939201.png

 

For month 3 it is calculated as follows: (32.48+52.99+91.94)/3 = 59.14

And for month 4 it is calculated as: (52.99+91.94+75.95)/3 = 73.63

 

Can anyone help me to solve this? 

1 ACCEPTED SOLUTION
karthikeyan9395
Frequent Visitor

Thanks @amitchandak. I developed the solution to the above query myself.

 

Here's the measure I developed without using any dates in the moving average concept.

 

moving_average_3months =
DIVIDE(IF(max(Sheet1[yearmonthorder]) > 2,
CALCULATE(sum(Sheet1[sales]),
FILTER(ALLSELECTED(Sheet1[yearmonthorder]),
Sheet1[yearmonthorder] <= max(Sheet1[yearmonthorder]) &&
Sheet1[yearmonthorder] >= max(Sheet1[yearmonthorder]) - 2)
),
BLANK()),3,BLANK())
 
Moving Average.png

View solution in original post

3 REPLIES 3
karthikeyan9395
Frequent Visitor

Thanks @amitchandak. I developed the solution to the above query myself.

 

Here's the measure I developed without using any dates in the moving average concept.

 

moving_average_3months =
DIVIDE(IF(max(Sheet1[yearmonthorder]) > 2,
CALCULATE(sum(Sheet1[sales]),
FILTER(ALLSELECTED(Sheet1[yearmonthorder]),
Sheet1[yearmonthorder] <= max(Sheet1[yearmonthorder]) &&
Sheet1[yearmonthorder] >= max(Sheet1[yearmonthorder]) - 2)
),
BLANK()),3,BLANK())
 
Moving Average.png
amitchandak
Super User
Super User

@karthikeyan9395 , Try like with 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])))))

 

Or

if( CALCULATE(distinctCOUNT('Date'[Month Year]),DATESINPERIOD('Date'[Date],MAX('Date'[Date]),-12,MONTH), not(isblank((Sales[Sales])))) < 3, blank() ,

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

 

Can you share sample data and sample output in table format? Or a sample pbix after removing sensitive data.

 

Please provide your feedback comments and advice for new videos
Tutorial Series Dax Vs SQL Direct Query PBI Tips
Appreciate your Kudos.

Hi @amitchandak ,

Thanks for your quick response. I am not able to use the dates in my measure because the dates are not constant for all the JCs sometimes it would be 28days in a month or 29days in a month. Hence I have created a custom column "yearmonthorder" which I used in measure to negate the 2 months. 

 

Here's the measure I used and it was not working.

 

Measure =
var acquistionmonth = SELECTEDVALUE(Sheet1[yearmonthorder])
var temp3 = AVERAGEX(CALCULATETABLE(Sheet1,FILTER(Sheet1,Sheet1[yearmonthorder] > acquistionmonth - 2)),Sheet1[sales])
return temp3
 
I have attached a Sample PBI-Moving Average.pbix  file for your reference. 
And thanks for helping me on this. 

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.