Forum Discussion
5-Day Moving Average
// Depending on the model you have...
// But let's assume that:
// You have a Dates table that's connected
// to your fact table. Say the fact table
// is Cases and stores the following columns:
// 1. Date (Dates joins on this field)
// 2. CityID
// 3. NewCaseCount
// 4. DeathCount
// There'll be other dimensions but they're
// not relevant. Of course, fact table should
// have all its columns hidden. Slicing only
// via dimensions is allowed.
[New Cases] = SUM( Cases[NewCaseCount] )
[New Case Avg (5D)] =
// change this to 7 to get a weekly
// moving avg
var __numOfDays = 5
var __lastVisibleDay = MAX( Dates[Date] )
var __daysToAvgOver =
DATESINPERIOD(
Dates[Date],
__lastVisibleDay,
-__numOfDays,
DAY
)
var __enoughDays =
COUNTROWS( __daysToAvgOver ) = __numOfDays
var __avg =
if( __enoughDays,
CALCULATE(
[New Cases],
// this will work OK if
// Dates is marked as a
// Date table in the model
__daysToAvgOver
)
)
return
__avg
[Deaths] = SUM( Cases[DeathCount] )
[Death Count Avg (5D)] =
// change this to 7 to get a weekly
// moving avg
var __numOfDays = 5
var __lastVisibleDay = MAX( Dates[Date] )
var __daysToAvgOver =
DATESINPERIOD(
Dates[Date],
__lastVisibleDay,
-__numOfDays,
DAY
)
var __enoughDays =
COUNTROWS( __daysToAvgOver ) = __numOfDays
var __avg =
if( __enoughDays,
CALCULATE(
[Deaths],
// this will work OK if
// Dates is marked as a
// Date table in the model
__daysToAvgOver
)
)
return
__avg
// By the way, in M you can do anything,
// even more than in DAX, so it's certain
// that the calculation you're talking about
// CAN BE done in M.- ripstaur6 years ago
Helper III
Thanks for this, and for the advice about M and DAX. I think I can adapt some of this to streamline my code. The one thing I don't see in this solution is a way to aggregate (or filter - not sure what the right word is in DAX/M) by FIPS Code, so I'm essentially getting the averages by county. I'm also not concerned over the number of days available...this record is long, and I don't show the first couple of weeks in most of my visualizations.
I am not much of a coder - I took some basic (not VB, basic) classes years ago and used to be able to write pretty good stuff in that, and for several years I wrote databases in DB3 and DB4, but I got away from that and have been mostly doing analysis using SPSS and Minitab until a few years ago, when I was on a job that where they were using Qlikview, so I got certified as a Qlikview developer. Then they shifted to Tableau, and I was fat, dumb and happy using Tableau. Now I am working with an organization that decided to shift from Tableau to Power BI "to save money." So now I am in this environment, and having to code things that were simpler in other packages, using two new and different languages, without any formal training in either one.
Last week my challenge with this dataset was to find daily differences. I was unable to find anything in M that would do it (although I do believe there has to be that functionality, and would rather have done it there, in "Edit Queries"). I did find some DAX code in one of the Power BI or Power Query forums. I adapted it, and then ended up just modifying and iterating it to get the solution I wrote above.