Forum Discussion
Rolling Average Zero Days
- Anonymous7 years ago
Couple things going on here:
- Need a dedicated date table to make use of the built-in time intelligence functions
- Built a quick one here, but you can see in PQ how I did that. It's using the dates from the fact table, so no need to update
- Load the FactTable and the new Date Table
- Need to set the Date table as a Date Table (Data View-->DimDate Table-->Model--> Calendar-->Mark As Date Table
Now we all that in place we can write our function:
7 Day Avg = /*Variables are defined once and "Stored", so have the first date of the fact table This ensures that we do not show an average before we have seven days This can also be used to make sure we dont into the future if our date table has more dates then the fact */ Var __StartDate= FIRSTDATE( ALL ( FactTable[Session Date]) ) Return if ( /* Here we are setting the initial boundry of when to start the average */ CALCULATE( LastDate( DimDate[Date]), DATEADD( DimDate[Date], -7, Day)) >= __StartDate, /* Here is where we are going to sub in 0 for blanks and then average that */ CALCULATE( //Calculate here since we are changing the context AVERAGEX( //avergex since we need to interate the data table DimDate, /* if Total Decl is blank, then we want a zero, else want the actual Total Decl*/ if( ISBLANK([Total Decl]),0, [Total Decl] //total decl is just SUM ( FactTable[Decl]) ) ),//close off AVERAGEX and then add the filter for calculate DATESINPERIOD( DimDate[Date], LASTDATE( DimDate[Date]), -7,DAY ) //Time Intelligence Function, which is why we need a Date Table ) )Ugh, that's hard to read. Here's a screenshot (and is in the file in the link below)
Then to build the visual, use the Date column from DimDate, add the two measures. But then besure to filter the visual on Total Decl is not blank, else you will get blanks since the 7 day average function is using those dates
File:
- Need a dedicated date table to make use of the built-in time intelligence functions
Couple things going on here:
- Need a dedicated date table to make use of the built-in time intelligence functions
- Built a quick one here, but you can see in PQ how I did that. It's using the dates from the fact table, so no need to update
- Load the FactTable and the new Date Table
- Need to set the Date table as a Date Table (Data View-->DimDate Table-->Model--> Calendar-->Mark As Date Table
Now we all that in place we can write our function:
7 Day Avg =
/*Variables are defined once and "Stored", so have the first date of the fact table
This ensures that we do not show an average before we have seven days
This can also be used to make sure we dont into the future if our date table has more dates then the fact */
Var __StartDate= FIRSTDATE( ALL ( FactTable[Session Date]) )
Return
if (
/* Here we are setting the initial boundry of when to start the average */
CALCULATE(
LastDate( DimDate[Date]), DATEADD( DimDate[Date], -7, Day)) >= __StartDate,
/* Here is where we are going to sub in 0 for blanks and then average that */
CALCULATE( //Calculate here since we are changing the context
AVERAGEX( //avergex since we need to interate the data table
DimDate,
/* if Total Decl is blank, then we want a zero, else want the actual Total Decl*/
if(
ISBLANK([Total Decl]),0, [Total Decl] //total decl is just SUM ( FactTable[Decl])
)
),//close off AVERAGEX and then add the filter for calculate
DATESINPERIOD( DimDate[Date], LASTDATE( DimDate[Date]), -7,DAY ) //Time Intelligence Function, which is why we need a Date Table
)
)Ugh, that's hard to read. Here's a screenshot (and is in the file in the link below)
Then to build the visual, use the Date column from DimDate, add the two measures. But then besure to filter the visual on Total Decl is not blank, else you will get blanks since the 7 day average function is using those dates
File:
Hi Anonymous I had a further look at creating a DimTable and figured out what you meant. Thank you very much this solution worked perfectly for me and the code you put up was very easy to implement.
Thank you again!!
- Anonymous7 years agoNot applicable
fajemile glad it worked out!