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
DeepDive
Helper IV
Helper IV

Dynamic Top N Categories and Top N Sub Categories and Top N Brand

Hi All, Am using below Curbal's concept for Top N Categories, but under Categories I have Sub-Categories and under Sub-Categories, I have Brands also. At individual level, all is working fine but when I drill down or expand categories it shows me correct for Sub-categories but for Categories it gives me all categories.

 

Curbal's Video link  :   https://www.youtube.com/watch?v=SsZseKOgrWQ

1 ACCEPTED SOLUTION
v-alq-msft
Community Support
Community Support

Hi, @DeepDive 

 

Based on your description, I created data to reproduce your scenario. The pbix file is attached in the end.

 

Table:

c1.png

 

You may create a hat-if parameter as below.

c2.png

 

Here are the calculated columns and measures:

Calculated column:
Level2 = [Category]&"-"&[Sub Category]
Level3 = [Category]&"-"&[Sub Category]&"-"&[Brand]

Measure:
Visual Control = 
var _categorytopn = 
SELECTCOLUMNS(
    TOPN(
        SELECTEDVALUE(Parameter[Parameter]),
        SUMMARIZE(
        ALL('Table'),
        'Table'[Category],
        "Sales",SUM('Table'[Sales])
        ),
        [Sales]
    ),
    "Category",
    [Category]
)
var _subcategorytopn = 
SELECTCOLUMNS(
    TOPN(
        SELECTEDVALUE(Parameter[Parameter]), 
        SUMMARIZE(
            FILTER(
               ALL('Table'),
               'Table'[Category]=SELECTEDVALUE('Table'[Category])
            ),
            'Table'[Category],
            'Table'[Sub Category],
            "Sales",SUM('Table'[Sales])
        ),
        [Sales]
    ),
    "Level2",
    [Category]&"-"&[Sub Category]
)
var _brandtopn = 
SELECTCOLUMNS(
    TOPN(
        SELECTEDVALUE(Parameter[Parameter]),
            SUMMARIZE(
                FILTER(
                    ALL('Table'),
                    'Table'[Category]=SELECTEDVALUE('Table'[Category])&&
                    'Table'[Sub Category]=SELECTEDVALUE('Table'[Sub Category])
                ),
                'Table'[Category],
                'Table'[Sub Category],
                'Table'[Brand],
                "Sales",SUM('Table'[Sales])
            ),     
        [Sales]
    ),
    "Level3",
    [Category]&"-"&[Sub Category]&"-"&[Brand]
)

return
IF(
    ISINSCOPE('Table'[Category])&&NOT(ISINSCOPE('Table'[Sub Category]))&&NOT(ISINSCOPE('Table'[Brand])),
    IF(
        SELECTEDVALUE('Table'[Category]) in _categorytopn,
        1,0
    ),
    IF(
        ISINSCOPE('Table'[Sub Category])&&NOT(ISINSCOPE('Table'[Brand])),
        IF(      
            SELECTEDVALUE('Table'[Category]) in _categorytopn&&
            SELECTEDVALUE('Table'[Level2]) in _subcategorytopn,
            1,0
        ),
        IF(
            ISINSCOPE('Table'[Brand]),
            IF(
                SELECTEDVALUE('Table'[Category]) in _categorytopn&&
                SELECTEDVALUE('Table'[Level2]) in _subcategorytopn&&
                SELECTEDVALUE('Table'[Level3]) in _brandtopn,
                1,0
            )
        )
    )
)

 

Finally you may put the measure in the visual level filter and use the parameter to filter the topn result. Because the maximum count for 'Brand' is 2, there are only two brand in 'Brand' level even though the slicer is set as 3. It depends on your data.

c3.png

 

c4.png

 

c5.png

 

Best Regards

Allan

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

 

View solution in original post

4 REPLIES 4
v-alq-msft
Community Support
Community Support

Hi, @DeepDive 

 

Based on your description, I created data to reproduce your scenario. The pbix file is attached in the end.

 

Table:

c1.png

 

You may create a hat-if parameter as below.

c2.png

 

Here are the calculated columns and measures:

Calculated column:
Level2 = [Category]&"-"&[Sub Category]
Level3 = [Category]&"-"&[Sub Category]&"-"&[Brand]

Measure:
Visual Control = 
var _categorytopn = 
SELECTCOLUMNS(
    TOPN(
        SELECTEDVALUE(Parameter[Parameter]),
        SUMMARIZE(
        ALL('Table'),
        'Table'[Category],
        "Sales",SUM('Table'[Sales])
        ),
        [Sales]
    ),
    "Category",
    [Category]
)
var _subcategorytopn = 
SELECTCOLUMNS(
    TOPN(
        SELECTEDVALUE(Parameter[Parameter]), 
        SUMMARIZE(
            FILTER(
               ALL('Table'),
               'Table'[Category]=SELECTEDVALUE('Table'[Category])
            ),
            'Table'[Category],
            'Table'[Sub Category],
            "Sales",SUM('Table'[Sales])
        ),
        [Sales]
    ),
    "Level2",
    [Category]&"-"&[Sub Category]
)
var _brandtopn = 
SELECTCOLUMNS(
    TOPN(
        SELECTEDVALUE(Parameter[Parameter]),
            SUMMARIZE(
                FILTER(
                    ALL('Table'),
                    'Table'[Category]=SELECTEDVALUE('Table'[Category])&&
                    'Table'[Sub Category]=SELECTEDVALUE('Table'[Sub Category])
                ),
                'Table'[Category],
                'Table'[Sub Category],
                'Table'[Brand],
                "Sales",SUM('Table'[Sales])
            ),     
        [Sales]
    ),
    "Level3",
    [Category]&"-"&[Sub Category]&"-"&[Brand]
)

return
IF(
    ISINSCOPE('Table'[Category])&&NOT(ISINSCOPE('Table'[Sub Category]))&&NOT(ISINSCOPE('Table'[Brand])),
    IF(
        SELECTEDVALUE('Table'[Category]) in _categorytopn,
        1,0
    ),
    IF(
        ISINSCOPE('Table'[Sub Category])&&NOT(ISINSCOPE('Table'[Brand])),
        IF(      
            SELECTEDVALUE('Table'[Category]) in _categorytopn&&
            SELECTEDVALUE('Table'[Level2]) in _subcategorytopn,
            1,0
        ),
        IF(
            ISINSCOPE('Table'[Brand]),
            IF(
                SELECTEDVALUE('Table'[Category]) in _categorytopn&&
                SELECTEDVALUE('Table'[Level2]) in _subcategorytopn&&
                SELECTEDVALUE('Table'[Level3]) in _brandtopn,
                1,0
            )
        )
    )
)

 

Finally you may put the measure in the visual level filter and use the parameter to filter the topn result. Because the maximum count for 'Brand' is 2, there are only two brand in 'Brand' level even though the slicer is set as 3. It depends on your data.

c3.png

 

c4.png

 

c5.png

 

Best Regards

Allan

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

 

Hi @v-alq-msft , Thanks a lot.

amitchandak
Super User
Super User

@DeepDive , refer if this can help

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

 

For Rank Refer these links
https://radacad.com/how-to-use-rankx-in-dax-part-2-of-3-calculated-measures
https://radacad.com/how-to-use-rankx-in-dax-part-1-of-3-calculated-columns
https://radacad.com/how-to-use-rankx-in-dax-part-3-of-3-the-finale
https://community.powerbi.com/t5/Community-Blog/Dynamic-TopN-made-easy-with-What-If-Parameter/ba-p/3...

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

Hi Amit, thanks for your reply.

 

My requirement is to show YTD at all levels only for selected top N using one slicer only....

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!

Jan25PBI_Carousel

Power BI Monthly Update - January 2025

Check out the January 2025 Power BI update to learn about new features in Reporting, Modeling, and Data Connectivity.

Jan NL Carousel

Fabric Community Update - January 2025

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