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

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Request now

Reply
Nikolainoergard
Frequent Visitor

Top N and others in calculation group

Hi community

 

I have followed this great material by SQLBI to do a dynamic TopN & Others solution : https://www.sqlbi.com/tv/implementing-the-top-n-and-others-pattern-using-calculation-groups-in-dax-u...

 

I have a issue with incorrect Ranking. I can't figure out why.. Any ideas what to do? In some filter cases it return correct ranking, but mostly incorrect.

 

Nikolainoergard_3-1713865781295.png

 

Relevant measures below:

Ranking = 
IF (
    ISINSCOPE ( 'Customer Names'[Customer_Name] ),
    VAR CustomersToRank = [TopN Value]
    VAR MeasureForRanking = SELECTEDVALUE ( 'Ranking measure'[Measure to use] )
    VAR Val = [ValueForRanking]
    VAR IsOtherSelected = SELECTEDVALUE ( 'Customer Names'[Customer_Name] ) = "Others"
    RETURN
        IF (
            IsOtherSelected,
            -- Rank for Others
            CustomersToRank + 1,
            
            -- Rank for regular Customers
            IF (
                Val > 0,
                VAR VisibleCustomers =
                    CALCULATETABLE (
                        VALUES ( 'pbi DimCustomer' ),
                        ALLSELECTED ( 'Customer Names' )
                    )
                VAR Ranking =
                    RANKX ( VisibleCustomers, [ValueForRanking], Val )
                RETURN
                    IF ( Ranking > 0 && Ranking <= CustomersToRank, Ranking )
            )
        )
)

 

ValueForRanking = 
VAR MeasureForRanking =
    SELECTEDVALUE ( 'Ranking measure'[Measure to use] )
VAR Val =
    SWITCH (
        TRUE,
        MeasureForRanking = "Sales", [Sales],
        MeasureForRanking = "Order intake", [Order intake],
        MeasureForRanking = "Order bank", [Order bank],
        [Sales]
    )
RETURN
    Val

 

Calculation item:

IF (
    NOT ISSELECTEDMEASURE ( [Ranking] ),
    VAR ValueOfAll =
        CALCULATE ( SELECTEDMEASURE (), REMOVEFILTERS ( 'Customer Names' ) )
    RETURN
        IF (
            NOT ISINSCOPE ( 'Customer Names'[Customer_Name] ),
            -- Calculation for a group of products
            ValueOfAll,
            -- Calculation for one product name
            VAR CustomersToRank = [TopN Value]
            VAR ValueOfCurrentCustomer =
                SELECTEDMEASURE ()
            VAR IsOtherSelected =
                SELECTEDVALUE ( 'Customer Names'[Customer_Name] ) = "Others"
            RETURN
                IF (
                    NOT IsOtherSelected,
                    -- Calculation for a regular product
                    VAR Ranking = [Ranking]
                    VAR Result =
                        IF (
                            NOT ISBLANK ( Ranking ),
                            ( Ranking <= CustomersToRank ) - ( Ranking = CustomersToRank + 1 )
                        )
                    RETURN
                        IF ( Result, ValueOfCurrentCustomer ),
                    -- Calculation for Others
                    VAR VisibleCustomers =
                        CALCULATETABLE (
                            VALUES ( 'pbi DimCustomer' ),
                            ALLSELECTED ( 'Customer Names'[Customer_Name] )
                        )
                    VAR CustomersWithValues =
                        ADDCOLUMNS ( 
                            VisibleCustomers, 
                            "@ValForRanking", [ValueForRanking],
                            "@SelMeasureAmount", SELECTEDMEASURE ()
                        )
                    VAR ValueOfTopCustomers =
                        SUMX (
                            TOPN ( CustomersToRank, CustomersWithValues, [@ValForRanking] ),
                            [@SelMeasureAmount]
                        )
                    VAR ValueOfOthers = ValueOfAll - ValueOfTopCustomers
                    RETURN
                        ValueOfOthers
                )
        ),
    SELECTEDMEASURE ()
)

 

Would appreicate any ideas/help to fix this

Thanks

3 REPLIES 3
Nikolainoergard
Frequent Visitor

Bump. Please let me know if more information is needed for understanding context.

Anonymous
Not applicable

Hi @Nikolainoergard ,

Based on the description, please try to confirm the filter applied isn’t affecting the ranking measure.

Then, review the Ranking measure formula. Try to use the all dax in the VisibleCustomers variable.

IF (
                Val > 0,
                VAR VisibleCustomers =
                    CALCULATETABLE (
                        VALUES ( 'pbi DimCustomer' ),
                        ALL( 'Customer Names' )
                    )
                VAR Ranking =
                    RANKX ( VisibleCustomers, [ValueForRanking], Val )
                RETURN
                    IF ( Ranking > 0 && Ranking <= CustomersToRank, Ranking )
            )

RANKX function (DAX) - DAX | Microsoft Learn

 

Best Regards,

Wisdom Wu

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

Hi @Anonymous 

Thanks for your message

I have tried diffrent options regarding applied filters.
1) Removing Calc. item currency conversion impact (Using hardcoded measure)

2) Removing Date filter 

3) Using ALL instead of AALSELECTED in Ranking measure


..And unfortunately i still get wrong ranking

Helpful resources

Announcements
November Power BI Update Carousel

Power BI Monthly Update - November 2025

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

Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.