Forum Discussion

AnthonyJoseph's avatar
AnthonyJoseph
Icon for Resolver III rankResolver III
4 years ago
Solved

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
  • 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.

     

     

14 Replies

  • 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 ) )
    )
    


    The file is attached below my signature 

     








  • tackytechtom's avatar
    tackytechtom
    Icon for Most Valuable Professional rankMost Valuable Professional

    Hi AnthonyJoseph ,

     

    I have a solution here:

     

     

    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/

    • AnthonyJoseph's avatar
      AnthonyJoseph
      Icon for Resolver III rankResolver III

      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's avatar
      AnthonyJoseph
      Icon for Resolver III rankResolver III

      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.

    • AnthonyJoseph's avatar
      AnthonyJoseph
      Icon for Resolver III rankResolver III

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

      • AnthonyJoseph's avatar
        AnthonyJoseph
        Icon for Resolver III rankResolver III

        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... 

         

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

  • 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.

     

     

  • v-yalanwu-msft's avatar
    v-yalanwu-msft
    Icon for Community Support rankCommunity 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.