Dynamically Display Top N and Bottom N Records in One Power BI Chart (2024)

By: Kenneth A. Omorodion |Updated: 2024-06-13 |Comments | Related: > Power BI Charts


Problem

Recently, I wrote an article,Rank and Sort Data Based on Multiple Columns in Power BI Using DAX. However,it is very common for business users to request the ability to dynamically viewthe Top N and Bottom N values of a measure, like Total Sales, on the same visual.This requirement is simple to implement on either the Top or Bottom N options. But,the challenge is when we need to represent the two options on the same chart simultaneously.

Solution

This article will demonstrate step-by-step how to dynamically represent Top Nand Bottom N in the same Power BI chart. However, I recommend that you readmyother article before reading this one, as I will be referencing it since itexplains how the ranking part is done using different approaches.

For this demo, I will leverage a data model with a Fact table called "Sales"(below). Of course, I will only show a snapshot of data rows within the table.

Dynamically Display Top N and Bottom N Records in One Power BI Chart (1)

I would also use a Dimension table called "Product"(below), which has more rows of data.

Dynamically Display Top N and Bottom N Records in One Power BI Chart (2)

The requirement is as follows: On a bar chart, we need to show the top five andbottom five Products from the Products tablebased on the Total Sales value derived from the Sales table.The anticipated output should look like the image below.

Dynamically Display Top N and Bottom N Records in One Power BI Chart (3)

Now that we understand the requirements, outlined below are the steps to follow.Remember to establish a relationship between your tables before you continue.

  1. Create a measure to summarize the Revenue on the Sales fact table.
  2. Create a measure to rank the measure created in step 1 and then apply alogic to render the top and bottom N in the visual.
  3. Apply the measure in step 2 to the Bar Chart visual.
  4. Apply a conditional formatting if required.
  5. Compare the output on the Top and Bottom N visual to when no DAX logic isapplied.

Step 1: Create a Measure to Summarize the Revenue on the Sales Fact Table

This step is very straightforward. You need to create a measure on theRevenue column as follows. In this demo, I have referred to this measure as "Sales."

Sales = SUM(Sales[Revenue])

Step 2: Create a Measure to Rank the Measure Created in Step 1 and Apply a Logicto Render the Top and Bottom N in the Visual

In this step, we will create a measure that first ranks the products and thenapplies a logic to only pull the Top N and Bottom N values and represent them onthe Bar Chart, as seen in the final output above.

To create this measure, use or adapt the following DAX code:

 Rank Value (Sales) = VAR _rank_top = RANKX ( ( ALLSELECTED ( 'Product'[Product] ) ), Sales[Sales],, DESC, DENSE )VAR _rank_bottom = RANKX ( ( ALLSELECTED ( 'Product'[Product] ) ), Sales[Sales],, ASC, DENSE )VAR _result = IF ( _rank_top <= 5, Sales[Sales], IF ( _rank_bottom <= 5, Sales[Sales], BLANK () ) )RETURN _result 

Step 3: Apply the Measure in Step 2 to the Bar Chart Visual

To do this, select a bar chart among the visuals in Power BI. Note: In your case,this might be other visuals. But a bar chart is best to demonstrate in this tip.

In the image below, you can see that I have included the "Product"column from the Products table into the Y-axis well and the newmeasure created in Step 2 in the X-axis well.

This ensures that only the Top 5 and Bottom 5 products are rendered on the visualbased on the total Sales value.

Dynamically Display Top N and Bottom N Records in One Power BI Chart (4)

Step 4: Apply Conditional Formatting, If Required

This is an optional step. If you are interested in applying conditional formattingto help easily differentiate between the Top 5 and Bottom 5 products by sales, thenfollow this step. I have used bar color as the conditional formatting for this demonstration.I will use green bars and red bars for Top N and Bottom N, respectively.

The conditional formatting window can be seen in the image below. Note: I havenot applied any logic for the conditional formatting to identify the Bottom N orTop N ranges. However, for simplicity, I have hardcoded the values in this tip justto show the concept. In your case, it might be obvious what to do. For example,you might have all your Bottom N values as negative values; thus, it would be easierto implement the conditional formatting.

Dynamically Display Top N and Bottom N Records in One Power BI Chart (5)

The output would look like the image below.

Dynamically Display Top N and Bottom N Records in One Power BI Chart (6)

Step 5: Compare the Output on the Top and Bottom N Visual to When No DAX Logicis Applied

This step demonstrates how to figure out if the output of the previous steps(particularly Step 3) has yielded the desired outcomes based on business requirements.

To do this, create a new bar chart visual with only the "Sales"measure created in Step 1, as well as the Products columnfrom the Products table, as seen in the image below.

Dynamically Display Top N and Bottom N Records in One Power BI Chart (7)

When you compare the above bar chart with the bar char for the Top and Bottom5 created earlier, you can see that they match for the Top 5 bars, representingthe Top 5.

Let's do the same for the bottom values by taking a snapshot of the bottompart of the image above. This shows that the DAX code we wrote earlier inStep 2 has dynamically pulled the bottom 5 products based on sales. See the imagebelow.

Dynamically Display Top N and Bottom N Records in One Power BI Chart (8)

In summary, this article successfully demonstrated how to represent Top N andBottom N on the same chart using DAX in Power BI. The N value is not fixed, allowingyou to adjust it according to your business needs. This logic can also work if youuse a rank of multiple fields from different or the same tables, but you would needto apply the logic used in my earlier article.

Next Steps

Learn more about Power BI in this 3 hour training course.


Click here to start the Power BI course




About the author

Kenneth A. Omorodion is a Business Intelligence Developer with over eight years of experience. He holds both a bachelor’s and master’s degree (Middlesex University in London). Kenneth has the MCSA, Microsoft Data Analyst - Power BI and Azure Fundamentals certifications. Kenneth is a Microsoft Certified Trainer and has delivered corporate training on Power BI, SQL Server, Excel and SSRS.

This author pledges the content of this article is based on professional experience and not AI generated.

View all my tips

Article Last Updated: 2024-06-13

Dynamically Display Top N and Bottom N Records in One Power BI Chart (2024)

FAQs

How to display top n and bottom n in the same view Power BI? ›

How to Show Top and Bottom N Value in a Single Visual in Power BI
  1. Create a new table which will be used as a slicer then.
  2. Create a measure you want to show, in my case, it would be: Average Gini = AVERAGE(Sheet1[GINI])
  3. Then, create the following measures.
May 16, 2023

How to show top 5 and bottom 5 in Power BI? ›

Top 5 Total Revenue By Product Names
  1. To display the Top 5: Select the first Table Visualization.
  2. In the Filters pane to the right, hover the mouse to the Product Names.
  3. Click on the downward facing caret icon.
  4. In the filter type dropdown, select Top N.
  5. Type in 5 into the box.
Nov 7, 2019

Why is my Power BI top n filter showing more than 10? ›

This is because you are filtering the response date column. So you are asking Power BI to give you a list of 10 dates and then it filteres the table by that. If you want just 10 rows of data you would need to apply your filter to some unique column like a responseID that has a different value in each row.

How do I connect two visuals in Power BI? ›

To create a group of visuals in Power BI Desktop, select the first visual from the canvas, then holding the CTRL button, click one or more additional visuals that you want in the group. In the Format menu, select Group, and from the submenu select Group. Groups are displayed in the Selection pane.

How do I align multiple visuals in Power BI? ›

Select the top right visual, hold down the Ctrl key, and select the top visual from the left side of the page. From the Format tab in the Report View, click on Align top from the Align drop-down icon. The Y Position value of both visuals should now be equal in the General format options. ...

How can we display the top and bottom 5 of records in a single tableau view? ›

Here are the steps to show the top 5 and bottom 5 in tableau:
  1. Create a calculated field with the calculation as follows: RANK( SUM( [Sales] ), 'desc' ) <= [N Parameter] ...
  2. Drag the newly created field to the Filters shelf. ...
  3. Right-click the newly created field on the Filters shelf. ...
  4. In the Filter dialog, check True.
Oct 16, 2020

How do I group data in Power BI? ›

To use grouping, select two or more elements on a visual by using Ctrl+Select. Then right-click one of the selected elements, scroll to Group, and choose Group from the context menu. Once it's created, the group is added to the Legend well for the visual with (groups) appended.

What is the difference between remove filter and all in Power BI? ›

Difference between ALL and REMOVEFILTERS:
  1. ALL: Removes filters and returns a table or a column, which can be used in other DAX functions like CALCULATE and SUMX. ...
  2. REMOVEFILTERS: This function does not return a table or a column; it only removes filters from the specified table(s) or column(s).
Apr 15, 2023

Can you do a top 10 filter in Power BI? ›

The visual-level filters of a visual in Power BI allow you to reduce the number of elements in a visual. This approach makes it very easy to apply a filter to the top 10 products in a report, according to the selection required in other slicers or visuals.

Can Power BI auto refresh more than 8 times? ›

The quota of eight refreshes resets daily at 12:01 a.m. local time. If the semantic model resides on a Premium capacity, you can schedule up to 48 refreshes per day in the semantic model settings. For more information, see Configure scheduled refresh later in this article.

How do you show both values and percentages in Power BI? ›

Often you may want to create a bar chart in Power BI and display both the value and the percentage. The easiest way to do this is by using the Table visual and by using Data bars as conditional formatting.

How do I change the layout in Power BI? ›

You can open mobile layout view in both Power BI Desktop and the Power BI service. Use the layout switcher at the bottom of the page to toggle between mobile and web layout. Alternatively select the View ribbon and choose Mobile layout.

How do I use the same formatting in Power BI? ›

1. You can copy and paste the visual from the formatted pbix and then use a format painter. 2. You can create a custom theme and use it on all reports.

Top Articles
Latest Posts
Article information

Author: Edmund Hettinger DC

Last Updated:

Views: 6244

Rating: 4.8 / 5 (78 voted)

Reviews: 93% of readers found this page helpful

Author information

Name: Edmund Hettinger DC

Birthday: 1994-08-17

Address: 2033 Gerhold Pine, Port Jocelyn, VA 12101-5654

Phone: +8524399971620

Job: Central Manufacturing Supervisor

Hobby: Jogging, Metalworking, Tai chi, Shopping, Puzzles, Rock climbing, Crocheting

Introduction: My name is Edmund Hettinger DC, I am a adventurous, colorful, gifted, determined, precious, open, colorful person who loves writing and wants to share my knowledge and understanding with you.