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
jbeckham812
Frequent Visitor

Average of Top N Values based on Ranking Index

Hi All,

 

I need a measure that calculates the average of the top 5 values in the "Departure to Received" column based on rank. Table below:

IDRanked IndexDeparture To Received
NH085                        500                 55
NH084                        499                 60
NH092                        498                 85
NH081                        497                 74
NH086                        496                 51
NH088                        495                 56
NH090                        494                 62

 

So in the table above, the average computed  would be 65 ((55+60+85+74+51)/5). Im a complete newbie to DAX, figured making the ranked table descending would help if TOPN was needed to be used in the measure, but could make the ranking ascending if that would make the solution easier.

2 ACCEPTED SOLUTIONS
amitchandak
Super User
Super User

@jbeckham812 , if Ranked Index is column, Create a new measure like

 


measure =
var _max = maxx(allselected(Table), Table[Ranked Index])
return
calculate(sum(Table[Departure To Received]), filter(Table, Table[Index] >= _max -5 && Table[Index] <=_max))

View solution in original post

v-luwang-msft
Community Support
Community Support

Hi @jbeckham812 ,

Whether you are creating a column, or a measure, you can use the following dax:

test = 
VAR test1 =
    MAX ( 'Table'[Ranked Index] )
VAR test2 =
    CALCULATE (
        AVERAGE ( 'Table'[Departure To Received] ),
        FILTER (
            'Table',
            'Table'[Ranked Index] <= test1
                && 'Table'[Ranked Index] >test1 - 5
        )
    )
RETURN
    test2

vluwangmsft_0-1630049856532.pngvluwangmsft_1-1630049869378.png

Don't forget to give thumbs up and accept this as a solution if it helped you!!!

 

Best Regards

Lucien

View solution in original post

2 REPLIES 2
v-luwang-msft
Community Support
Community Support

Hi @jbeckham812 ,

Whether you are creating a column, or a measure, you can use the following dax:

test = 
VAR test1 =
    MAX ( 'Table'[Ranked Index] )
VAR test2 =
    CALCULATE (
        AVERAGE ( 'Table'[Departure To Received] ),
        FILTER (
            'Table',
            'Table'[Ranked Index] <= test1
                && 'Table'[Ranked Index] >test1 - 5
        )
    )
RETURN
    test2

vluwangmsft_0-1630049856532.pngvluwangmsft_1-1630049869378.png

Don't forget to give thumbs up and accept this as a solution if it helped you!!!

 

Best Regards

Lucien

amitchandak
Super User
Super User

@jbeckham812 , if Ranked Index is column, Create a new measure like

 


measure =
var _max = maxx(allselected(Table), Table[Ranked Index])
return
calculate(sum(Table[Departure To Received]), filter(Table, Table[Index] >= _max -5 && Table[Index] <=_max))

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.