Forum Discussion

kemppaik's avatar
kemppaik
Frequent Visitor
7 years ago
Solved

Dynamic Ranking of Top 10 and Others in Matrix

Hello, I’m trying to generating a listing of the Top 10 customers for a selected time range, in addition to a total for “Others”. The time range is a slicer and filters the data. I have been a...
  • jdbuchanan71's avatar
    7 years ago

    Hello kemppaik 

    I have used the following method in a couple of models with success.

    First, we need  table that has the unique list of items you want to rank and an additional row for "Other", we can get that with a simple calculated table.

    Customers = 
    UNION (
        DISTINCT ( 'Details'[Customer Name] ),
        ROW ( "Customer Name", "Other" )
    )

    This table we join back into Details on the [Customer Name] field.

    Then we can write the measure that calcs our TopN customers and other. 

    Top N = 
    VAR Top_N =
        CALCULATETABLE ( Customers, TOPN ( 5, ALL ( Customers ), CALCULATE ( [Total Amount], ALL ( 'Date' ) ) ) )
    RETURN
        IF ( 
            NOT ISFILTERED ( Customers[Customer Name] ), CALCULATE ( [Total Amount], ALL ( Customers ) ),
            IF ( SELECTEDVALUE ( Customers[Customer Name] ) = "Other",
                CALCULATE ( [Total Amount], EXCEPT ( ALL ( Customers ), Top_N ) ),
                CALCULATE ( [Total Amount], INTERSECT ( Customers, Top_N ) )
            )
        )

    Finally we need a measure to do the sorting of our customers since we want the Top customers sorted descending the other on the bottom.

    TopN Sort = 
    IF ( SELECTEDVALUE ( Customers[Customer Name] ) = "Other", 0, [Top N] )

    The sorting part is a bit ugly but it is the only way I know to do it right now.  We add the TopN Sort measure into the matrix, sort the matrix by TopN Sort and collapse all the columns of TopN Sort so you don't see them.  This is all so we get the following:

    I have uploaded my sample .pbix file here Top 5 Other sorted.pbix

    In my example I only did the top 5, you just need to change the highlighted number.

  • kemppaik's avatar
    kemppaik
    7 years ago

    Thanks for the response jdbuchanan71 .  I was able to get this to work.  I tried a Top N calculation before but didn't get this far because I didn't do the Top N calculation correctly - I was making it too complex.