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
Anonymous
Not applicable

26 & 52 Week Averages on data set with no dates and multiple criteria

Hello Everyone,

 

I've been working on this for a while now to no avail. I looked up solutions online and came back empty handed.

 

I have a data set similar to the below table but much larger. The dataset represents weekly metrics for multiple clients going back a few years. Problem is, the data has no datetime - it's split by week and year fields from the source.

 

My goal is to get the 26-week & 52-week averages for each client as of today for Cost, Quanty and everything else that's measurable in the table. Then the output would be represented in a separate matrix per client like so:

Client          26-week-avgQuantity          26-week-avgCost          52-week-avgQuantity          52-week-avgCost ....

Client 1                  xx                                             xx                                  xx                                          xx

Client 2                  xx                                             xx                                  xx                                          xx

 

Since i couldn't add a datetime filter (there are no date fields). I added rowCnt for each client in the source database (SQL) where the number of rowCnt represents the week number starting from the earliest date for the client in the master table.

 

I tried to create a measure that calculates the average of the last 26 weeks for each client but that didn't work. Then i figured I can create a separate calculated table with the total averages per client but couldn't make that work either.

 

The SQL equivilent of what I'm looking for would be:

SELECT average(TotalCost) $$,

            average(ClaimsQuantity) Qty,

            database_name

FROM ...

WHERE rowCnt >= (max(rowCnt)-26)

GROUP BY database_name

 

Any help is appreciated!

 

YearWeekTotalCostClaimsQuantitymemberswithclaimsrundatedatabase_namerowCnt
20181 $790,229.93                     7,334                                   87911/18/2019Client 1105
20182 $760,093.71                     3,589                                   83311/18/2019Client 1106
20183 $274,556.92                     7,201                                   29011/18/2019Client 1107
20184 $ 80,066.32                     6,201                                   53511/18/2019Client 1108
20185 $515,801.28                     7,299                                   62011/18/2019Client 1109
20184 $621,371.65                     1,554                                   27311/18/2019Client 26
20181 $382,295.08                     9,957                                   28911/18/2019Client 320
20182 $271,687.48                     1,561                                   37411/18/2019Client 321
20183 $720,261.23                     8,254                                   62911/18/2019Client 322
20184 $211,872.72                     5,660                                   63111/18/2019Client 323
20191 $ 34,853.87                     4,923                                   11311/18/2019Client 1158
20192 $   4,627.83                     2,835                                   98511/18/2019Client 1159
20193 $990,425.66                     8,638                                   81411/18/2019Client 1160
20194 $ 9,293.55                     3,453                                   19811/18/2019Client 1161
20195 $ 28,963.96                     4,206                                   99811/18/2019Client 1162
20191 $938,712.19                     8,224                                   68711/18/2019Client 251
20192 $500,311.44                     1,414                                   74811/18/2019Client 252
20193 $ 28,972.37                     1,211                                     6911/18/2019Client 253
20194 $896,399.80                     4,516                                   37611/18/2019Client 254
20195 $751,658.80                     1,303                                   13011/18/2019Client 255
20191 $857,307.93                     4,571                                     2311/18/2019Client 372
20192 $188,620.86                     2,093                                   43111/18/2019Client 373
20193 $266,024.07                     3,376                                   47411/18/2019Client 374
20194 $905,514.33                     9,574                                     2211/18/2019Client 375
20195 $527,883.33                     7,540                                   90511/18/2019Client 376
1 ACCEPTED SOLUTION
Anonymous
Not applicable

Solved, hope this helps someone:

 

26-wk-avg Cost = AVERAGEX(
TOPN(26,
FILTER(
ALL(KeymetricsWeekly[rowCnt]),
KeymetricsWeekly[rowCnt] >= SELECTEDVALUE(MaxRowCntPerClient[Row - 26]) -- created separate table with max row count per client
),
KeymetricsWeekly[rowCnt], DESC),
CALCULATE(AVERAGE(KeymetricsWeekly[TotalCost]))

View solution in original post

3 REPLIES 3
Anonymous
Not applicable

Solved, hope this helps someone:

 

26-wk-avg Cost = AVERAGEX(
TOPN(26,
FILTER(
ALL(KeymetricsWeekly[rowCnt]),
KeymetricsWeekly[rowCnt] >= SELECTEDVALUE(MaxRowCntPerClient[Row - 26]) -- created separate table with max row count per client
),
KeymetricsWeekly[rowCnt], DESC),
CALCULATE(AVERAGE(KeymetricsWeekly[TotalCost]))
Greg_Deckler
Super User
Super User

You might be able to adapt Sequential to give you a sequential week number per customer. 

 

https://community.powerbi.com/t5/Quick-Measures-Gallery/Sequential/m-p/380231?search-action-id=93121...


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...
Anonymous
Not applicable

Hi Greg,

 

The cntRow field acts as a sequenctial per client where 1 = week 1 from the earliest year available.

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.