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
JustineTennent
New Member

Quick Measure - 7 day rolling average excluding 0's

Hi All,

 

I've added a quick measure to calculate the 7 day rolling average of some data--- problem is where a record had a value of 0 it is excluded from the average therefore increasing the moving average value and inturn making it incorrect

 

ie.

 

20 1/1/18

30 2/1/18

30 3/1/18

0 4/1/18

40 5/1/18

40 6/1/18

40 7/118

50 8/1/18

 

= moving average value for the  4/1/18 should be 28.57 instead its 33 (only using 6 values instead?) 

How can i fix the measure to include the data that has 0's

 

 

GARI TOTAL rolling average =
IF(
ISFILTERED('GARI Summary MOD'[LOAD_DATE]),
ERROR("Time intelligence quick measures can only be grouped or filtered by the Power BI-provided date hierarchy or primary date column."),
VAR __LAST_DATE = LASTDATE('GARI Summary MOD'[LOAD_DATE].[Date])
RETURN
AVERAGEX(
DATESBETWEEN(
'GARI Summary MOD'[LOAD_DATE].[Date],
DATEADD(__LAST_DATE, -3, DAY),
DATEADD(__LAST_DATE, 3, DAY)
),
CALCULATE(SUM('GARI Summary MOD'[GARI TOTAL]))
)
)

 

 

 

 

7 REPLIES 7
Ashish_Mathur
Super User
Super User

Hi,

 

Here's what i would suggest:

 

  1. Create a Calendar Table with running dates from the first date till the last date that appear in the Date column of your source dataset.  You may create this Calendar Table by going to Modelling > New Table and writing the following formula = CALENDAR(MIN(Data[Date]),MAX(Data[Date]))
  2. Create a relationship between the Date column of the Data Table to the Date column of the Calendar Table
  3. In your visual, drag the Date column from the Calendar Table
  4. Write the followng measure

 

=CALCULATE(AVERAGE(Data[Value]),DATESBETWEEN(Calendar[Date],MIN(Calendar[Date])-7,MAX(Calendar[Date])))

 

Hope this helps.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

hat method does not work.  Rolling average are still based upon the days with entries when filtered thus inflating the value.  For example here is some data from a pbix I am working on.

 

14 Day Average = 
AVERAGEX(DATESINPERIOD(Dates[Date],LASTDATE(Dates[Date]),-14,DAY),[Total Sales])

 

Date range 8/13 - 8/31

And here are the results :

 

EquipmentCostCodeDateTotal Sales
BD110071003-28/31/2018 0:00$310
BD110071003-28/25/2018 0:00$310
BD110071003-28/18/2018 0:00$465

 

EquipmentSales MTD14 Day  Average
BD11007$1,086$362

 

The date table in this instance is linked to the sales table as you suggested, but this calculation still filters out days with no sales in the denominator.  The average should be  $1086 / 14 days = $76

but Dax is reporting  $1086 /  3 = $362

 

I wonder how many Power BI viz out there are incorrect because no one bothered to check the math?  

 

 

 

 

Anonymous
Not applicable

Hi @realdanielbyrne 

 

I was wondering if you had this issue solved, if so please share the solution.I am having the same problem in getting the moving average for the past 25 days.

 

kind regards,

HiltonM

Hi,

 

Share some data and show the expected result.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/
Phil_Seamark
Employee
Employee

Hi @JustineTennent

 

You could try this technique  I've highlighted the changes in red.

 

GARI TOTAL rolling average = 
IF(
ISFILTERED('GARI Summary MOD'[LOAD_DATE]),
ERROR("Time intelligence quick measures can only be grouped or filtered by the Power BI-provided date hierarchy or primary date column."),
VAR __LAST_DATE = LASTDATE('GARI Summary MOD'[LOAD_DATE].[Date])
RETURN
    SUMX(
        DATESBETWEEN(
            'GARI Summary MOD'[LOAD_DATE].[Date],
            DATEADD(__LAST_DATE, -3, DAY),
            DATEADD(__LAST_DATE, 3, DAY)
        ),
        CALCULATE(SUM('GARI Summary MOD'[GARI TOTAL]))
    ) /7
)

To learn more about DAX visit : aka.ms/practicalDAX

Proud to be a Datanaut!

Unfortunately your answer divides the original sample value by 7 rather than the sum of 7 days 😞

 

thanks for trying tho!

HI @JustineTennent

 

Looks like I had it too early in the formula.

 

Please try this.

 

GARI TOTAL rolling average = 
IF(
ISFILTERED('GARI Summary MOD'[LOAD_DATE]),
ERROR("Time intelligence quick measures can only be grouped or filtered by the Power BI-provided date hierarchy or primary date column."),
VAR __LAST_DATE = LASTDATE('GARI Summary MOD'[LOAD_DATE].[Date])
RETURN
    SUMX(
        DATESBETWEEN(
            'GARI Summary MOD'[LOAD_DATE].[Date],
            DATEADD(__LAST_DATE, -3, DAY),
            DATEADD(__LAST_DATE, 3, DAY)
        ),
        CALCULATE(SUM('GARI Summary MOD'[GARI TOTAL]))
    ) 
)/7

To learn more about DAX visit : aka.ms/practicalDAX

Proud to be a Datanaut!

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