Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
6 years ago
Solved

How to Display top Reasons per category

Sample data

Table Name: Hospital

 

Category | Denial Reaon | Denied Services

A | reason1 | 20

A | reason2 | 30

A | reason3 | 90

A | reason4 | 100

A | reason5 | 10

A | reason6 | 25

B| reason10 | 28

B | reason11 | 26

B| reason12 | 30

B | reason13 | 16

 

 

 

SO my requirement is i have to display latest top values it should be sorted by denied services.

I have parameter top numbers.

Lets say top number =2

output:

 

A | reason2 | 30

A | reason1 | 20

B| reason10 | 28

B | reason11 | 26

 

need to show top latest reasons per services.

 

 

  • Hi Anonymous 

    Create a what-if parameter, use the column in the slicer,

     

    Then create measures

    rank measure =
    RANKX (
        FILTER (
            ALLSELECTED ( 'Table' ),
            'Table'[Category] = MAX ( 'Table'[Category] )
        ),
        CALCULATE ( MAX ( 'Table'[Denial Reaon] ) ),
        ,
        ASC,
        DENSE
    )
    
    flag measure = IF([rank measure]<=[top n Parameter Value],1,0)
    

     

    Add measure in the visual level filter of a table visual,

     

    To make it sort as

    In Edit queries, sort "Category" column on Ascending, then sort "Denied Services" column on Descending,

    next, add an index column

     

    Close&&apply, select "Denial Reaon" column and select "sort by column" as below:

    Best Regards
    Maggie
    Community Support Team _ Maggie Li
    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

2 Replies

  • JarroVGIT's avatar
    JarroVGIT
    Icon for Resident Rockstar rankResident Rockstar

    I think we are missing important bits of your table. Is it indexed? Does it have a date? If it is Date, then does this correctly represent your question: We need the TopN rows per Category by Date, sorted by DeniedServices? (you can replace Date with index, if your table is indexed). 

     

    In this answers, I assume you have your table indexed. If this is not the case, you can add an index column in the Query Editor of your table (Ribbon 'Add Column' -> Index Column). This gives us the following table:

    Table Hospital: note the Index column!

    Next we add an Parameter (and make sure it has 'Enabable load' activated, so it is loaded in the datamodel and we can reference it later on in DAX expressions):

    Parameter with current value of 2.

    Now we are going to make a calculated column in the Hospital table. This column will rank the index column per Category. The formula is;

     

    RankPerCategory = RANKX(FILTER(Hospital, Hospital[Category]=EARLIER(Hospital[Category])), Hospital[Index], , ASC, Dense)

     

    This results in the following table Hospital:

    Note that the Ranking restarts for Category B rows.

    Next step is to create a table derived from the Hospital table, one where we only want the TopN rows per category based on the Rank column. The N in TopN is based on the Parameter. The formula used to create the table and outcome (from the dataview):

     

    FilteredHospitalTableRanked = FILTER(Hospital, Hospital[RankPerCategory] <= VALUES(TopNParam[TopNParam]))

     

     

    EDIT: i forgot the part about the last sort. In the table FilteredHospitalTableRanked, we are going to do some cool stuff now; we are going to rank the rows based on two columns! First we are going to rank it based on the Category (so that A is always grouped an clustered before rows of B start) and then we are going to rank based on DeniedServices in that category. We are going to do this by creating yet another calculated column, this time in the FilteredHospitalTableRanked table:

    Final Rank = 
    RANKX (
        ALL ( FilteredHospitalTableRanked ),
        RANKX ( ALL ( FilteredHospitalTableRanked ), [Category],, DESC )
            + DIVIDE (
                RANKX ( ALL ( FilteredHospitalTableRanked ), [DeniedServices],, ASC ),
                ( COUNTROWS ( ALL ( FilteredHospitalTableRanked ) ) + 1 )
            ),
    )

    As you can see, we are using three times the RANKX formula. The first one is to create the overall ranking for each row. It ranks based on the (second RANKX) the rank of the category (A=1, B=2) + the normalized rank (meaning, from 0 to 1) of the DeniedServices in that category. The normalized rank is determined by ranking it normaly and then divide the rank by the total number of rows + 1. 

    Putting it all in a table visual looks like this (sorted on 'Final Rank);

    If this helped you, please mark this as the Solution 🙂 Kudo's are welcome!

     

  • v-juanli-msft's avatar
    v-juanli-msft
    Icon for Community Support rankCommunity Support

    Hi Anonymous 

    Create a what-if parameter, use the column in the slicer,

     

    Then create measures

    rank measure =
    RANKX (
        FILTER (
            ALLSELECTED ( 'Table' ),
            'Table'[Category] = MAX ( 'Table'[Category] )
        ),
        CALCULATE ( MAX ( 'Table'[Denial Reaon] ) ),
        ,
        ASC,
        DENSE
    )
    
    flag measure = IF([rank measure]<=[top n Parameter Value],1,0)
    

     

    Add measure in the visual level filter of a table visual,

     

    To make it sort as

    In Edit queries, sort "Category" column on Ascending, then sort "Denied Services" column on Descending,

    next, add an index column

     

    Close&&apply, select "Denial Reaon" column and select "sort by column" as below:

    Best Regards
    Maggie
    Community Support Team _ Maggie Li
    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.