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
sdgiss
Helper I
Helper I

Moving daily average with offset

Hello PBI Community!

 

I am trying to produce a 21-day moving average with a 7-day offset. The DAX formula below gets me close. In this instance the offset (7 days) is applied however the 21-day moving average only comes in at 15 days. If I make it -27 the output is then a 21-day moving average. I would prefer to have an accurate representation of the moving average interval. What am I overlooking?

 

Thank you all!

 

21-day moving avg with 7-day offset :=var sum_dist =

CALCULATE (
    [Total Dist],
    FILTER (
        ALL ( dDateSeason ),
        dDateSeason[Date]
            <= MAX ( dDateSeason[Date] ) - 7
            && dDateSeason[Date]
                >= MAX ( dDateSeason[Date] ) - 21
    )
)

 

var count_days_dist =

CALCULATE (
    DISTINCTCOUNT ( dDateSeason[Date] ),
    FILTER (
        ALL ( dDateSeason ),
        dDateSeason[Date]
            <= MAX ( dDateSeason[Date] ) - 7
            && dDateSeason[Date]
                >= MAX ( dDateSeason[Date] ) - 21
    )
)

 

return

DIVIDE(sum_dist,count_days_dist)

1 ACCEPTED SOLUTION
AllisonKennedy
Super User
Super User

@sdgiss If you want the average to span 21 days, then it will need to be 27 days ago to 7 days ago (since you are use = on both ends it is inclusive, otherwise you'd need to use 28). 

 

When you say 21 day moving average offset, what are you wanting to acheive? I think the 27 (or 28 without 😃 is what you're looking for?


Please @mention me in your reply if you want a response.

Copying DAX from this post? Click here for a hack to quickly replace it with your own table names

Has this post solved your problem? Please Accept as Solution so that others can find it quickly and to let the community know your problem has been solved.
If you found this post helpful, please give Kudos C

I work as a Microsoft trainer and consultant, specialising in Power BI and Power Query.
www.excelwithallison.com

View solution in original post

9 REPLIES 9
amitchandak
Super User
Super User

@sdgiss , Try like this , with help from a date table

Rolling 21 = CALCULATE(count(Sales[Serial Number]),DATESINPERIOD('Date'[Date ],MAX('Date'[Date ])-7,-21,DAY))

Hello @amitchandak!

 

The formula above yields the appopropriate output, but unfortnately it doesn't work as a measure inside the following. It is usable in other measures but I'm guessing the PREVIOUSDAY function doesn't like it.

 

=CALCULATE([Rollling 21-day avg],PREVIOUSDAY(dDateSeason[Date]))

 

This is the error I receive when attempting to load it a pivot table.


Screen Shot 2020-12-31 at 12.39.17 PM.png

Thank you for your help @amitchandak! I had thought of this as an alternative but got stuck on my formula above. When in doubt, I should always choose the path of least resistance. Thank you for pointing this out with your solution!

AllisonKennedy
Super User
Super User

@sdgiss If you want the average to span 21 days, then it will need to be 27 days ago to 7 days ago (since you are use = on both ends it is inclusive, otherwise you'd need to use 28). 

 

When you say 21 day moving average offset, what are you wanting to acheive? I think the 27 (or 28 without 😃 is what you're looking for?


Please @mention me in your reply if you want a response.

Copying DAX from this post? Click here for a hack to quickly replace it with your own table names

Has this post solved your problem? Please Accept as Solution so that others can find it quickly and to let the community know your problem has been solved.
If you found this post helpful, please give Kudos C

I work as a Microsoft trainer and consultant, specialising in Power BI and Power Query.
www.excelwithallison.com

Thank you so much for your reply @AllisonKennedy! It turns out I was making this much harder than it had to be! The solution from @amitchandak works like a charm. Many thanks again for offering up your assistance!

@sdgiss Glad you got what you're looking for. @amitchandak  solution should yield the same results as your solution - they are just two different ways to write/express the same calculation (depending on how you want to think about it). In your original one, you were defining the start and end dates. In Amit's he is defining the start date and how long the period should be.  

 

Love kudos if I helped. 


Please @mention me in your reply if you want a response.

Copying DAX from this post? Click here for a hack to quickly replace it with your own table names

Has this post solved your problem? Please Accept as Solution so that others can find it quickly and to let the community know your problem has been solved.
If you found this post helpful, please give Kudos C

I work as a Microsoft trainer and consultant, specialising in Power BI and Power Query.
www.excelwithallison.com

You were a great help @AllisonKennedy! You helped confirm that my first formula might be the one to move forward with. The one from @amitchandak is more straightforward and convenient but it doesn't work as a measure with time intelligence functions. If I can't find a way to resolve this issue then I will likely elevate your response to the solution.

 

Thank you, and Happy New Year!

@sdgiss  What do you mean when you say 'it doesn't work as a measure with time intelligence functions'. If you can add more detail on what's not working, we can hopefully help you resolve the issue. 🙂 Happy New Year to you too!


Please @mention me in your reply if you want a response.

Copying DAX from this post? Click here for a hack to quickly replace it with your own table names

Has this post solved your problem? Please Accept as Solution so that others can find it quickly and to let the community know your problem has been solved.
If you found this post helpful, please give Kudos C

I work as a Microsoft trainer and consultant, specialising in Power BI and Power Query.
www.excelwithallison.com

@AllisonKennedy my sincere apologies for not responding to this sooner. It has been a hectic few months. 

 

Did you see the error message when trying to use the suggestion from Amit? I have the measure in there as well. When I place the original equation (the one you validated for me) I have no issues with the measure output, however with Amit's suggestion it makes the measure unusable.

 

Thank you!

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.