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

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes! Register now.

Reply
snph1777
Helper V
Helper V

Power BI - DAX language RANKX function for TOP 5 only

I have a Power BI table report as below.

 

Products (dimension column)ProductCount (measure)ProductRank (measure)
Fridge361
AC302
Fan283
Light254
Chair205
Bike126
Car57

 

I have developed this table report using a dimension column called Products, and two measures

called ProductCount and ProductRank.

 

The measures use COUNT and RANKX functions at the backend.

 

The ProductRank measure value is obvioulsy based on the ProductCount measure.

 

I am currently showing all the 7 different products.

 

Out client wants to show only the TOP 5 products, and show all other products as Rank 6, with the Product name 'Others'.

 

Products (dimension column)ProductCount (measure)ProductRank (measure)
Fridge361
AC302
Fan283
Light254
Chair205
Others176

 

There are some limitations I have in using Power BI. I am using the Power BI Service embedded in our client application, where I cannot create Calculated Columns or new TABLEs (with ADDCOLUMNS), unless I raise a ticket.  I can use Measure always.

 

Any suggestion you can provide ?

 

I am okay with any suggestion; it would be great if you can provide me a solution that uses only Measures. If not possible, I can take the option to create TABLEs or Calculated Columns.

 

7 REPLIES 7
mahoneypat
Microsoft Employee
Microsoft Employee

@jdbuchanan71  is right that you need to have Other in a column to do this right.  For fun, I came up with a way to hack itwith the measure below to get the result shown.  It is not exactly what you were looking for, but does convey the same info (but it does hijack the total value).

 

Top 5 and Other =
VAR top5products =
    TOPN (
        5,
        ALLSELECTED ( ProductCount[Products (dimension column)] ),
        [Product Count], DESC
    )
VAR top5sum =
    CALCULATE ( [Product Count], KEEPFILTERS ( top5products ) )
VAR others =
    CALCULATE (
        [Product Count],
        EXCEPT (
            ALLSELECTED ( ProductCount[Products (dimension column)] ),
            top5products
        )
    )
RETURN
    IF (
        HASONEVALUE ( ProductCount[Products (dimension column)] ),
        top5sum,
        "Others - " & others
    )

 

mahoneypat_0-1597960985031.png

If this works for you, please mark it as the solution.  Kudos are appreciated too.  Please let me know if not.

Regards,

Pat





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


Hello  @mahoneypat 

 

I am using this in a visual, so really don't need a total;  I am planning to show this in a pie chart.

 

Can i do this, showing the 'Others' as a 6th rank ?  Will I get 17 for Others?

This approach wouldn't work in a pie chart.  You will have to do one of the suggestions before to get an "Other" value in your products column.

 

Regards,

Pat

 





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


Thanks for the clarification. Will keep you posted.

@jdbuchanan71

 

Our client has finally agreed to create a calculated tablle.

 

Will update you in the next day or so.

 

Thanks very much. Appreciate your help so far.

 

Will keep you posted.

jdbuchanan71
Super User
Super User

@snph1777 

I don't know of any way to do it with only measures.  The reason being, you need somwhere that the value of 'Other' exists along with the list of products.  That is a simple calculated table to create with code like this:

Product List = 
UNION ( 
    DISTINCT ( 'Product'[Product Name] ),
    ROW("Product Name", "Other")
)

You join this new table into your existing product table and pull the product name from the new table for your report.

Then you can make a TopN measure that will show the top 5 and group the rest.

TopN = 
VAR Top_N = 
    CALCULATETABLE ( 'Product List', TOPN ( 5, ALLSELECTED ( 'Product List' ), [Sales Amount] ) )
RETURN
    IF ( 
        NOT ISFILTERED ( 'Product List'[Product Name] ), CALCULATE ( [Sales Amount], ALLSELECTED ( 'Product List' ) ),
        IF ( SELECTEDVALUE ( 'Product List'[Product Name] ) = "Other",
            CALCULATE ( [Sales Amount], EXCEPT ( ALLSELECTED ( 'Product List' ), Top_N ) ),
            CALCULATE ( [Sales Amount], INTERSECT ( 'Product List', Top_N ) )
        )
    )

In order to sort 'Other to the bottom of the list you would need a measure liks this and add it into your visual then set the column width narrow enough to hide it.  It is just there to sort.

TopN Sort = 
IF ( SELECTEDVALUE ( 'Product List'[Product Name] ) = "Other", 0, [TopN] )

jdbuchanan71_0-1597934598038.png

 

Thanks @jdbuchanan71

 

Will get back.

 

Are there any alternate solutions too ?

Helpful resources

Announcements
September Power BI Update Carousel

Power BI Monthly Update - September 2025

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

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Solution Authors
Top Kudoed Authors