Rank With Multiple Criteria

Lewzerrrr

Active Member
Joined
Jan 18, 2017
Messages
256
Hi,

This has been asked a lot but I can't adapt solutions to my needs.

I need to rank by brand the top & bottom variance but only if the sales & plan <> 0.

Sample data below with expected results

BrandSalesPlanVarTop RankBottom Rank
TS7,3746,2871,087212
TS15,84916,798-94986
TM3,0853,549-46436
TM9,49811,716-2,21872
TS3,6664,165-499410
TS5,0266,091-1,06595
TS12,04212,987-94577
TS4,0595,677-1,618113
TM2,2944,121-1,82763
TS14,60315,349-74668
TM000--
TM8421,219-37718
TM000--
TS11,81413,996-2,182122
TS6,9307,996-1,066104
TS000--
TM3,3745,832-2,45881
TS13,09112,184907311
TS41,37540,0401,335113
TM5,8026,408-60645
TM4,3254,702-37727
TM2,0822,830-74854
TS21,47022,101-63159
TS7,11512,438-5,323131

<colgroup><col width="64" span="6" style="width:48pt"> </colgroup><tbody>
</tbody>
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Turn your table into a pivot table. Then, in the 'Rows' section, add Brand and Var. Then add var to the 'Values' section. Click on 'Field Value Settings' and go to 'Show Values As' and select 'Rank Largest to Smallest'. This is your 'Top Rank'. Add Var to 'Values' again, only this time select 'Rank Smallest to Largest', which is your bottom rank. Then, on your pivot table under 'Row Filters', in the 'Select Field' dropdown, select 'Var' and filter out your zero values.
 
Upvote 0
Turn your table into a pivot table. Then, in the 'Rows' section, add Brand and Var. Then add var to the 'Values' section. Click on 'Field Value Settings' and go to 'Show Values As' and select 'Rank Largest to Smallest'. This is your 'Top Rank'. Add Var to 'Values' again, only this time select 'Rank Smallest to Largest', which is your bottom rank. Then, on your pivot table under 'Row Filters', in the 'Select Field' dropdown, select 'Var' and filter out your zero values.

Good solution however I need a formula as I can't change the table format unfortunately
 
Upvote 0
Try this.

Top Rank formula: =IF(D2=0,"",SUMPRODUCT((($A$2:$A$25=A2)*($D$2:$D$25<>0))*(D2<$D$2:$D$25))+1)
Bottom Rank formula: =IF(D2=0,"",SUMPRODUCT((($A$2:$A$25=A2)*($D$2:$D$25<>0))*(D2>$D$2:$D$25))+1)
 
Upvote 0
Try this.

Top Rank formula: =IF(D2=0,"",SUMPRODUCT((($A$2:$A$25=A2)*($D$2:$D$25<>0))*(D2<$D$2:$D$25))+1)
Bottom Rank formula: =IF(D2=0,"",SUMPRODUCT((($A$2:$A$25=A2)*($D$2:$D$25<>0))*(D2>$D$2:$D$25))+1)

This would blank out if sales & plan was populated but matched together.

I've created this code to get the top ranks however stumbling on bottom as I'd need to find the bottom number to work back to 1.

Code:
Sub RankTop()    
    MyLR = Cells(Rows.Count, 2).End(xlUp).Row
    
    Range("B2:G" & MyLR).Sort key1:=[G2], Order1:=xlDescending, Header:=xlNo '' sort variance vs plan to get highest
    Range("B2:G" & MyLR).Sort key1:=[C2], Order1:=xlDescending, Header:=xlNo '' sort brands to get TS to TM
       
    I = 1
    
        For J = 2 To MyLR
        
        If Cells(J, 3) <> Cells(J - 1, 3) Then I = 1 '' Re-rank from 1 when it finds TM
        
            If Cells(J, 5) = 0 And Cells(J, 6) = 0 Then GoTo NextIteration '' Skips over where sales and plan are both zero
            
                Cells(J, 8) = I
            
            I = I + 1
            
NextIteration:


        Next J
        
End Sub
 
Upvote 0
This would blank out if sales & plan was populated but matched together.

I'm not sure what you mean by this. But, looking at your code, it seems like the data isn't in the same place. My formulas made the assumption that your data started in A1, so maybe changing the references would work. I pasted the data you had in the original post in A1 and the formulas produced the same results that you posted as your desired results.
 
Upvote 0
I'm not sure what you mean by this. But, looking at your code, it seems like the data isn't in the same place. My formulas made the assumption that your data started in A1, so maybe changing the references would work. I pasted the data you had in the original post in A1 and the formulas produced the same results that you posted as your desired results.

So let's say there was a sales of 1,000 and also a plan of 1,000 giving a variance of 0. I wouldn't want this to be blanked out which it would by your formula. I only want to blank if sales and plan are both zero.
 
Upvote 0
I see. Try these formulas.

Top Rank: =IF(AND(B2=0,C2=0),"",SUMPRODUCT((($A$2:$A$25=A2)*(($B$2:$B$25<>0)*($C$2:$C$25<>0)))*((B2-C2)<($B$2:$B$25-$C$2:$C$25)))+1)
Bottom Rank: =IF(AND(B2=0,C2=0),"",SUMPRODUCT((($A$2:$A$25=A2)*(($B$2:$B$25<>0)*($C$2:$C$25<>0)))*((B2-C2)>($B$2:$B$25-$C$2:$C$25)))+1)
 
Upvote 0
I see. Try these formulas.

Top Rank: =IF(AND(B2=0,C2=0),"",SUMPRODUCT((($A$2:$A$25=A2)*(($B$2:$B$25<>0)*($C$2:$C$25<>0)))*((B2-C2)<($B$2:$B$25-$C$2:$C$25)))+1)
Bottom Rank: =IF(AND(B2=0,C2=0),"",SUMPRODUCT((($A$2:$A$25=A2)*(($B$2:$B$25<>0)*($C$2:$C$25<>0)))*((B2-C2)>($B$2:$B$25-$C$2:$C$25)))+1)

Amazing, thank you! Last thing if you can, I'd need it to rank unique instead of duplicating

For example if there's 2 zero variance then go 1, 2 as this formula goes 1, 1
 
Upvote 0
Let me know if these work.

Top: =IF(AND(B2=0,C2=0),"",(SUMPRODUCT((($A$2:$A$25=A2)*(($B$2:$B$25<>0)*($C$2:$C$25<>0)))*((B2-C2)<($B$2:$B$25-$C$2:$C$25)))+1)+COUNTIFS($A$2:A2,A2,$D$2:D2,D2)-1)

Bottom: =IF(AND(B2=0,C2=0),"",(SUMPRODUCT((($A$2:$A$25=A2)*(($B$2:$B$25<>0)*($C$2:$C$25<>0)))*((B2-C2)>($B$2:$B$25-$C$2:$C$25)))+1)+COUNTIFS($A$2:A2,A2,$D$2:D2,D2)-1)
 
Upvote 0

Forum statistics

Threads
1,215,338
Messages
6,124,358
Members
449,155
Latest member
ravioli44

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top