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

Power BI is turning 10! Let’s celebrate together with dataviz contests, interactive sessions, and giveaways. Register now.

Reply
LuisNC
Helper I
Helper I

Top N with DAX

Hello Dear Community,

I've got a dataset with only one table:

LuisNC_0-1690217199126.png

Here's a few row's example:

LuisNC_1-1690217265672.png

 

Every song has a genre and a popularity. I just want to create a visual showing the top 5 genres based on their average popularity.

It shouldn't be too hard to do, but I can't make it 😞

I can make it adding filters in a bar chart, but I'd like to know how to do it with DAX:

LuisNC_2-1690217498056.png

Could you please give me a hand?

Thank you!

1 ACCEPTED SOLUTION
OwenAuger
Super User
Super User

Hi @LuisNC 

You can use a pattern like this:

1. First create an Average Popularity measure:

Average Popularity = 
AVERAGE ( spotify_tracks[popularity] )

2. Next create the measure Average Popularity for Top 5 Genres:

Average Popularity for Top 5 Genres =
VAR N = 5
VAR TopGenres =
    CALCULATETABLE (
        TOPN (
            N,
            VALUES ( spotify_tracks[genre] ),
            [Average Popularity]
        ),
        ALLSELECTED ()
    )
RETURN
    CALCULATE (
        [Average Popularity],
        KEEPFILTERS ( TopGenres )
    )

3. Place the measure Average Popularity for Top 5 Genres in the visual with genre on the axis.

 

Does this work for you?

 

Regards


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

View solution in original post

2 REPLIES 2
LuisNC
Helper I
Helper I

Thank you @OwenAuger , It's correct !!

OwenAuger
Super User
Super User

Hi @LuisNC 

You can use a pattern like this:

1. First create an Average Popularity measure:

Average Popularity = 
AVERAGE ( spotify_tracks[popularity] )

2. Next create the measure Average Popularity for Top 5 Genres:

Average Popularity for Top 5 Genres =
VAR N = 5
VAR TopGenres =
    CALCULATETABLE (
        TOPN (
            N,
            VALUES ( spotify_tracks[genre] ),
            [Average Popularity]
        ),
        ALLSELECTED ()
    )
RETURN
    CALCULATE (
        [Average Popularity],
        KEEPFILTERS ( TopGenres )
    )

3. Place the measure Average Popularity for Top 5 Genres in the visual with genre on the axis.

 

Does this work for you?

 

Regards


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

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

Check out the June 2025 Power BI update to learn about new features.

June 2025 community update carousel

Fabric Community Update - June 2025

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