October 9, 2019   Tips and Tricks /

Power BI: Top N and Other

In this tutorial, I'll walk you through the creation in Power BI of a dynamic list with the Top N products and Other. You can see the result below, where the list is filtered based on the selected month. 

 

Power BI table with top N products and other
Final screen with top N and slicer

Load the external file in Power BI, for creating the top N table

For this example, I'll use a sample table with 3 columns: sales date, product sold and amount of sales. To follow along with the tutorial, you can download the sample data from here.

Below is what the table looks like once it's loaded.

Loaded table in Power BI
Loaded table

Create a new table with all the products and additional row for other

The first step is to create a new table with a column having all the products plus 'Others'. The table can be created with DAX. For that, click on 'New Table' in the Modeling tab.

Add a new table in Power BI, in the Modeling tab
Add a new table

After clicking it, a text field will appear. You can then paste the following DAX code in your textbook:

products table =
UNION ( VALUES ( 'sales by date' ), ROW ( "product", "Other" ) )

Here is the explanation of the code: 

'Products table' is the name of the new table, you can choose any name you wish. 

The expression does three things: the VALUES function creates a list of all the products, the ROW function creates a new product called Other, and finally, UNION merges the product list with the other product.

You can check out the final list by clicking on ' Data Tab' on the left pane.

Table created with all products
Check new table with Data tab

Create a Power BI measure with the value for top N products and Other

The final step is to create the values for this new table. 

Logic for Top N and Other

The logic is the following:
– if the product is in the Top N (top 10 in the example), show the sales for the product
– if the product is Other, show the total sales minus the sales of the Top 10 products
– for Total, show the total sales
– otherwise (if the product is not in the Top N), don't show it

Add a measure

This calculation will be saved in a measure. You create a new measure by clicking on New Measure in the Modeling tab.

Add a new measure in Power BI from Modeling tab
Create a new measure

You can paste the following formula for the Top N and Other products in the textbox. It's a long formula so I'll walk you through it.

Sales Top N and Others =

//1- create a table with the top 10 products only
VAR topProducts = TOPN (10, VALUES ( 'sales by date' ),
CALCULATE ( SUM ( 'sales by date'[sales] ) )
)

//2- calculate the sum of sales of the top 10 products
VAR sumOfTopProducts =
SUMX ( topProducts, CALCULATE ( SUM ( 'sales by date'[sales] ) ) )

//3- calculate the sum of the current product, of all products, and of other products
VAR currentProduct = SELECTEDVALUE ( 'products table' )
VAR currentProductSales = CALCULATE (SUM ( 'sales by date'[sales] ),'sales by date' = currentProduct )
VAR totalSales = SUM ( 'sales by date'[sales] )
VAR otherProductsSales = totalSalessumOfTopProducts

//4- check what is the product currently selected
VAR topProductSelected = CONTAINS ( topProducts, , currentProduct )
VAR otherSelected = currentProduct = "Other"
VAR selectionIsTotal = NOT ( ISFILTERED ( 'products table' ) )

RETURN
//5- if the product is in the top 10, show the sales for the product
IF ( topProductSelected, currentProductSales,
//6- if the product is other, show the sum of Other products
IF (otherSelected, otherProductsSales,
//7- for Total, show the total sales. Otherwise (for the product is not in top 10), show nothing (blank)
IF (selectionIsTotal, totalSales, BLANK() ) ))

Top N measure explanation

There are 2 parts to this formula: what comes before the RETURN (steps 1 through 4) and what comes after the RETURN (steps 5 through 7). 
Before the RETURN are intermediary calculations, and after RETURN is the logic that was described above.

Step 1 creates a table with the top 10 products, based on each product's sales. The next step uses the table created in step 1 and calculates the sum of the top 10 products.
Step 3 calculates the sales of the selected product, the total sales and the sales of other products.

Step 4 creates intermediary variables based on the product currently selected: 
topProductSelected checks if the product selected is in the top 10,
otherSelected checks if the selected product is Other,
selectionIsTotal checks if the total is selected

Finally, steps 5 through 7 reuse the intermediary calculations above to implement the logic.

After pasted, you click on Commit and you have your formula!

Commit the formula
Commit the formula

Visualize your Top N and Other

You can now insert your visualization, remembering to choose the product and measure from your new product table instead of the initial table.
Power BI now shows your table with your Top N and other products!

Add a new visualization with the new table and measure
Add a new visualization using the new table and measure

Add a slicer

Once the visualization is in place, you can add a slicer and filter the new table as the Top N is completely dynamic!. The Top products and Other line will be recalculated automatically.

Add a slicer
Add a slicer

The slicer can be anything you wish and does not need to be months: it could be years, regions, customers.

Enjoyed the article? Subscribe to the newsletter and download the Power BI of the article for free.

    Share this post

    AUTHORLuca
    I help SMEs who want to get a clear view on what drives their profit. After becoming a CPA and 10 years in Corporate Finance between the US, Europe, and Asia I discovered my real passion. I love helping Small and Medium Companies figure out how to improve their profit and be sustainable.

    Join the discussion

    4 Comments

    • mehul says:

      Want to learn Power BI

    • Adam says:

      This article has just saved me from a lot of very frustrating work – I really like how carefully you've explained everything too. Everything's working like a charm and it was very easy to tweak things to my data set's needs.

      Thank you so much!

      • Luca says:

        Hi Adam, I am glad that you found it useful!

        Best of luck for building your Power BI dashboard

    Leave a Reply

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

    LN Consulting
    Tired Of Building A Dashboard By Yourself?

    Continue Reading...

    May 2, 2022
    A recurring problem that Power BI users find is that of incorrect totals. Here is an example: I have a table with Salespeople and their monthly sales, based on that I want to calculate their sales incentive. The sales incentive is equal to 5% of the sales if they sell more than 80k$, otherwise it’s ...
    Read More
    October 17, 2019
    A question often asked is ’how can I do a custom sort in Power BI’. It’s a common need but its implementation is not straightforward in Power BI. In this post, I will walk you through it in the easiest possible way. As an example, I will start with a Sales table that has 3 ...
    Read More