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
PowerBIBegin
Regular Visitor

How to show top N rows in a specific hierarchy with a slicer?

Hello,

 

My data is at the bottom of my post. My Power BI looks like the following screenshot. How can I add a slicer so that I can dynamically show top N rows for each city based on Count of Machine ID. For example, if N is 2, only Recife and Rio de Janeiro will show up under Brazil. Only Tokyo and Kyota will show up under Japan. In Argentina, since it only has Bluenos Aires, so only Bluenos Aires should show up.PowerBI.png

 

 

 

Machine ID
City
Country
Continent
Year
213746
Kolwezi
DR Congo
Africa
2000
275601
Lubumbashi
DR Congo
Africa
1900
509142
Nagoya
Japan
Asia
2000
610214
Tokyo
Japan
Asia
1950
461479
Recife
Brazil
South America
2000
976554
São Paulo
Brazil
South America
2000
606025
São Paulo
Brazil
South America
1900
528691
Buenos Aires
Argentina
South America
2000
434051
Rio de Janeiro
Brazil
South America
1950
489893
Buenos Aires
Argentina
South America
2000
397222
Buenos Aires
Argentina
South America
1850
245315
Tokyo
Japan
Asia
2000
945377
São Paulo
Brazil
South America
1850
1 ACCEPTED SOLUTION

Hi,

Use this measure

Measure 2 = SUMX(VALUES(Data[Country]),[Measure])

Hope this helps.

Ashish_Mathur_0-1755299376764.png

 


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

View solution in original post

11 REPLIES 11
MarkLaf
Super User
Super User

I think this should add up correctly. Using numeric range parameter to set N. Selected N is referenced with the [Top N Value] measure, which PBI creates for you by default when setting up parameter.

 

Top N Cities Machine Count = 
VAR _aggRows =
    CALCULATETABLE (
        SUMMARIZECOLUMNS (
            Data[Country],
            Data[City],
            "MachineCount", COUNTROWS ( Data )
        ),
        ALLSELECTED ( Data )
    )
VAR _topNCities =
    WINDOW (
        0, ABS,
        [Top N Value], ABS,
        _aggRows,
        ORDERBY ( [MachineCount], DESC ),
        PARTITIONBY ( Data[Country] )
    )
RETURN
    CALCULATE (
        COUNTROWS ( Data ),
        KEEPFILTERS ( _topNCities )
    )

 

Note: assuming that the Machine ID is your primary key in the table, so using COUNTROWS rather than other count functions.

v-achippa
Community Support
Community Support

Hi @PowerBIBegin,

 

Thank you for reaching out to Microsoft Fabric Community.

 

Thank you @MasonMA and @Ashish_Mathur for the prompt response.

 

As we haven’t heard back from you, we wanted to kindly follow up to check if the solution provided by the user's for the issue worked? or let us know if you need any further assistance.

 

Thanks and regards,

Anjan Kumar Chippa

Ashish_Mathur
Super User
Super User

Hi,

PBI file attached.

Hope this helps.

Ashish_Mathur_0-1754705209921.png

 


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

Hello Ashish,

 

Thanks for uploading your pbix file. I have one question. In the screenshot below, when I select Parameter = 2, The Measure of South America is 6 but actually it should be 8 (5 from Brazil and 3 from Argentina). Also, why the Total at the bottom is 6? Should it not be (Measure of South America + Measure of Asia + Measure of Africa) = 6 + 3 + 2 = 11?

 

 

 

question.png

You are welcome.  Share the download link of the PBI file.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

Hi Ashish, I can't find your link to the PBI file in your reply.

I have requested you for the download link.  Share that file (the picture of which you showed in your reply posted in Wednesday)


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

Hello Ashish, please see this pbix file. When I set Parameter to 2. The number of South America is 6 but it should be 8 (5 from Brazil and 3 from Argentina). Also, the Total at the bottom is 6 but it should be (Measure of South America + Measure of Asia + Measure of Africa) = 6 + 3 + 2 = 11.

PowerBIBegin_0-1755289838061.png

 

Hi,

Use this measure

Measure 2 = SUMX(VALUES(Data[Country]),[Measure])

Hope this helps.

Ashish_Mathur_0-1755299376764.png

 


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/
MasonMA
Memorable Member
Memorable Member

Hello @PowerBIBegin 

 

One of the solutions would be 'build a Numeric Parameter' and use this Parameter as Top N in your slicer. 


Under Modelling, you can create a 'Top N' parameter as below

MasonMA_0-1754699516920.png

 

Create one explicit measure to Count the Machines. 

Machine Count = COUNT('Table'[Machine ID])

 Use below Measure as Value in your Matrix to dynamically rank the Cities. 

Top N Cities = 
VAR SelectedN = SELECTEDVALUE('Top N'[Top N], 1) 
VAR CurrentCountry = SELECTEDVALUE('Table'[Country])
VAR CurrentCity = SELECTEDVALUE('Table'[City])
VAR CityRank = 
    RANKX(
        FILTER(
            ALLSELECTED('Table'[City], 'Table'[Country]),  
            'Table'[Country] = CurrentCountry
        ),
        CALCULATE([Machine Count]),  
        ,
        DESC,
        DENSE
    )

RETURN
    IF(
        CityRank <= SelectedN,
        [Machine Count],  
        BLANK()              
    )

 

With your sample data, i have below result (you can switch off the 'Row Subtotals' in the formatting pane)

MasonMA_1-1754699799336.png

Hope this helps:) 

Hello @MasonMA 

 

Thanks for your reply. With your solution, I'm able to show top N cities. However, the number in the right column is not correct. In the following screenshot, I select 2 and it shows top 2 cities in the matrix, which is correct but somehow Japan is 101. I think it should be the sum of Kobe and Kyoto, which are 30 and 26, respectively. So Japan should be 56. Asian should be 56, too. This is the link to my pbix and data files.

 

PowerBIBegin_0-1755098574501.png

 

Helpful resources

Announcements
August Power BI Update Carousel

Power BI Monthly Update - August 2025

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

August 2025 community update carousel

Fabric Community Update - August 2025

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

Top Solution Authors