Power BI

How to Build a Dynamic Top-N Ranking Dashboard in Power BI Using RANKX and TOPN

How to Build a Dynamic Top-N Ranking Dashboard in Power BI Using RANKX and TOPN

Table of Contents

Introduction

In power BI you can easily find your top 5 customers or best-performing sales representatives using the Dynamic Top-N Analysis. Power BI comes with two helpful DAX functions called RANKX() and TOPN(), which allow you to create an interactive ranking dashboard. For a better understanding of these functions, you can go through our blog posts regarding  TOPN() Function in Power BI: Step-by-Step Guide to Calculating Top 10 Stores and How to Use RANKX() for Dynamic Rankings in Power BI.

This blog post will help you to understand the concept behind these two functions and the steps to create a dynamic Top-N dashboard. With dynamic Top-N ranking, you allow the user to use a slicer to select Top 5, Top 10, or Top 20 records instead of manually changing the filter every time to rank a particular set of numbers.

Dynamic Top-N Product Sales Dashboard

Download this dashboard from the link at the end of the blog.

Prepare Your Data and Create a Sales Measure

In our example, we assume you are creating a dashboard to monitor sales performance and use the following data tables:

  • Products:Product ID, Product Name, and Category.
  • Sales:Product ID, Sales Amount, Quantity, and Order Date.
  • Customers:Customer ID, Customer Name, and Region.

Here, we assume that the tables have been related appropriately, and the Products and Customers tables filter the Sales table.

Now, create a measure for Total Sales:

Total Sales =
SUM ( Sales[Sales Amount] )

Create a Dynamic Top-N Selection

Now, let us enable the user to select how many items he wants to display. For this purpose, go to Modeling > New parameter > Numeric range in Power BI Desktop and define the parameters as shown below:

parameters
Configure the parameters to customize the settings.

Power BI will make a table of parameters along with the measure that will contain the chosen value of the user. This measure will be used in DAX formula.

DAX

Selected Top N =
SELECTEDVALUE ( ‘Select Top N'[Select Top N], 5 )

Then, add the parameter field to a slicer. Users can now select 5, 10, 15, or 20, depending on the parameter configuration.

Rank Products Using RANKX()

Now create a measure to rank products based on total sales.

DAX

Product Rank =
RANKX ( ALLSELECTED ( Product[Product Name] ), [Total Sales], DESC, DENSE )

These are the functions of the above measure:

  • ALLSELECTED() considers the selected products and allows other report selections to have an impact on ranking. [Total Sales] is the measure used to compare products.
  • DESC ranks the highest sales first.
  • DENSE assigns consecutive ranks when products have identical sales.

In other words, in case of a tie between two products for the first position, they are both ranked 1 and the next product gets rank 2 through dense ranking.

Display Only the Selected Top-N Products

When the ranking measure is done, design another measure which evaluates whether a product lies within the chosen range.

DAX

Show Top N =
IF ( [Product Rank] <= [Selected Top N], 1, 0 )

Dynamic Ranking Dashboard Design

  1. Now, add Product Name and Total Sales to a bar chart. Then drag Show Top N into the visual-level filters and set it to show items where the value is 1.

Thus, the chart will dynamically change as the user changes the Top-N slicer.

Top N Numbers to filters
Selecting 5 displays the first five ranked products, while selecting 10 displays the first ten.

Use TOPN() to Calculate Top-N Sales

TOPN() works especially well with a more complicated DAX formula, such as calculating the combined sales of the top performers.

DAX

Top N Sales =
VAR TopProducts =
    TOPN (
        [Selected Top N],
        ALLSELECTED ( Product[Product Name] ),
        [Total Sales], DESC
    )
RETURN
    CALCULATE ( [Total Sales], KEEPFILTERS ( TopProducts ) )

This measure calculates the combined sales of the selected Top-N products.

Top n sales

If the user chooses the Top 5, this measure will return the total sales for those five products.

The final dashboard includes the following visuals:

  • Top-N slicer:Helps to specify the number of products displayed.
  • Ranked bar chart:Illustrates the list of products ranked based on sales.
  • KPI cards:Provide information about total sales and selected Top-N sales,
  • Category slicer:Helps explore rankings for a certain product category.
  • Date slicer:It can be optional, but helpful to see the difference in rankings for different reporting periods.

Common mistakes to be avoided

The use of a dynamic ranking dashboard may lead to some unexpected outcomes based on the lack of consideration of the filter context.

ALL() instead of ALLSELECTED():

ALL() eliminates filters from the specified table or column and can affect rankings by ignoring selections that users expect to affect the results. Choose the function according to the desired behavior.

Ignoring ties:

TOPN() returns more than N rows if several items tie at the cutoff. Choose whether you want ties to be included or broken by a secondary field.

Ranking the wrong field:

Rank the right attribute, for example, Product ID, not Product Name, if the names are not unique.

Conclusion

A dynamic Top-N ranking dashboard in Power BI enables you to analyze performance without building additional reports or repeatedly modifying filters. RANKX() is nice to use when you want to show the rank of each item. TOPN() helps you work with a selected group of top-performing records.

FAQs

Yes, we can apply both the RANKX() and TOPN() functions; where RANKX() will rank every product and TOPN() will find the best products selected.

While applying the RANKX() function, both products will receive equal rank values, while using the TOPN() function the number of selected products can exceed.

Extended Learning & References

If you’re looking to deepen your Power BI knowledge and build practical, job-ready skills, explore these professional training courses offered by Power BI Training. These courses cover everything from the fundamentals to advanced reporting, DAX, and Microsoft Copilot integration.

Leave a Reply

Your email address will not be published. Required fields are marked *