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

Compete to become Power BI Data Viz World Champion! First round ends August 18th. Get started.

Reply
AnthonyJoseph
Resolver III
Resolver III

show top 5 based on category

Hello Community,

 

I have been assigned a task to filter the top 5 category items based on a sum of values. I have two tables (Table Year and Table detail) as mentioned below.

 

I need to show a column bar chart that display top 5 categories (irrespective of the attribute) for each year. Is this something that's possible in DAX?

 

I tried implementing it but ended by with blanks between bars in the visual. Is there anyway we can not include those blank bars in visuals

 

Table - Detail

Template noAttributeCategoryValue
1VehicleBike100
1VehicleScooter200
1VehicleHeavy vehicle140
1Vehiclebicycle240
1Vehiclecar50
1Non-vechileRocket20
1Non-vechileHelicopter30
1Non-vechileAirplane45
1Non-vechileAircraft56
1Non-vechileJet70
2VehicleBike10
2VehicleScooter20
2VehicleHeavy vehicle30
2Vehiclebicycle10
2Vehiclecar50
2Non-vechileRocket20
2Non-vechileHelicopter67
2Non-vechileAirplane87
2Non-vechileAircraft23
2Non-vechileJet49
3VehicleBike46
3VehicleScooter23
3VehicleHeavy vehicle45
3Vehiclebicycle67
3Vehiclecar12
3Non-vechileRocket98
3Non-vechileHelicopter76
3Non-vechileAirplane34
3Non-vechileAircraft22
3Non-vechileJet18

 

Table Year:

Template noYear
12019
22020
32021
1 ACCEPTED SOLUTION
Whitewater100
Solution Sage
Solution Sage

Hi:

The chart you want is designed to follow the order of the first year, then from there I'm not sure it appropriates space. Notice position 1 & 2 - bycycle then bike. then Heavy Bikes. It's a bad chart to use(my opinion) as the other options more clearly show what is going on between the categories and years. 2020 is definitely lower than 2021 but it's hard to tell with the chart you select. The two charts to the right appear to show both individual category and grand total differences. I wish I knew why the spacing is a bit off.

Whitewater100_0-1650574426774.png

 

 

View solution in original post

14 REPLIES 14
v-yalanwu-msft
Community Support
Community Support

Hi, @AnthonyJoseph ;

Based on my research, I'm afraid it couldn't achieve in Power BI for now.

Clustered column chart aligns with each labels and group by legends, it will keep the space of blank items and not support auto fit them. I'd like to suggest you post this requirement to ideas.

You can do conditional formatting for value using a measure and using field value option.

 

https://community.powerbi.com/t5/Desktop/Hiding-blanks-in-Clustered-Column-Chart/td-p/1935415


Best Regards,
Community Support Team _ Yalan Wu
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Whitewater100
Solution Sage
Solution Sage

Hi:

The chart you want is designed to follow the order of the first year, then from there I'm not sure it appropriates space. Notice position 1 & 2 - bycycle then bike. then Heavy Bikes. It's a bad chart to use(my opinion) as the other options more clearly show what is going on between the categories and years. 2020 is definitely lower than 2021 but it's hard to tell with the chart you select. The two charts to the right appear to show both individual category and grand total differences. I wish I knew why the spacing is a bit off.

Whitewater100_0-1650574426774.png

 

 

Whitewater100
Solution Sage
Solution Sage

Hi:

Sorry wrong link:

https://drive.google.com/file/d/1gJdZ6ToMoPuGY2qItJFeFLHdttcMQ4q_/view?usp=sharing 

 

Top 5 by Year = GENERATE( VALUES(
'Year'[Year]),
TOPN (
5,
ADDCOLUMNS (
VALUES ( Category[Category] ),
"Sales Amount", [Sales Amount]
),
[Sales Amount]
)
)

Whitewater100_0-1650565882753.png

 

 

Whitewater100
Solution Sage
Solution Sage

Hi:

Do you have dates associated with the dtail table?

 

Thanks..

Hi Anthony:

I hope this is what you need. Link here:

https://drive.google.com/file/d/1gJdZ6ToMoPuGY2qItJFeFLHdttcMQ4q_/view?usp=sharing 

Whitewater100_0-1650565202832.png

 

We can use Generate & Values to narrow it down:

Top 5 by Year = GENERATE( VALUES(
'Year'[Year]),
TOPN (
5,
ADDCOLUMNS (
VALUES ( 'Table'[Category] ),
"Sales Amount", [Sales Amount]
),
[Sales Amount]
)
)

THanks @Whitewater100 . Appreciate your help. However, I think I m still facing the same issue. I can still see the gaps between bars in the x-axis. From my Dax solution I did achieved the same result and I m trying to get rid of the gaps between the bars. For example: 2019 has a gap before and after the pink bar. Similary for 2020 and 2021 there is at least one empty bar  which I want to get rid off... 

 

AnthonyJoseph_0-1650566711313.png

P.S. I do also understand the reason for those white gaps but dont know how to solve it 🙂

Hi:

Can you use my file? It all works fine , here are several ways to plot below. You can change your X-Axis to Categorical and it looks a little better, but this chart is not the best for comparing values and is designed to keep the categories or continuous axis in a uniform way across all three years. You can see 2019 and 2021 look same for the first two categories becasue they are in same order. 2020 is not. In this case it's probably better to use a different visual like below. It's a design thing. These other visuaIs handle the type of order issues you are facing on this one chart. I have tested a bunch of format things and that chart is designed like it is behaving.I hope you mark this as solved.

 

Whitewater100_1-1650567534210.png

 

 

No .... No dates table is associated or involved in this data model

tackytechtom
Super User
Super User

Hi @AnthonyJoseph ,

 

Another way could have been to use the filter pane like this:

tomfox_3-1650400838737.png

 

Hope this helps! 🙂

 

/Tom
https://www.tackytech.blog/
https://www.instagram.com/tackytechtom/



Did I answer your question➡️ Please, mark my post as a solution ✔️

Also happily accepting Kudos 🙂

Feel free to connect with me on LinkedIn! linkedIn

#proudtobeasuperuser 

tackytechtom
Super User
Super User

Hi @AnthonyJoseph ,

 

I have a solution here:

tomfox_1-1650400529435.png

 

tomfox_2-1650400605552.png

 

Here the DAX:

Top5Measure = 
VAR _rank =
    RANKX(
        ALLSELECTED ( Table ),
        CALCULATE ( 
            [ValueSumMeasure], 
            MAX (Table[emplate no] ) = Table[emplate no]
        ), 
        , DESC
        , DENSE 
    )

VAR Top5 =
    IF ( _rank <= 5, _rank, BLANK () )
RETURN
    Top5

 

Note, the ValueSumMeasure is simply the SUM over ther Value column.

 

Let me know if this solves your issue 🙂

 

/Tom
https://www.tackytech.blog/
https://www.instagram.com/tackytechtom/



Did I answer your question➡️ Please, mark my post as a solution ✔️

Also happily accepting Kudos 🙂

Feel free to connect with me on LinkedIn! linkedIn

#proudtobeasuperuser 

Hello Community,

 

I have been assigned a task to filter the top 5 category items based on a sum of values. I have two tables (Table Year and Table detail) as mentioned below.

 

I need to show a column bar chart grouped by rank (maximum values for all years is the highest rank, the second maximum values will be rank# 2,etc)

 

I need to display rank in x-axis with the "Values" in y-axis and "Year" as legend. Any help on how to achieve this is highly appreciated.

 

Table - Detail

Template noAttributeCategoryValue
1VehicleBike100
1VehicleScooter200
1VehicleHeavy vehicle140
1Vehiclebicycle240
1Vehiclecar50
1Non-vechileRocket20
1Non-vechileHelicopter30
1Non-vechileAirplane45
1Non-vechileAircraft56
1Non-vechileJet70
2VehicleBike10
2VehicleScooter20
2VehicleHeavy vehicle30
2Vehiclebicycle10
2Vehiclecar50
2Non-vechileRocket20
2Non-vechileHelicopter67
2Non-vechileAirplane87
2Non-vechileAircraft23
2Non-vechileJet49
3VehicleBike46
3VehicleScooter23
3VehicleHeavy vehicle45
3Vehiclebicycle67
3Vehiclecar12
3Non-vechileRocket98
3Non-vechileHelicopter76
3Non-vechileAirplane34
3Non-vechileAircraft22
3Non-vechileJet18

 

Table Year:

Template noYear
12019
22020
32021

@AnthonyJoseph 


Is this a duplicated post? I answered the question in the 1st post here: https://community.powerbi.com/t5/DAX-Commands-and-Tips/show-top-5-based-on-category/m-p/2464378#M669...


Did I answer your question? Mark my post as a solution! and hit thumbs up


Subscribe and learn Power BI from these videos

Website LinkedIn PBI User Group

Thanks @tackytechtom The DAX gives me the rank of the top 5 items however, when I plot it to a graph stacked bar chart I get blanks in between the bars for the items that are not in a year. Example Bike is  in top 5 of the 2019 but when plotted to graph year 2020 has blank value in bar. Similarly Car is showing has blank in 2019 since we dont have car in the 2019 in top 5 values but its there in top 5 as 2020.

Fowmy
Super User
Super User

@AnthonyJoseph 
If you tried the small multiples option in your charts, it won't allow you to hide the categories with blank values, The other option is to show it in a matrix as follows. You can also search for a custom visual if one exists.

Top 5 Categories = 
CALCULATE(
    [Sum of Value],
    KEEPFILTERS( TOPN( 5 , ALLSELECTED( Table1[Category] ) , [Sum of Value] , DESC ) )
)

Fowmy_0-1650399349668.png


The file is attached below my signature 

 








Did I answer your question? Mark my post as a solution! and hit thumbs up


Subscribe and learn Power BI from these videos

Website LinkedIn PBI User Group

Helpful resources

Announcements
August Power BI Update Carousel

Power BI Monthly Update - August 2025

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

August 2025 community update carousel

Fabric Community Update - August 2025

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