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

Don't miss out! 2025 Microsoft Fabric Community Conference, March 31 - April 2, Las Vegas, Nevada. Use code MSCUST for a $150 discount. Prices go up February 11th. Register now.

Reply
holywasabi
Frequent Visitor

Display value with highest average score

Hi all, newbie here. 😁

 

I want to show the "Wine Name" for the one that has the highest average score, and the 2nd highest average score. 

 

With the condition that the "Wine Name" has appeared in the dataset more than once.

 

Note : Average score is calculated using wine_fact[points]



wine-dashboard@4x.png

 


Link to pbix file : 
https://drive.google.com/drive/folders/1d_6_pT5TJtg5KpRC79plvzXfako83upA?usp=sharing

Appreciate any help, much thanks! 😊

1 ACCEPTED SOLUTION
OwenAuger
Super User
Super User

Hi @holywasabi 

These days, I would recommend using the INDEX function to return a value with a given rank where the ranking is based on multiple values. In this case, it appears that you need Wine Name to be ranked first based on [score (average)] then based on Wine Name itself (ascending lexicographic ordering).

 

Here is how you could write the measures. The only difference between them is the WineRank variable which should be set to the desired rank.

wine name (wine score (average) top-1) = 
VAR WineRank = 1 -- Change to desired rank
VAR WineScore =
    ADDCOLUMNS (
        FILTER (
            VALUES ( wine_fact[Wine Name] ),
            CALCULATE ( COUNTROWS ( wine_fact ) ) > 1
        ),
        "@Score", [score (average)]
    )
VAR WineResult =
    SELECTCOLUMNS (
        INDEX (
            WineRank,
            WineScore,
            -- For equal scores, ties are broken using Wine Name (ascending lexicographic ordering)
            ORDERBY ( [@Score], DESC, wine_fact[Wine Name], ASC )
        ),
        "@WineResult", wine_fact[Wine Name]
    )
RETURN
    WineResult
wine name (wine score (average) top-2) = 
VAR WineRank = 2 -- Change to desired rank
VAR WineScore =
    ADDCOLUMNS (
        FILTER (
            VALUES ( wine_fact[Wine Name] ),
            CALCULATE ( COUNTROWS ( wine_fact ) ) > 1
        ),
        "@Score", [score (average)]
    )
VAR WineResult =
    SELECTCOLUMNS (
        INDEX (
            WineRank,
            WineScore,
            -- For equal scores, ties are broken using Wine Name (ascending lexicographic ordering)
            ORDERBY ( [@Score], DESC, wine_fact[Wine Name], ASC )
        ),
        "@WineResult", wine_fact[Wine Name]
    )
RETURN
    WineResult

Does this work for you?


Owen Auger
Did I answer your question? Mark my post as a solution!
Blog
Twitter
LinkedIn

View solution in original post

4 REPLIES 4
v-xuxinyi-msft
Community Support
Community Support

Hi @holywasabi 

 

I wish you all the best. It seems to me that OwenAuger 's approach is the right one and gets you the desired results.

vxuxinyimsft_0-1737365159518.png

 

vxuxinyimsft_1-1737365269713.png

 

I would like to confirm whether you have successfully resolved this issue or if you need further assistance. If OwenAuger 's reply was helpful to you, could you consider marking it as a solution? This will help more users who are facing the same or similar difficulties. Thank you!

 

Best Regards,
Yulia Xu

Thank you for offering more assistance, Yulia 🙂

 

It works like a charm 

OwenAuger
Super User
Super User

Hi @holywasabi 

These days, I would recommend using the INDEX function to return a value with a given rank where the ranking is based on multiple values. In this case, it appears that you need Wine Name to be ranked first based on [score (average)] then based on Wine Name itself (ascending lexicographic ordering).

 

Here is how you could write the measures. The only difference between them is the WineRank variable which should be set to the desired rank.

wine name (wine score (average) top-1) = 
VAR WineRank = 1 -- Change to desired rank
VAR WineScore =
    ADDCOLUMNS (
        FILTER (
            VALUES ( wine_fact[Wine Name] ),
            CALCULATE ( COUNTROWS ( wine_fact ) ) > 1
        ),
        "@Score", [score (average)]
    )
VAR WineResult =
    SELECTCOLUMNS (
        INDEX (
            WineRank,
            WineScore,
            -- For equal scores, ties are broken using Wine Name (ascending lexicographic ordering)
            ORDERBY ( [@Score], DESC, wine_fact[Wine Name], ASC )
        ),
        "@WineResult", wine_fact[Wine Name]
    )
RETURN
    WineResult
wine name (wine score (average) top-2) = 
VAR WineRank = 2 -- Change to desired rank
VAR WineScore =
    ADDCOLUMNS (
        FILTER (
            VALUES ( wine_fact[Wine Name] ),
            CALCULATE ( COUNTROWS ( wine_fact ) ) > 1
        ),
        "@Score", [score (average)]
    )
VAR WineResult =
    SELECTCOLUMNS (
        INDEX (
            WineRank,
            WineScore,
            -- For equal scores, ties are broken using Wine Name (ascending lexicographic ordering)
            ORDERBY ( [@Score], DESC, wine_fact[Wine Name], ASC )
        ),
        "@WineResult", wine_fact[Wine Name]
    )
RETURN
    WineResult

Does this work for you?


Owen Auger
Did I answer your question? Mark my post as a solution!
Blog
Twitter
LinkedIn

Thank you so so much!

That is such an elegant solution, works as I had intended, and I learned something new. 😊

Thousand apologies for the late reply, was totally flooded with work (this is a side project). 😣

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Prices go up Feb. 11th.

Jan25PBI_Carousel

Power BI Monthly Update - January 2025

Check out the January 2025 Power BI update to learn about new features in Reporting, Modeling, and Data Connectivity.

Jan NL Carousel

Fabric Community Update - January 2025

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