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 and bottom n in a single visual using slicer dynamically in Power BI? ›

In this blog, I will show the necessary steps to achieve this.
  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. ...
  4. Then, drag the value we created before in the slicer table to slicer.
May 16, 2023

How do I create a dynamic slicer in Power BI? ›

To create a dynamic slicer, Click on Field from New Parameter from the Modeling tab, all you need to do is select the data you want to slice and filter, and then select the “dynamic slicer” option in the visualization pane. This will create a dynamic slicer for your selected data.

How do you use the sync slicer in Power BI? ›

To sync two or more separate slicers, you mark them as being part of a group.
  1. Make sure the Sync slicers pane is visible by selecting it from the View menu.
  2. Select one of the slicers that you want in the group. ...
  3. Then select another slicer you want to sync with the first, and enter the same name in the group name box.
Jan 18, 2023

How do you show hierarchy data in Power BI? ›

To view your hierarchical scorecard, you can use the slicer to navigate to whichever level of your hierarchy you want to view. From here you can do things like open the details pane, go to a report, and perform check-ins, just like any other scorecard view.

How do you show values in a donut chart in Power BI? ›

Start by creating a donut chart in Power BI with your desired data. Drag and drop the category field into the "Legend" area and the value field into the "Values" area of the chart visualization. Next, add data labels to your donut chart: Select the donut chart on your report canvas.

How do I create a dynamic table in Power BI? ›

Steps to Create a Dynamic Date Table
  1. Start with a Blank Query: From the Home tab, choose 'New Source' and select 'Blank Query'. ...
  2. Convert and Rename Columns: Convert the query to a table and rename the column to 'Current Date' for clarity.
  3. Set Up Date Ranges: ...
  4. Generate the Full Date List: ...
  5. Add Time Segments:
Apr 25, 2024

How do you show data labels in Power BI bar chart? ›

On the Layout tab, in the Labels group, click Data Labels, and then click the option that you want. For additional data label options, click More Data Label Options, click Label Options if it's not selected, and then select the options that you want.

How to show top 5 products in Power BI? ›

To display only the top 5 products in terms of sales, follow these steps:
  1. Click on the bar chart to select it.
  2. In the “Visualizations” pane, click on the “Filters” tab.
  3. Click on the “Product” dropdown under the “Visual level filters” section.
  4. Change “Basic filtering” to “Top N” by clicking on the dropdown.
May 3, 2023

How do I add a slicer to a specific visual in Power BI? ›

To enable slicers

Click on Edit Interactions and the choose the "Filter" option that appears on top to enable the slicer to affect the visual. Follow the same steps to edit interactions (disable or enable) to more than one visual.

What are the different viewing options available in Power BI slicer visual? ›

The versatile Power BI slicer
  • Available for all data types. List (vertical and horizontal orientations) Dropdown.
  • Also available for Date or DateTime data types. Between. Before. After. Relative Date. ...
  • Also available for Whole Number or Decimal Number data types. Between. Less Than or Equal To. Greater Than or Equal To.
Sep 21, 2020

How do you make a visual independent of a slicer? ›

Click on the slicer and then under the tab Format, select Edit Interactions. Turn off the interaction on the visual you don't want the slicer to affect. Click on the slicer and then under the tab Format, select Edit Interactions. Turn off the interaction on the visual you don't want the slicer to affect.

Top Articles
Destiny 2: Prisma Fragmente freischalten – Alle Fundorte auf Deutsch
Is badlion Anticheat being discontinued?
Fone Tech Cleveland Ms
Double Helicath Clan Boss
Boost Mobile 69Th Ashland
Camila Cabello Wikifeet
Tweaker Configuration
Friscolawnmowing
Craiglist Mohave
Raymond James Stadium Seat Map Taylor Swift
Okc Farm And Garden Craigslist
Mannat Indian Grocers
Ge Tracker Awakener Orb
Katmoie
Stepmom Full Video Hd
Immobiliare di Felice| Appartamento | Appartamento in vendita Porto San
Riverrun Rv Park Middletown Photos
Food King El Paso Ads
Elfqrindiscard
Uscis Fort Myers 3850 Colonial Blvd
80 For Brady Showtimes Near Brenden Theatres Kingman 4
Clean My Mac Sign In
Vegamovies Home
Monroe County Incidents
Money Network Pay Stub Portal 711
Barber Gym Quantico Hours
100000 Divided By 3
Shaleback Hollow Location
Myrtle Beach, South Carolina: Abwechslungsreicher Freizeitspaß unter der Südstaaten-Sonne
Enter Cautiously Nyt Crossword
247 Transfer Portal Rankings Basketball
Netronline Historic Aerials
Juicy Deal D-Art
Ctbids Reno
Www.1Tamilmv.cfd
This Eras Tour Detail Makes Us Wonder If Taylor & Karlie Still Have Bad Blood
Culver's Flavor Of The Day Little Chute
Wgu Admissions Login
Kieaira.boo
Rida Asfahani Leaked Video
Salon5 – Europa, was geht? – Podcast
Wrdu Contests
About Data | Weather Underground
Jami Lafay Gofundme
Unblocked Games 67 Ez
Exceptions to the 5-year term for naturalisation in the Netherlands
When His Eyes Opened Chapter 191
Walmart Makes Its Fashion Week Debut
James in Spanish | Spanish to Go
7-11 Paystub Portal
Latest Posts
Article information

Author: Tuan Roob DDS

Last Updated:

Views: 5923

Rating: 4.1 / 5 (42 voted)

Reviews: 89% of readers found this page helpful

Author information

Name: Tuan Roob DDS

Birthday: 1999-11-20

Address: Suite 592 642 Pfannerstill Island, South Keila, LA 74970-3076

Phone: +9617721773649

Job: Marketing Producer

Hobby: Skydiving, Flag Football, Knitting, Running, Lego building, Hunting, Juggling

Introduction: My name is Tuan Roob DDS, I am a friendly, good, energetic, faithful, fantastic, gentle, enchanting person who loves writing and wants to share my knowledge and understanding with you.