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

Don't miss out! 2025 Microsoft Fabric Community Conference, March 31 - April 2, Las Vegas, Nevada. Use code MSCUST for a $150 discount. Prices go up February 11th. Register now.

Reply
rashmi_panicker
Regular Visitor

Nested Top N using Power BI DAX

Hello All,
 
I am working on a Power BI requirement where I have Sales for say 10 categories and within each category I have approximately 15-20 products. The requirement is to display 5 bar graphs for each of the top 5 categories and within each bar graph, I have to display top 10 products. I tried Ranking following the the example here - https://www.sqlbi.com/articles/filtering-the-top-3-products-for-each-category-in-power-bi/
 
But, when I try to filter the rank column, I don't get proper result.
 
Also, I tried using the Top N for the Top 10 products but not sure how to come up with a nested top N where I can display data separately like Top 10 products for top 1st category then Top 10 products for top 2nd category and so on till Top 10 products for top 5th category.
 
Please let me know if anyone has worked on similar requirement.
 
Thanks.
 
1 ACCEPTED SOLUTION

Hi, @rashmi_panicker 

 

Because the context of the matrix is complex, and the value of measure changes according to the results in the filter pane, it is not possible to simply filter.

With my efforts, I made this perfect solution, hope you can give me a kudos.

 

vjaneygmsft_0-1645181523972.png

Total rank measure:

Rank = 
IF (

    ISINSCOPE ( 'Table'[Category] ),
        RANKX (
            FILTER (
                ALL ( 'Table' ),
                [Manufacturer] = SELECTEDVALUE ( 'Table'[Manufacturer] )
            ),
            CALCULATE ( MAX ( 'Table'[Sales] ) )
        )
    
    ,
    IF (
            ISINSCOPE ( 'Table'[Manufacturer] ),
    RANKX (
        SUMMARIZE ( ALL ( 'Table' ), [Manufacturer], "total", SUM ( 'Table'[Sales] ) ),
        CALCULATE ( SUM ( 'Table'[Sales] ) ),
))
        
)


Column for rank category:

rankcategory = RANKX(FILTER(ALL('Table'),[Manufacturer]=EARLIER('Table'[Manufacturer])),[Sales])

vjaneygmsft_1-1645181770865.png

I created a column because I can't use two topns in the filter pane.

Below is my sample. If you don't understand anything, feel free to ask me.

 

Janey

 

View solution in original post

12 REPLIES 12
amitchandak
Super User
Super User

@rashmi_panicker , refer if this blog can help

https://www.sqlbi.com/articles/filtering-the-top-3-products-for-each-category-in-power-bi/

Join us as experts from around the world come together to shape the future of data and AI!
At the Microsoft Analytics Community Conference, global leaders and influential voices are stepping up to share their knowledge and help you master the latest in Microsoft Fabric, Copilot, and Purview.
️ November 12th-14th, 2024
 Online Event
Register Here

Thank you for your response Amit. But, I have already tried this example and have mentioned the same url in my post above. I am able to reach to a point where I am able to rank my Categories and Products properly using this sqlbi.com post. But, when I try to add filter on the Rank column, I don't get the required data.

Hi, @rashmi_panicker 

 

I don't know what your formulas and data look like, it's hard to give you substantial help.

Can you share some sample data and your desired result? So we can help you soon.

 

Janey

rashmi_panicker_0-1644410224380.png

 

In the above figure, I am getting the Rank Multi values using the following  measure. But, I am not able to filter my visual for the 2nd highest Category. This is because if I filter the visual by Rank Multi = 2, it will give me 2nd product from all the categories. I think, if we can somehow bring the values of Rank Multi as entered manually in the pic above, I can filter the categories and display each category in separate visual

 

Sales Unfiltered =
IF (
    ISINSCOPE ( 'Product'[Product Name] ),
    [Sales Amount],
    CALCULATE (
        [Sales Amount],
        ALL ( 'Product'[Product Name] )
    )
)

 

Rank Multi =
IF (
    ISINSCOPE( 'Product'[Product Name] ),
        RANKX (
        CALCULATETABLE (
            VALUES ( 'Product'[Product Name] ),
            ALLSELECTED ( 'Product'[Product Name] )
        ),
        [Sales Amount]
    ),
    IF (
        ISINSCOPE( 'Product'[Category] ),
        RANKX (
            ALLSELECTED ( 'Product'[Category] ),
            [Sales Unfiltered]
        )
    )
)

Hi,  @rashmi_panicker 

 

I have understood your needs. As for your problem, it is because the value of measure will change with the context, and the context of the matrix also involves hierarchy, which is more complex. But measure has no hierarchy in bar chart.

If you can provide a data sample, I can test it for you. According to your needs ,It may need to modify measure or use column. Creating data out of thin air is not my thing.

vjaneygmsft_1-1644478757416.png

Reference:

How to Get Your Question Answered Quickly - Microsoft Power BI Community

 

Janey

Hello Janey,

I am not able to find the option for attaching the source file. PFB the data.

ManufacturerCategorySalesDate
AlphaC11510011-01-2022
AlphaC21380011-01-2022
AlphaC31630012-01-2022
AlphaC41440013-01-2022
AlphaC51820006-01-2022
BetaC91560019-01-2022
BetaC101460020-01-2022
BetaC61290007-01-2022
BetaC11370008-01-2022
BetaC81890007-01-2022
GammaC111830021-01-2022
GammaC81910011-01-2022
GammaC131390011-01-2022
GammaC141210012-01-2022
GammaC151280013-01-2022
ThetaC201480019-01-2022
ThetaC161990006-01-2022
ThetaC171870007-01-2022
ThetaC181650008-01-2022
ThetaC191370007-01-2022

 

For the above data,

 

Totals for Manufacturers

Alpha

77800

Beta

75700

Gamma

76200

Theta

83600

 

Based on the above Total, Theta, Alpha and Gamma are the top 3 Manufacturers.

So, I want 3 graphs on my report for the top 3 Manufacturers. Each showing top 4 categories,

For Theta, C16, C17, C18 & C20 should be displayed on the 1st graph

For Alpha, C5, C3, C1 & C4 should be displayed on the 2nd graph

For Gamma, C8, C11, C13 & C15 should be displayed on the 3rd graph

 

Thank you.

 

Hi, @rashmi_panicker 

 

Because the context of the matrix is complex, and the value of measure changes according to the results in the filter pane, it is not possible to simply filter.

With my efforts, I made this perfect solution, hope you can give me a kudos.

 

vjaneygmsft_0-1645181523972.png

Total rank measure:

Rank = 
IF (

    ISINSCOPE ( 'Table'[Category] ),
        RANKX (
            FILTER (
                ALL ( 'Table' ),
                [Manufacturer] = SELECTEDVALUE ( 'Table'[Manufacturer] )
            ),
            CALCULATE ( MAX ( 'Table'[Sales] ) )
        )
    
    ,
    IF (
            ISINSCOPE ( 'Table'[Manufacturer] ),
    RANKX (
        SUMMARIZE ( ALL ( 'Table' ), [Manufacturer], "total", SUM ( 'Table'[Sales] ) ),
        CALCULATE ( SUM ( 'Table'[Sales] ) ),
))
        
)


Column for rank category:

rankcategory = RANKX(FILTER(ALL('Table'),[Manufacturer]=EARLIER('Table'[Manufacturer])),[Sales])

vjaneygmsft_1-1645181770865.png

I created a column because I can't use two topns in the filter pane.

Below is my sample. If you don't understand anything, feel free to ask me.

 

Janey

 

Thank you Janey for your response. The solution that you provided is perfect but I am not able to go beyond this point. We are able to get the result in a Matrix. Now, how do we split these 3 Manufacturers on 3 different graphs displaying their own respective top 4 categories and their sales?

Hi, @rashmi_panicker 

 

Create two columns for rank and use it in filter pane. Set the rankmanufacturer to 1, 2, and 3 in the three graphs respectively.

rankManufacturer = RANKX (
       SUMMARIZE ( ALL ( 'Table' ), [Manufacturer], "total", SUM ( 'Table'[Sales] ) ),
       SUMX(FILTER('Table',[Manufacturer]=EARLIER('Table'[Manufacturer])),[Sales]))

vjaneygmsft_1-1645408708884.png

 

vjaneygmsft_0-1645408545446.png

 

Janey

 

Hello Janey,

 

Your solution will definitely work in case of the sample shared by me. But, my actual table has many other columns and my report has multiple slicers. When I try to add a column in this table for rank, I am getting the result as 3 & 4 digit numbers instead of 1,2,3, etc. How do I modify the DAX expression for rank column to incorporate this scenario?

Hi, @rashmi_panicker 

 

Due to your special needs, I can only use calculated column, because it doesn't change with the context like measure, but requires more memory. 

What's mean about  'I am getting the result as 3 & 4 digit numbers instead of 1,2,3, etc'? I don't know your real scenario or how you modified my code in your report, how else can I help you?

 

Janey

@rashmi_panicker  I'm too busy today, I'll reply to you next week. Sorry.

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.