Forum Discussion

l6903b5's avatar
l6903b5
Frequent Visitor
3 years ago

Rolling average per category with date table

Hi, 

 

I have a simple PM dataset containing product category, date and score (1-10). 

ProductDate (YYY-MM-DD)ITV Score
BS2023-01-279.0
PS2023-01-208.2
LP2023-01-197.0
VO2023-01-177.5
PN2023-01-257.8
etc  

Products belong to product group B2B or Retail. 

 

I have a date table based on the PM dates: zzCalendar = CALENDAR(MIN(PM[DATE]);MAX(PM[DATE])).

I have a measurement 

ITV rolling avg =

var _ITV = AVERAGEX(values(zzProduct[PRODUCT]); CALCULATE([ITV];DATESINPERIOD(zzCalendar[Date];max(zzCalendar[Date]);-365;DAY)))
return
if (HASONEVALUE(zzProduct[PRODUCT]);_ITV; AVERAGEX(values(zzProduct[PRODUCT]); ROUND(_ITV;2)))
 
The measurement calculates the average score for each product and for each aggregation (Product group and Total) it calculates the average of the products (rounded on 2 decimals). 
 
Problem with this measurement is that max(zzCalendar[Date]) or lastdate(zzCalendar[Date]) returns the lastdate for all product categories. But the product categories have different lastdates. For each product category the daterange  for the calculation is different. 
 
So I tried a different approach:

ITV rolling avg =

var _ITV = AVERAGEX(values(zzProduct[PRODUCT]); CALCULATE([ITV];DATESINPERIOD(zzCalendar[Date];max(PM[Date]);-365;DAY)))
return
if (HASONEVALUE(zzProduct[PRODUCT]);_ITV; AVERAGEx(values(zzProduct[PRODUCT]); ROUND(_ITV;2)))
 
This formula returns the rights rolling averages, but when I put the rolling average in a line chart to see the trend it gives unwanted results. The rolling average drops sharply and the number of measurements drops from around 400 to 9.  It has got to do with the fact that in december last year there were no measurements for B2B.

 If I know how I would have inserted a dataset and a pbix, but I hope somebody can help?

No RepliesBe the first to reply