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

Append v/s Merge in Power BI

Let's discuss another problem of the week. As a Power BI user, there are times when you want to combine queries. What are the ways to do so? In most cases, you can attain it by using either append or merge and both serve different purposes. Let's understand what do these terms mean in Power BI and how they are functionally different from each other.  It is quite common to get data from various sources and you need to combine those data depending on a particular column which is common in both tables so that you can add extra information or column to your big table. In such cases, we use merge queries. How to perform merge queries? For instance, I am considering Sample Superstore data and we will merge the returns table to the order table. You will find both merge and append in the home tab in extreme right in the power query editor. ProTip - You will find two options when you click on the drop-down in merge which are merge queries and merge queries as new. When you use merge que

Use Relationship in DAX

Data modeling is an essential part of creating perfect visuals. While creating complex data models there can be a case where you can find an inactive relationship represented by dotted lines and it occurs because you already have an active relationship between the two tables. But as a developer, you need to use both the relationship. How can it be done? You can use "Use Relationship" in such cases. Use relationship can be added to your DAX and act as a modifier or enhancer for calculation. It activates the inactive relation. But make sure you have an inactive relationship in place before using the use relationship function. Let's see how it works on Sample Superstore data. In my fact table I have two dates- Order date and Ship date. I am making the two relations between my date table and fact table. The relation between the sample superstore (date) to date table (date) is active while the relation between the sample superstore (ship date) to date table (date) is inactive