Forum Discussion

jaak198's avatar
jaak198
New Member
8 years ago

Ranking by multiple columns

Hi, 

 

Below is a dummy table which summarises the data I am using. 

 

VillageCustomer codeSpendTotal spend by CustomerRank
A10110015001
A10120015001
A10130015001
A10140015001
A10150015001
A1021007002
A1021507002
A1022007002
A1022507002
A1031001005
A1042002004
A1053003003
A1063003003
A1073003003
B201110065001
B201120065001
B201130065001
B201140065001
B201150065001
B202110047002
B202115047002
B202120047002
B202125047002
B203110011005
B204120012004
B205130013003
B206130013003
B207130013003

 

Now the problem that I am having is that I want to be able to rank the Customers by their total spend and I need to be able to rank this by Village. I have managed to do this using a combination of RANKX and FILTER, however, the problem I am having is that when the total spend amounts are the same (as for customers 105, 106, 107, 205, 206 & 207) then it gives the customers the same rank. I want it to give those customers a different rank. If I change my rank setting from "dense" to "skip" then that screws up my ranking at the top with all the rank 1's. 

 

Ideally what I want is to be able to rank by total spend, by village, and when the spend amounts are the same to then use the customer code as the tie breaker. I don't care which of the ties get ranked above the other but just that they are given a different rank.

 

I have see a couple of solutions using RANKX and DIVIDE however I couldn't get them to work to give me the correct results. 

 

Apologies if such topics have already been covered but I am a "newbie".

 

Ali 

8 Replies

  • Anonymous's avatar
    Anonymous
    Not applicable

    jaak198,

    Create a new table using the DAX below.

    Table = SUMMARIZE(Table1,Table1[Village],Table1[Customer code],"Total spend by customer",SUM(Table1[Spend]))

    Then create a new column using dax below in the new table.

    Rank = 
    RANKX (
        FILTER (
            'Table',
            'Table'[Village] = EARLIER ( 'Table'[Village] )
        ),
        RANKX (
            FILTER (
                'Table',
                'Table'[Village] = EARLIER ( 'Table'[Village] )
            ),
            'Table'[Total spend by Customer],
            ,
            ASC
        )
            + DIVIDE (
                RANKX (
                    FILTER (
                        'Table',
                        'Table'[Village] = EARLIER ( 'Table'[Village] )
                    ),
                    'Table'[Customer code],
                    ,
                    ASC
                ),
                (
                    COUNTROWS (
                        FILTER (
                            'Table',
                            'Table'[Village] = EARLIER ( 'Table'[Village] )
                        )
                    )
                        + 1
                )
            )
    )

     

    Regards,
    Lydia

    • jaak198's avatar
      jaak198
      New Member

      Hi Lydia, 

       

      Apologies for my ignorance in this matter but I am just starting off however how do I actually create a new table?

       

      Ali. 

      • Anonymous's avatar
        Anonymous
        Not applicable

        jaak198,

        Click "New Table" as shown in the screenshot below, then apply my first formula.


        Regards,
        Lydia

    • Fredrik_W's avatar
      Fredrik_W
      New Member

       

      I got this to work with my multi-column ranking, but I also want the SKIP-feature to work so that when two or more values are the same (i.e. same tie), they get the same ranking value and the next value gets the next ranking when counted for the previous rows. In the example, Customers 105, 106 and 107 in Village A should all be ranked 3, and Customer 104 and 103 ranked 6 and 7 respectively. I'm a rookie in Power BI and can't really see why just adding "SKIP" as an argument in the right place in the first RANKX  doesn't solves this matter (though I have a feeling it's because the complexity in the RANKX-formula). Is there a way...?