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

Get certified in Microsoft Fabric—for free! For a limited time, the Microsoft Fabric Community team will be offering free DP-600 exam vouchers. Prepare now

Reply
pan
Frequent Visitor

IF plus Rolling Average?

Hi!

 

I have current (2017) annual data that I have to compare against a 3 year rolling average for volume by product. However, for some products, there isn't always annual data available, i.e., some products have data for 2016 but not for 2015, etc. 

 

For ex:

2017, Product A, Country A, Volume

2017, Product B, Country A, Volume

2017, Product C, Country A, Volume

2016, Product A, Country A, Volume

2016, Product B, Country A, Volume

2015, Product A, Country A, Volume

2014, Product A, Country A, Volume

2014, Product C, Country A, Volume

 

In such case, only product A should have a rolling average which I will then compare with current year volume.

 

PS. Not pressing but can the same process be extended per country as well?

 

Can somebody help? I'm fairly new to Power BI. Thanks!

1 ACCEPTED SOLUTION
v-yuezhe-msft
Microsoft Employee
Microsoft Employee

@pan,

What DAX formula do you use to calculate rolling average? I make a test using the following DAX to calculate rolling average, the rolling average measure returns value even some products have no data for 2015. And when you create visuals using different legend level(Product or County) with this measure, the measure returns expected values.

{Base value} rolling average = 
IF(
 ISFILTERED({Date}),
 ERROR("Time intelligence quick measures can only be grouped or filtered by the Power BI-provided date hierarchy."),
 VAR __LAST_DATE =
  LASTDATE({Date}.[Date])
 RETURN
  AVERAGEX(
   DATESBETWEEN(
    {Date}.[Date],
    DATEADD(__LAST_DATE, {Periods Before}, {Period}),
    DATEADD(__LAST_DATE, {Periods After}, {Period}),
   ),
   CALCULATE({Base value})
  )
)

1.JPG


Reference:
https://community.powerbi.com/t5/Quick-Measures-Gallery/Rolling-Average/m-p/160720

Regards,
Lydia

Community Support Team _ Lydia Zhang
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

1 REPLY 1
v-yuezhe-msft
Microsoft Employee
Microsoft Employee

@pan,

What DAX formula do you use to calculate rolling average? I make a test using the following DAX to calculate rolling average, the rolling average measure returns value even some products have no data for 2015. And when you create visuals using different legend level(Product or County) with this measure, the measure returns expected values.

{Base value} rolling average = 
IF(
 ISFILTERED({Date}),
 ERROR("Time intelligence quick measures can only be grouped or filtered by the Power BI-provided date hierarchy."),
 VAR __LAST_DATE =
  LASTDATE({Date}.[Date])
 RETURN
  AVERAGEX(
   DATESBETWEEN(
    {Date}.[Date],
    DATEADD(__LAST_DATE, {Periods Before}, {Period}),
    DATEADD(__LAST_DATE, {Periods After}, {Period}),
   ),
   CALCULATE({Base value})
  )
)

1.JPG


Reference:
https://community.powerbi.com/t5/Quick-Measures-Gallery/Rolling-Average/m-p/160720

Regards,
Lydia

Community Support Team _ Lydia Zhang
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Early Bird pricing ends December 9th.

Nov PBI Update Carousel

Power BI Monthly Update - November 2024

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

October NL Carousel

Fabric Community Update - October 2024

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