Hey guys!
In this post we will see how we can do a 4 weeks rolling average. In another post (Here) I have elaborated what a rolling average is and how it's easy to do it by Year, month and quarter. But since power bi doesn't have week level I will show you a workaround in this article.
Also the major challenge with using the quick measure for rolling average is that you can't use the data without the date hierarchy but with this approach we can use date in any format we need easily.
Problem Statement:
Get the 4 weeks rolling average of resolution time. (support date)
Sample data:
I prepared a sample data like this from 01-Jan-2020 to 07-april-2021 with =RandBetween(0.1,100)/57 function in column C to get some randome decimal values as resolution time
Solution:
While loading the data
1. I created an extra column
WeekStart=Date.StartOfWeek([Created Date],Day.Sunday)
This column results in the start date of the week for the created date. Change the type to Date in Transform->DataType: Date
2. Created another column with the following formula to get the start date 28 days (4 weeks ago)
4weeksagoWeekStart =Date.StartOfWeek(Date.AddDays([Created Date],-28),Day.Sunday)
Load the data.
After loading:
avgResolutionTime = AVERAGE(Sheet1[Resolution Time])
avgResolutionTime running total in WeekStart =CALCULATE([avgResolutionTime],FILTER(all(Sheet1),Sheet1[Created Date]>=max(Sheet1[4weeksagoWeekStart]) && Sheet1[Created Date]<max(Sheet1[WeekStart])))
In this example we consider last completed week for every week start. If you want to include the current week create a next week start while loading with adddays(Date,+7,days) so you get the start of next week. Also create 3 weeks ago start by using -21 instead of -28. And Change the above formula to Sheet1[Created Date]>=max(Sheet1[3weeksagoWeekStart]) && Sheet1[Created Date]<max(Sheet1[NExtWeekStart])
Explaining the above formula
Lets take an example to understand the formula. If we want the 4 weeks rolling average on 31st of May 2020 (Sunday) the rolling average must be for the dates between 03 May (sunday) to 30 May saturday.
Week 1 May 03- May 09
Week 2 May 10 to May 16
Week 3 May 17 to May 23
Week 4 May 24 to May 30
So against 31-May-2020 we need to have Avg(ResolutionTime) from May 03-May 30
For 31-May-2020
max(Sheet1[4weeksagoWeekStart]) is May 03
Screenshot of rolling average calculated with the above formula: Value =0.99
A thumbs up, if this works, motivates me to write more on this blog. Do follow to get notified when i post new tips and tricks.