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

Don't miss out! 2025 Microsoft Fabric Community Conference, March 31 - April 2, Las Vegas, Nevada. Use code MSCUST for a $150 discount. Prices go up February 11th. Register now.

Reply
mzs1988
Frequent Visitor

Moving Averages - DAX and Power BI

Hi , 

 

I'm trying to implement a moving average in Dax.  I saw multiple solution and they are working fine.  However I do not understand why the below solution does not work. Espacially that it seems pretty intiuitve. 

 

The idea is simple.  I have a table that contains all my data 'Query1'  and a calander table 'Calendar Relative' and the relationship is propery defined. Where the Date in Calender is dt in Query1.  

 

In my measure I use Summarize to return all the items ( ATM devices ) that I would like to calculate the running average for and I return all the dates they where operational in.  Next I use AddColumns and calculate a 3 days running average for each Device and Date it was operational In.  I use DatesInPeriod to do this. 

 

Below is my code : 

 

define 
var moving_average = 
ADDCOLUMNS(  SUMMARIZE(Query1   ,'Query1'[device_id] , Query1[dt] ),
"movingAverage" , calculate(average(Query1[completion_time] ) , DATESINPERIOD('Calendar Relative'[Date],Query1[dt] , -3 , DAY) ) , 
"min date" , calculate(min(Query1[dt] ) , DATESINPERIOD('Calendar Relative'[Date],Query1[dt] , -3 , DAY) ),
"max date " , calculate(max(Query1[dt] ) , DATESINPERIOD('Calendar Relative'[Date],Query1[dt] , -3 , DAY) ),
"count trans " , calculate(count(Query1[completion_time]) , DATESINPERIOD('Calendar Relative'[Date],Query1[dt] , -3 , DAY) ))  
    
//evaluate Average_pre_period
//evaluate Average_in_period
//evaluate row("diff" , dif ) 
EVALUATE moving_average

 

 

According to my understanding this should calculate the moving average. But what it is currently doing is that is calculates a single day average.  In other word the DatesInPeriod is always returning a single day instead of current date -3 period. 

 

 

Can anyone help to explain why this is happening ? 

thanks 

1 ACCEPTED SOLUTION
amitchandak
Super User
Super User

@mzs1988 , Try a meausre like

 

calculate(averageX(Values('Query1'[device_id]) , calculate(Sum(Query1[completion_time] ))) , DATESINPERIOD('Calendar Relative'[Date],max('Calendar Relative'[Date]), -3 , DAY))

 

Make sure Calendar Relative is marked as date table

Full Power BI Video 20 Hours YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

View solution in original post

3 REPLIES 3
mzs1988
Frequent Visitor

thank you @amitchandak for your fast response. 

 

The measure you gave me shows a single day average. I tried putting in a summarize funcation or a table visuals with all possible machines and dates and gives the same result. This is similar to what I'm seeing in the original code I shared in the post.  

amitchandak
Super User
Super User

@mzs1988 , Try a meausre like

 

calculate(averageX(Values('Query1'[device_id]) , calculate(Sum(Query1[completion_time] ))) , DATESINPERIOD('Calendar Relative'[Date],max('Calendar Relative'[Date]), -3 , DAY))

 

Make sure Calendar Relative is marked as date table

Full Power BI Video 20 Hours YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

Hello Again. 

 

I turns out that your Measure does work.  The only thing that I had to do was to replace the Date from the data calender with the the actual date in the table , so the measure becomes as follows : 

 

moving = 
 var maxdate = max(Query1[dt])
 return calculate(averageX(Query1,[completion_time] ) , DATESINPERIOD(Query1[dt],maxdate, -7 , DAY))

 

Which is strange may I say, I did not expect that to work. 

 

On a side note , Once I do this change the summarize function that i posted orignally also works 

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! Prices go up Feb. 11th.

Jan25PBI_Carousel

Power BI Monthly Update - January 2025

Check out the January 2025 Power BI update to learn about new features in Reporting, Modeling, and Data Connectivity.

Jan NL Carousel

Fabric Community Update - January 2025

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