Forum Discussion
Rolling Average Grouped by Week
Hey,
I am working with two tables, related on the below Date1 column. I want to ultimately get a rolling 7 day average (represented as a line) on the Line and Stacked Column. This line needs to be grouped by week (ideally the graph would show the starting date of the week represented). I am able to get a rolling average if I don't try to group each date into a week (see below screenshot) but I can't quite seem to get a 7 day rolling average and also have the bars grouped by week.
Data Table:
| ID | Zip Code | Date1 | Date2 | Age | Race | Ethnicity | Sex | Status | In | |||||||||||||||||||||||||||||||||||||||
| 1 | 28027 | 3/11/2020 | 3/11/2020 | 39 | White | Not Hispanic or Latino | Male | RD | ||||||||||||||||||||||||||||||||||||||||
| 3 | 28025 | 3/11/2020 | 3/16/2020 | 47 | Unknown | Not Hispanic or Latino | Female | RD | ||||||||||||||||||||||||||||||||||||||||
| 4 | 28027 | 3/13/2020 | 3/18/2020 | 48 | White | Not Hispanic or Latino | Female | RD |
Date Table:
|
Hi, Anonymous
Based on your description, I created data to reproduce your scenario. The pbix file is attached in the end.
Table:
Calendar(a calculated table):
Calendar = CALENDAR(DATE(2019,1,1),DATE(2020,12,31))You may create calculated columns and a measure as below.
Calculated column: Weeknum = WEEKNUM('Calendar'[Date]) Startofweek = CALCULATE( MAX('Calendar'[Date]), FILTER( ALL('Calendar'), 'Calendar'[Date]<=EARLIER('Calendar'[Date])&& WEEKDAY('Calendar'[Date])=1 ) ) Measure: Result = var _result = CALCULATE( AVERAGE('Table'[Value]), FILTER( ALL('Calendar'), 'Calendar'[Date]>=MIN('Calendar'[Date])-7&& 'Calendar'[Date]<MIN('Calendar'[Date]) ) ) return IF( ISBLANK(_result), AVERAGE('Table'[Value]), _result )Result:
Best Regards
Allan
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
3 Replies
- amitchandakSuper User
Anonymous , not very clear with your data. I hope you have created a Date table with the week.
And have a rolling formula like
Rolling 7 day = CALCULATE(sum(Sales[Sales Amount]),DATESINPERIOD('Date'[Date],MAX(Sales[Sales Date]),-7,Day))
For Week refer :https://community.powerbi.com/t5/Community-Blog/Week-Is-Not-So-Weak-WTD-Last-WTD-and-This-Week-vs-Last-Week/ba-p/1051123
- v-alq-msftCommunity Support
Hi, Anonymous
Based on your description, I created data to reproduce your scenario. The pbix file is attached in the end.
Table:
Calendar(a calculated table):
Calendar = CALENDAR(DATE(2019,1,1),DATE(2020,12,31))You may create calculated columns and a measure as below.
Calculated column: Weeknum = WEEKNUM('Calendar'[Date]) Startofweek = CALCULATE( MAX('Calendar'[Date]), FILTER( ALL('Calendar'), 'Calendar'[Date]<=EARLIER('Calendar'[Date])&& WEEKDAY('Calendar'[Date])=1 ) ) Measure: Result = var _result = CALCULATE( AVERAGE('Table'[Value]), FILTER( ALL('Calendar'), 'Calendar'[Date]>=MIN('Calendar'[Date])-7&& 'Calendar'[Date]<MIN('Calendar'[Date]) ) ) return IF( ISBLANK(_result), AVERAGE('Table'[Value]), _result )Result:
Best Regards
Allan
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
- v-alq-msftCommunity Support
Hi, Anonymous
If you take the answer of someone, please mark it as the solution to help the other members who have same problems find it more quickly. If not, let me know and I'll try to help you further. Thanks.
Best Regards
Allan
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.