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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
jaak198
New Member

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 8
v-yuezhe-msft
Employee
Employee

@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
            )
        )
)

1.JPG

 

Regards,
Lydia

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

 

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...?

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. 

@jaak198,

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

Regards,
Lydia

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

Or is there a way to do this without needing to create a table? I managed to sum the sales by Village AND customer code so now I have total sales for each customer by village as a separate column. I then tried to reformat your suggested DAX formula to give me the ranking that I need and it almost did the trick (I'm sure I'm doing something wrong)!

 

The problem that I have now is that while it ranks the highest spending customer as no. 1, it ranks the 2nd highest spending as no. 80. I think that's because there are 80 rows of transactions for customer 1. I have tried to add in dense into your suggested DAX in order to get it to change that 80 to 2 but I can't get it to work. Any idea where I should add in the tie breaker of dense? I assume it is dense isn't it?

 

Thanks for all your help so far. Appreciate it. 

 

Ali. 

@jaak198,

We are not able to create new table using DAX in PowerPivot, I recommend you use Power BI Desktop instead, everything works well in Power BI Desktop.

Regards,
Lydia

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

Ok, I will give that a go and see if I can replicate my calculations in Power BI

Hi Lydia,

 

Very strange, I am in Power Pivot but I don't have a "New Table" option available in the toolbar. Perhaps I have to add it in as an option?

 

Ali. 

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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

Top Solution Authors