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
seanmcc
Helper I
Helper I

Average of Top N values

Hi all,

 

I am looking for some help creating a measure to calculate the average value of top 5 per player which need to be filtered.

I have created a measure to calculate a player's max speed but needs to be filtered;

Max Speed = CALCULATE(MAX('GPS Data'[Maximum Velocity (m/s)]), 'GPS Data'[Date] > DATE(2017,07,03), 'GPS Data'[Matchday (+/-)] <> "MD" && 'GPS Data'[Matchday (+/-)] <> "PSF" && 'GPS Data'[Matchday (+/-)] <> "Off Season")
 
I now need to use this filtering to assess the player's top 5 'Maximum Velocity (m/s)' values and average them.
This is the measure I have attempted;
CALCULATE(AVERAGE('GPS Data'[Maximum Velocity (m/s)]),TOPN(5,'GPS Data','GPS Data'[Name],DESC), Dates[Date] > DATE(2017,07,03), 'GPS Data'[Matchday (+/-)] <> "MD" && 'GPS Data'[Matchday (+/-)] <> "PSF" && 'GPS Data'[Matchday (+/-)] <> "Off Season")
 
Below is a sample of the maximum velocity this player acheived, sorted highest to lowest.
My desired outcome is the average of the top 5 = 10.04
 
 
Session IDMaximum Velocity (m/s)Average of top 5
07/11/2018Player 110.3310.33
23/08/2018Player 110.3310.33
11/09/2018Player 19.999.99
07/08/2018Player 19.89.8
15/08/2017Player 19.759.75
20/11/2018Player 19.759.75

 

Thanks in advance

Sean

1 ACCEPTED SOLUTION
v-kelly-msft
Community Support
Community Support

Hi @seanmcc

 

I created a sample table and get the top 4 as an example,see below:

Annotation 2020-05-11 141249.png

First go to query editor ,add an index column;

Then create 2 measures as below:

 

Maximum Velocity (m/s) = 
var a = SUMX(GROUPBY('Table','Table'[Player]),RANKX(ALLSELECTED('Table'),CALCULATE(SUM('Table'[Speed])),,DESC,Dense))
return
IF(a<=4,MAX('Table'[Speed]),0)
Average of top 4 = AVERAGEX(FILTER(ALL('Table'),'Table'[Player]=MAX('Table'[Player])&&'Table'[Maximum Velocity (m/s)]<>0),'Table'[Maximum Velocity (m/s)])

 

And you will see:

Annotation 2020-05-11 141618.png

For the related .pbix file,pls click here.

 

 
Best Regards,
Kelly
Did I answer your question? Mark my post as a solution!

View solution in original post

3 REPLIES 3
v-kelly-msft
Community Support
Community Support

Hi @seanmcc

 

I created a sample table and get the top 4 as an example,see below:

Annotation 2020-05-11 141249.png

First go to query editor ,add an index column;

Then create 2 measures as below:

 

Maximum Velocity (m/s) = 
var a = SUMX(GROUPBY('Table','Table'[Player]),RANKX(ALLSELECTED('Table'),CALCULATE(SUM('Table'[Speed])),,DESC,Dense))
return
IF(a<=4,MAX('Table'[Speed]),0)
Average of top 4 = AVERAGEX(FILTER(ALL('Table'),'Table'[Player]=MAX('Table'[Player])&&'Table'[Maximum Velocity (m/s)]<>0),'Table'[Maximum Velocity (m/s)])

 

And you will see:

Annotation 2020-05-11 141618.png

For the related .pbix file,pls click here.

 

 
Best Regards,
Kelly
Did I answer your question? Mark my post as a solution!

Hi Kelly,

Thank you for posting this.  When I applied it in my setting I noticed that when it is calculating the maximum velocities, it is not considering that two max velocities that are the same should be counted as two of the four max.  In your example, the avg of top 4 should be (10.33+10.33+9.99+9.8)/4=10.11. However it appears to be disregarding the second 10.33 and calculating the avg as (10.33+9.99+9.8+9.75)/4=9.99.

How would you modify the measures to calculate the average that include duplicated maximums?

Thanks

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.