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

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
nish18_1990
Helper II
Helper II

Top N values based on the total count in matrix

 
 

Hi ,

 

I need a top N parameter based on the total count of Sales .  If i select top 2 then it should show me top 2 subcategory based on the total Sales which is displayed on the right side .

 

nish18_1990_0-1750747627078.png

 

 

 

Data:

 

CategorySub CategorySalesQuantity Q2country
FurnitureChairs10010China
FurnitureTables10020China
AutomobileCars10030China
AutomobileBikes70040China
FurnitureChairs10013India
FurnitureTables50022India
AutomobileCars10054India
AutomobileBikes10060India
FurnitureChairs10030China
FurnitureTables30029China
AutomobileCars20088China
AutomobileBikes10022China
FurnitureChairs50021India
FurnitureTables10033India
AutomobileCars60054India
AutomobileBikes10060India
FurnitureChairs10030China
FurnitureTables10029China
FurnitureSofa10088China
FurnitureBed10022China
FurnitureDining10021India
FurnitureTables10033India
FurnitureTables10054India
FurnitureSofa10060India
AutomobileCars100054India
AutomobileBikes10060India
FurnitureChairs10030China
FurnitureTables20029China
FurnitureSofa10088China
FurnitureBed10022China
FurnitureDining10021India
FurnitureTables40033India
FurnitureTables80054India
FurnitureSofa10060India

 

1 ACCEPTED SOLUTION

@nish18_1990 - then you have NO OPTION other than to use a Table visual (not a Matrix). It cannot be a Matrix because a Matrix does not supply the Row context for Sub Category that is needed for the rank. 

 

If you want to break ties by Sub Category YOU NEED TO USE A TABLE. 

 

I have attached a final version of this file. Page 1 has the measure Rank 2 which uses the DAX below to rank in the following order:

 

1. Count of Quantity

2. Sum of sales (to break any ties above)

3. Sub Category A-Z (to further break ties from above)

4. Country A-Z (to break the final ties)

 

VAR _rank =
    RANK (
        DENSE,
        ALLSELECTED ( 'Table'[Sub Category], 'Table'[country] ),
        ORDERBY (
            CALCULATE ( COUNT ( 'Table'[Quantity Q2] ) ), DESC,
            CALCULATE ( SUM ( 'Table'[Sales] ) ), DESC,
            'Table'[Sub Category], ASC,
            'Table'[country], ASC
        ),
        LAST
    )
RETURN
    IF ( _rank <= [Parameter Value], _rank )

 

The table also contains two measures for Count of QTY and Sum of Sales that contain logic to return blank when the rank returns blank. This happens when the parameter value is set to show only a certain number of results. - Which has been your requirement all along, and can only be achieved using a TABLE. 

 

I've now provided this solution multiple times and fine tuned it as much as I can, I'd appreciate it if you could mark it as the solution. 

 

If I answered your question please mark my post as the solution, it helps others with the same challenge find the answer!

View solution in original post

12 REPLIES 12
maruthisp
Solution Sage
Solution Sage

Hi @nish18_1990 ,
I tried to implement the solution for the problem. Please check the pbix file.
Top N values based on the total count in matrix.pbix
I checked in a table visual only. Please let me know if there is any questions..

If this reply helped solve your problem, please consider clicking "Accept as Solution" so others can benefit too. And if you found it useful, a quick "Kudos" is always appreciated, thanks! 

 

Best Regards, 

Maruthi 

LinkedIn - http://www.linkedin.com/in/maruthi-siva-prasad/ 

X            -  Maruthi Siva Prasad - (@MaruthiSP) / X



m4ni
Advocate I
Advocate I

Hi @nish18_1990 

If I understand correctly you should be able to acheive this from the filter pane.

 

Select TOP N from on the Sub category and then enter 2.

I would create a measure for the count of sales and then select that measure as the By Value in the filter pane.

Please see screenshot unsing your data.

 

m4ni_0-1750754029275.png

 

Hope this is what you mean.  Otherwise please explain further.

Thanks

 

No I dont want it in filter tab . I need a dynamic parameter for top N . If somebody choose 5 on parameter it should be top 5 categories . I did achieved it partially by rank .

Var Ranks:
CALCULATE(
    RANKX(
    ALL('My_Main_Table'),
    CALCULATE(
    [#sales],
        ALLEXCEPT('My_Main_Table',My_Main_Table[Subcategory])),,
    DESC,
   Dense
        )
    )

 

But the issue  i am getting  is same rank for same count , because of that if i select 10 on paramater , I am getting more than 10 sub categories :

 

nish18_1990_0-1750754810409.png

 

@nish18_1990 - You need to find a way to break your ties, beacuse your rank is only happening over one column where some of the values are the same.

 

Below I have done this with the Quantity and Sub Category columns. 

 

RANK (
    DENSE,
    ALLSELECTED ( 'Table' ),
    ORDERBY (
        'Table'[Sales], DESC,
        'Table'[Quantity Q2], ASC,
        'Table'[Sub Category], ASC
    ),
    LAST
)

 

If I answered your question please mark my post as the solution, it helps others with the same challenge find the answer!

@nish18_1990 - I thought I would finish off your problem. Given you have not indicated you have found the solution. 

 

Please find the attached file, which only shows the number of categories within the "top" number selected. 

 

I have done this by ranking the sales first, and to break the ties I have used Quantity then Sub-category. 

 

I hope this enables you to see how this can be done. 

 

mark_endicott_0-1750843874285.png

mark_endicott_1-1750843889532.png

 

If I answered your question please mark my post as the solution, it helps others with the same challenge find the answer!

 

 

Hi , 

 

Its working in table structure but when i convert it in matrix table it is not working . If the table is below form how to use the parameter on basis of the total count of quantity 

 

nish18_1990_0-1750848853921.png

 

@nish18_1990 - Ok this DAX fixes that:

 

VAR CurrentSubCategory = SELECTEDVALUE('Table'[Sub Category])

VAR QuantityQ2 =
    CALCULATE(
        COUNT('Table'[Quantity Q2]),
        ALLSELECTED('Table'[Sub Category])
    )

VAR SalesTotal =
    CALCULATE(
        SUM('Table'[Sales]),
        ALLSELECTED('Table'[Sub Category])
    )

VAR _rank =
    RANKX(
        ALLSELECTED('Table'[Sub Category]),
        CALCULATE(COUNT('Table'[Quantity Q2])) * 1000000 + (1000000 - CALCULATE(SUM('Table'[Sales]))),
        ,
        ASC,
        Dense
    )

RETURN
    IF ( _rank <= [Parameter Value] , _rank)

 

But as you will see from the screenshot, there is now a tie for the first rank becuase the count of quantity and sum of sales are the same, how should we resolve this tie?

 

mark_endicott_0-1750850314494.png

 

Also, you should know that using this layout will stop the parameter logic working because you will always have data to display in the columns. 

 

Using a table is the way to get your parameter logic to work as desired. 

 

 

Is there any way we can have different ranks for same numbers ? like first two 1 should be 1 and 2 rank . Is it achievable in POWER BI . Could you please share the power Bi as well. Thanks !!!

@nish18_1990 - it is only acheivable if you tell me how.

 

For example we could do it on the sub category name, e.g. B is before D so bikes gets 1 and Dining gets 2. 

 

But I don't know if this is what you are trying to acheive. 

 

Also, even if we do this, it still will not resolve the issue of the fact that the matrix with both columns and rows will prevent the parameter logic from working. 

Yes need it by subcategory . Attached screenshot below for eg . On the left side the subcategories are different before count . if you see below 11 is coming twice and rank is same . When i do a top 5 in parameter selection on the Var Rank field ,i am getting more than 5 rows because i have same values. Is there any way to just show top 5 rows only even if the count has same value or ranks . Can we do indexing on rows and restrict in intex is <=5 . 

 

nish18_1990_0-1750924713704.png

 

@nish18_1990 - then you have NO OPTION other than to use a Table visual (not a Matrix). It cannot be a Matrix because a Matrix does not supply the Row context for Sub Category that is needed for the rank. 

 

If you want to break ties by Sub Category YOU NEED TO USE A TABLE. 

 

I have attached a final version of this file. Page 1 has the measure Rank 2 which uses the DAX below to rank in the following order:

 

1. Count of Quantity

2. Sum of sales (to break any ties above)

3. Sub Category A-Z (to further break ties from above)

4. Country A-Z (to break the final ties)

 

VAR _rank =
    RANK (
        DENSE,
        ALLSELECTED ( 'Table'[Sub Category], 'Table'[country] ),
        ORDERBY (
            CALCULATE ( COUNT ( 'Table'[Quantity Q2] ) ), DESC,
            CALCULATE ( SUM ( 'Table'[Sales] ) ), DESC,
            'Table'[Sub Category], ASC,
            'Table'[country], ASC
        ),
        LAST
    )
RETURN
    IF ( _rank <= [Parameter Value], _rank )

 

The table also contains two measures for Count of QTY and Sum of Sales that contain logic to return blank when the rank returns blank. This happens when the parameter value is set to show only a certain number of results. - Which has been your requirement all along, and can only be achieved using a TABLE. 

 

I've now provided this solution multiple times and fine tuned it as much as I can, I'd appreciate it if you could mark it as the solution. 

 

If I answered your question please mark my post as the solution, it helps others with the same challenge find the answer!

what i need is :

 

Count      Var rank

38                1

32                2

11                3

11                4

9                  5

5                  6

5                   7

5                  8

 

 

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

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

June 2025 community update carousel

Fabric Community Update - June 2025

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