Skip to main content

Highlighting Top 10 with RankX and TopN

Happy Friday!! If you have worked with other BI tools such as Tableau setting up the Top 10 is relatively easy but in Power BI you have dedicated functions such as RankX and Top N to do this. In this blog we will see how to highlight and filter out the TopN Sub Categories based on the Sales recorded in 2013.

Before we start. Here are a few pre-requisites we are using Sample Superstore data and we have created an explicit measures called "Total Sales". If you aren't familiar with implicit and explicit measures do read this article. Total Sales is equivalent to Sum of Sales. 

We will start with RankX. The goal here is to highlight the Top 10 subcategories based on the Total Sales occurred in the year 2013. Refer to the image below.


How to make this? We will start creating a basic DAX which will provide rank to different sub categories. Make sure you provide an order in the DAX itself. In this case we have provided "DESC- descending". Once you have the DAX ready pull it in the matrix to see if we are getting the correct rank based on the sales figure.


One common mistake is to not provide ALL inside the RankX. If we do not provide ALL then your DAX can't interpret the level at which the ranks should be assigned at. In this case we have given Sub Category inside ALL.

Once you get the correct rank in place we can go to the bar graph. But we still haven't provided any color in the DAX. Yes, correct we need to write a separate DAX to give Green color to the Top 10 bars. 

We have created a basic DAX with simple IF statement stating if the RankX measure is greater or equal to 0 then show green color ("green color code"). After this we need to go the formatting and select the conditional formatting of the bars. It will open up a pop up window where we need to select the field value and select the measure.

Isn't it amazing? Let's take it to the next level. What if I say we can make this formatting dynamic in nature. What if the end user can change Top 10 to Top 5 and your visuals changed accordingly. Yes, that is possible in Power BI. We will reap the advantage of Numeric Range Parameter.

To create parameter go to the Modelling section in the top ribbon and select Numeric Range under New Parameter. This will open up a pop up window.


Since I only have 18 sub categories I chose maximum as 20. Once you create the parameter a new slicer will be created but this slicer doesn't have any impact on the bar graph. To enable the interaction between these 2 we need to adapt the DAX.


Can you figure out the changes made in this DAX? We have moved to Switch from If and instead of providing a number we have given TopN Value which will make the new slicer coming from the parameter into the play. 


What if we only want to show the Top N bars and rest of the bars shouldn't appear at all. To do this we are using TopN function. TopN function is actually a very straight forward. 


Wait!!! What???? I thought TopN is straight forward. There is no error appearing in the DAX. This is one of the common mistakes. The error says the expression refers to multiple columns.Let's get rid of this error message.

Can you notice the changes? We have included the Calculate and Values. With the help of Values we have provided unique values and hence we get Top 10 sub categories. 

Let's use the parameter of TopN and combine with the Sub Category Rank with TopN measure. This will allow us to showcase only TopN bars. To do so we need to adjust our DAX a little bit. Instead of providing a number 10 right after TopN we will include the TopN value. Easy peasy!!! Similarly you can create measures for BottomN just by changing the order from Desc to Asc.









"All our articles are intended to address all the frequently asked questions related to a topic. Do leave a comment if it answers any of your question. "


Thanks for Reading Let's connect on LinkedIn. For more such blogs and pro tips do follow us






Comments

Popular posts from this blog

Ultimate Beginners Guide to DAX Studio

There are zillions of external tools available with Power BI but DAX Studio is one of the most commonly used tools to work with DAX queries. It is a perfect tool to optimize the DAX and the data model. In this blog let's shed some light on the basic functionalities that can take your report to the next level. ARE YOU READY?  To start you will need the latest version of the DAX Studio. You can download it from their website . Don't worry you don't have to pay for the license. Fortunately, DAX Studio is a free tool As a BI Developer, I am using DAX Studio regularly. Based on my experience I use it for several purposes but in this blog, I will highlight the most common ones. Extracting a dump of all the measures used in your PBIX. Why do we need to do this? It can be used for documentation purposes also sometimes we try to reuse the DAX and such a dump comes in handy in this scenario. How to achieve it? Open the DAX Studio it is located under the external tools once you open t...

Identify and Delete Unused Columns & Measures

Heavy dashboards and a bad data model is a nightmare for every BI Developer. Heavy dashboards can be slow due to multiple reasons. It is always advised to stick with best practices. Are you still figuring out about those best practices then you should definitely have a quick read on Best Practice Analyser ( link ). One of the most common issues with slow dashboards is unused columns and unused measures.  It is very normal to load some extra columns and create some test measures in your dashboard but as a part of cleanup process those unused columns and unused measures should be removed. Why we are removing them? Because if you keep them then ultimately it will increase the size of your data model which is not a good practice.  How to identify the culprits (unused columns and unused measures)? In today's blog we will provide you with 2 most common external tools which will help you in identifying the culprits. More external tools😒. Who's going to pay for this? To your surprise...

Best Practice Analyser (BPA) Guide

Do you want to save tons of efforts to check if your data model and PBIX file follows the standard best practices and norms? Then this blog is for you. If you are a follower of our channel we already deep dive into the importance of the DAX Studio as an external tool. If you are a beginner I would highly recommend to visit this blog . In today's blog we will check how Tabular Editor can help to optimize the data model.  Best Practice Analyser allows to define or import best practices. It will make sure that we do not violate the best practices while developing a dashboard. Isn't it exciting!! Before we start make sure you already have Tabular Editor version 2.24.1 installed on your system. To install it do visit this link and select the link for windows installer. Once Tabular Editor is installed it will reflect in your PBIX file under external tool. Also, we need to define the standard rules. To do so in your advanced scripting or C# script copy this and save it via Ctrl+S. An...