Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Compete to become Power BI Data Viz World Champion! First round ends August 18th. Get started.

Reply
Stuki
Frequent Visitor

How to use Top n with additional filter

Hi, 

 

Is there a way of pre-filtering a table before the Top n filter applies?

 

Scenario: I have a list of companies with sales data. I want to show the bottom n companies rated by Gross Margin, filtered with Sales above a certain limit. If I simply apply both those filters at the visualization level then, regardless of the order that I put them in in the panel, it always applies the Top n filter first and then the other filter second. The consequence of that is that it first produces a list of n and then further reduces that list according the second filter. So, instead of a bottom 5, say, I end up with, say 2. What I want is the bottom n companies out of all the companies that meet the second criteria. 

 

Here's some dummy data:

 

CustomerSales ($)
Customer #009       1,500
Customer #006       6,100
Customer #003       7,300
Customer #008     58,500
Customer #004  102,200
Customer #010  142,300
Customer #007  183,000
Customer #001  199,000
Customer #005  203,400
Customer #002  246,000

 

Let's say I want to show the bottom 5 customers by sales with sales above $10,000, this is what I would want to see:

 

CustomerSales ($)
Customer #008    58,500
Customer #004  102,200
Customer #010  142,300
Customer #007  183,000
Customer #001  199,000

 

Ideally, I'd like to be able to do that at the visualization level rather than creating new columns in the data table.

 

Thanks

2 REPLIES 2
amitchandak
Super User
Super User

@Stuki , assume sales is measure create a new measure use numeric parameter if need

 

GT 10k =sumx(Values(Customer[Customer]) , if([sales]>10000, [sales], blank()) )

 

then have topn sales measure 

 

sumx(keepfilterS(TOPN(5, ALLSELECTED(Customer[Customer]) , [GT 10k], asc)), [GT 10k])

 

TOPN with Numeric Parameter -https://youtu.be/cN8AO3_vmlY?t=26448

 

Learn Power BI: Dynamic TOPN using TOPN/Window and Numeric parameter: https://youtu.be/vm2mdEioQPQ

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

Thanks @amitchandak .

 

I'm going to need some time to fully understand this. I'm pretty new to Power BI so still on the learning curve when it comes to DAX. I have made an attempt to put this into my model and the GT 10k measure is working as it should (ie. returning a blank for any customer with sales < 10k). However, the 2nd measure doesn't return anything. This is what I have entered for it (I have changed the names of the measures so that are consistent with my naming convention):

 

Bottom n Revenue = sumx(keepfilterS(TOPN(10, ALLSELECTED(Customers[Company Name]), [Customer Revenue above $10k], ASC)), [Customer Revenue above $10k])
 
Is this correct? Also could you explain what this should do?
 
Please could you explain how I actually use these to produce a table of the bottom n Customers?
 
Thanks again

Helpful resources

Announcements
July 2025 community update carousel

Fabric Community Update - July 2025

Find out what's new and trending in the Fabric community.

July PBI25 Carousel

Power BI Monthly Update - July 2025

Check out the July 2025 Power BI update to learn about new features.