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

The Power BI DataViz World Championships are on! With four chances to enter, you could win a spot in the LIVE Grand Finale in Las Vegas. Show off your skills.

Reply
egrospe17
Frequent Visitor

8-Week moving Average not working

Hello PBI Experts,

I've been scrambling my brains on trying to figure out what is wrong with my DAX moving average formula. Basically, my dataset consists of a date (start of week) and Paid invoiced amount. I'm using the following DAX formula to calculate the 8-week moving average: 

 

8W_Moving_Avg = 
VAR CurrentDate = MAX('New Calendar'[Date])
VAR StartDate = CurrentDate - 56 -- 12 weeks * 7 days

RETURN
    CALCULATE(
        AVERAGEX(
            SUMMARIZE(
                FILTER(
                    ALL('New Calendar'),
                    'New Calendar'[Date] >= StartDate &&
                    'New Calendar'[Date] <= CurrentDate
                ),
                'New Calendar'[Week from Start],
                "TotalValue", CALCULATE(SUM('WeeklyInvoicePaid'[InvoicePaidUSD]))
            ),
            [TotalValue]
        )
    )

 

However when I try to validate the numbers in excel, it looks like the DAX formula is incorrect. What am i doing wrong? 

 

egrospe17_0-1739486722457.png

 

 

1 ACCEPTED SOLUTION
OwenAuger
Super User
Super User

Hi @egrospe17 

It appears that the 8W_Moving_Avg measure is returning a 9-week moving average since it applies a date filter covering a range of 57 days, which extends into a 9th week.

 

I would suggest rewriting below this using DATESINPERIOD. The 3rd argument of DATESINPERIOD specifies the number of intervals (days in this case) beginning from the date provided as the 2nd argument (StartDate). Negative values for NumberOfIntervals produce periods extending backwards in time starting from StartDate.

 

Also, I'm assuming that 'New Calendar' is marked as a date table with the Date column being 'New Calendar'[Date], so you don't need to include  ALL ( 'New Calendar' ) or REMOVEFILTERS ( 'New Calendar' ).

 

8W_Moving_Avg =
VAR CurrentDate =
    MAX ( 'New Calendar'[Date] )
VAR NumDays = 56 -- 8 weeks
RETURN
    CALCULATE (
        AVERAGEX (
            VALUES ( 'New Calendar'[Week from Start] ),
            CALCULATE ( SUM ( WeeklyInvoicePaid[InvoicePaidUSD] ) )
        ),
        DATESINPERIOD ( 'New Calendar'[Date], CurrentDate, - NumDays, DAY )
    )

 

Alternatively, you could change 56 to 55 in your original measure, but this suggested measure should be more efficient as it filters the Date column rather than the 'New Calendar' table (see here). Also, SUMMARIZE shouldn't be used to add columns in general (see here).

 

Does this work for you?


Owen Auger
Did I answer your question? Mark my post as a solution!
Blog
Twitter
LinkedIn

View solution in original post

2 REPLIES 2
OwenAuger
Super User
Super User

Hi @egrospe17 

It appears that the 8W_Moving_Avg measure is returning a 9-week moving average since it applies a date filter covering a range of 57 days, which extends into a 9th week.

 

I would suggest rewriting below this using DATESINPERIOD. The 3rd argument of DATESINPERIOD specifies the number of intervals (days in this case) beginning from the date provided as the 2nd argument (StartDate). Negative values for NumberOfIntervals produce periods extending backwards in time starting from StartDate.

 

Also, I'm assuming that 'New Calendar' is marked as a date table with the Date column being 'New Calendar'[Date], so you don't need to include  ALL ( 'New Calendar' ) or REMOVEFILTERS ( 'New Calendar' ).

 

8W_Moving_Avg =
VAR CurrentDate =
    MAX ( 'New Calendar'[Date] )
VAR NumDays = 56 -- 8 weeks
RETURN
    CALCULATE (
        AVERAGEX (
            VALUES ( 'New Calendar'[Week from Start] ),
            CALCULATE ( SUM ( WeeklyInvoicePaid[InvoicePaidUSD] ) )
        ),
        DATESINPERIOD ( 'New Calendar'[Date], CurrentDate, - NumDays, DAY )
    )

 

Alternatively, you could change 56 to 55 in your original measure, but this suggested measure should be more efficient as it filters the Date column rather than the 'New Calendar' table (see here). Also, SUMMARIZE shouldn't be used to add columns in general (see here).

 

Does this work for you?


Owen Auger
Did I answer your question? Mark my post as a solution!
Blog
Twitter
LinkedIn

Thank SO MUCH! This solved the issue.

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!

FebPBI_Carousel

Power BI Monthly Update - February 2025

Check out the February 2025 Power BI update to learn about new features.

Feb2025 NL Carousel

Fabric Community Update - February 2025

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