Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
User7664
Helper I
Helper I

Calculating 15, 10, 5, 2 week average when your week id looks like 202001 and 202020

Hi All

I need to calculate the 15, 10, 5, 2 and other weekly averages from a dataset i take from a database. The database formats the week ID as 202001, 202002, to 202052. It deals with the 53rd week in its on unique way but we never receive a 202053 for example.

 

Ideally i want to do this without the need for another lookup table but I've not had much luck. Ive attached a sample file which may more clearly illustrate how the data is received. I appreciate that in this example and indeed any actual dataset the first 15 weeks of a 14 week average woud be blank.

 

Sample file in the dropbox folder.

2 REPLIES 2
amitchandak
Super User
Super User

@User7664 , These things work best with a separate date/week table. Create a table with year week and join it with main table back.

 

Have rank column there

Week Rank = RANKX(all('Date'),'Date'[Year Week],,ASC,Dense) //YYYYWW format

 

then you can work with measure like

example

Last 2 weeks = CALCULATE(sum('order'[Qty]), FILTER(ALL('Date'),'Date'[Week Rank]>=max('Date'[Week Rank])-2 && 'Date'[Week Rank]<=max('Date'[Week Rank])))

 

Last 5 weeks = CALCULATE(sum('order'[Qty]), FILTER(ALL('Date'),'Date'[Week Rank]>=max('Date'[Week Rank])-5 && 'Date'[Week Rank]<=max('Date'[Week Rank])))

 

You can make it work with your table, but the moment you need more week then select all will create a lot of overhead

 

Power BI — Week on Week and WTD
https://medium.com/@amitchandak.1978/power-bi-wtd-questions-time-intelligence-4-5-98c30fab69d3
https://community.powerbi.com/t5/Community-Blog/Week-Is-Not-So-Weak-WTD-Last-WTD-and-This-Week-vs-La...
https://www.youtube.com/watch?v=pnAesWxYgJ8

 

Thanks @amitchandak , the issue is that using a date table is that there should be 53 weeks in most years, with the 53rd and 1st week having in most cases less than 7 day each, the combined is 7 in total. The database always moves the 53rd week into the 1st week of the following month to ensure that the first week always have 7 days. 

 

Creating a date table and using the ISO week means that it would expect a 53rd, my dataset will never had a 53rd week. 

 

I must take the weekly consolidated view from the database as each day has over 1m rows, taking the weekly consolidation keeps my output under the 150m row limit i have for retieving set by the db. 

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.