How to Rank by Group in Excel With COUNTIFS
By ExcelReady TeamUpdated 6 min read
The short answer
RANK.EQ ranks against the whole list, so for a rank within each group use COUNTIFS. In D2, type =COUNTIFS($A$2:$A$9,A2,$C$2:$C$9,">"&C2)+1 and fill down. It counts how many rows in the same group have a higher value and adds 1. Ties get the same rank, just like RANK.EQ.
Key takeaways
- RANK.EQ can't rank within a group, but COUNTIFS can: count the bigger values in the same group, then add 1.
- The formula is =COUNTIFS(group_range,this_group,value_range,">"&this_value)+1.
- Lock both ranges with $ signs so they stay fixed as you fill down.
- Swap ">" for "<" to rank the smallest value first, such as the fastest time.
- Equal values in a group share a rank, and the next rank is skipped.
On this page+
To rank by group in Excel, use COUNTIFS instead of RANK.EQ: count how many rows in the same group have a higher value, then add 1. The formula =COUNTIFS($A$2:$A$9,A2,$C$2:$C$9,">"&C2)+1 gives each rep a rank inside their own region, with ties sharing a rank. This guide is for anyone who needs "top seller per region" or "fastest per team" rankings, and it works in every version from Excel 2007 on.
Why RANK.EQ can't rank by group#
RANK.EQ compares a number against one whole range, and it has no argument for a condition. So =RANK.EQ(C2,$C$2:$C$9) ranks Kofi against every rep, not just the reps in his region.
All examples use this sales table in A1:C9:
| A | B | C | |
|---|---|---|---|
| 1 | Region | Rep | Sales |
| 2 | North | Kofi | 5,200 |
| 3 | South | Mei | 6,100 |
| 4 | North | Ravi | 4,800 |
| 5 | South | Ana | 7,300 |
| 6 | North | Lena | 6,100 |
| 7 | South | Tom | 5,900 |
| 8 | North | Sam | 4,800 |
| 9 | South | Ivy | 6,400 |
Overall, Kofi ranks 6th: five reps sold more. Inside North, he ranks 2nd, behind only Lena. That second number is what most reports need. The guide to the RANK function in Excel covers the overall rank.
How to rank by group with COUNTIFS#
COUNTIFS counts rows that meet every condition you give it. To rank within a group, give it two conditions: the same group, and a higher value. In D2 type:
=COUNTIFS($A$2:$A$9,A2,$C$2:$C$9,">"&C2)+1Then fill down to D9 (Windows: Ctrl+D; Mac: ⌘D). Here is what each part does:
| Part | Meaning |
|---|---|
$A$2:$A$9,A2 | Only count rows in the same region as this row |
$C$2:$C$9,">"&C2 | Only count rows with higher sales than this row |
+1 | Nobody higher means rank 1, one person higher means rank 2 |
The & joins the operator ">" to the value in C2, so for Kofi the condition becomes ">5200". Only Lena (6,100) is in North with more, so the count is 1 and the rank is 2. The COUNTIFS function guide explains the range and criteria pairs.
The $ signs lock both ranges, so they stay on rows 2 to 9 as you fill down. A2 and C2 have no $, so they move to A3 and C3 on the next row.
The result: ranks within each region#
After filling down, each region has its own 1, 2, 3:
| Region | Rep | Sales | Rank in region |
|---|---|---|---|
| North | Kofi | 5,200 | 2 |
| South | Mei | 6,100 | 3 |
| North | Ravi | 4,800 | 3 |
| South | Ana | 7,300 | 1 |
| North | Lena | 6,100 | 1 |
| South | Tom | 5,900 | 4 |
| North | Sam | 4,800 | 3 |
| South | Ivy | 6,400 | 2 |
The rows don't need to be sorted: COUNTIFS checks the whole range each time. Notice that Mei and Lena both sold 6,100 but are ranked separately, because they are in different regions.
Ravi and Sam tie on 4,800 in North, so both get rank 3. There is no rank 4 in North, which matches how RANK.EQ treats ties. The comparison of RANK.EQ and RANK.AVG explains the other way to handle ties.
How to rank smallest first within a group#
For times, costs or error counts, the smallest value should be rank 1. Change the operator from greater than to less than:
=COUNTIFS($A$2:$A$9,A2,$C$2:$C$9,"<"&C2)+1Now Ravi and Sam (4,800) share rank 1 in North, and Lena (6,100) becomes rank 4.
How to break ties within a group#
To give every row a different rank, add a second COUNTIFS that counts only the rows above the current one with the same group and value:
=COUNTIFS($A$2:$A$9,A2,$C$2:$C$9,">"&C2)+COUNTIFS($A$2:A2,A2,$C$2:C2,C2)The second part uses an expanding range: $A$2:A2 grows to $A$2:A3, $A$2:A4 and so on as you fill down. It always counts the current row, so it replaces the +1, and it adds one more for each earlier tie. Ravi keeps rank 3 and Sam, who comes later in the list, gets 4. Use this only when a unique rank is truly needed, such as picking one winner, and tell readers how ties were broken.
How to find the top values in each group#
Sometimes you need the value, not the rank: "what are the best and second-best sales in North?" MAXIFS gives the top one in Excel 2019 or later:
=MAXIFS(C2:C9,A2:A9,"North")The result is 6,100. For the 2nd, 3rd or nth value, combine LARGE with FILTER in Excel 2021 or Microsoft 365:
=LARGE(FILTER(C2:C9,A2:A9="South"),2)This returns 6,400, the second-highest South sale. In older versions, =LARGE(IF(A2:A9="South",C2:C9),2) does the same, but you must confirm it with Ctrl+Shift+Enter as an array formula. SMALL works the same way for the lowest values.
If you need group averages next to the ranks, the guide to averaging by group uses the same conditions with AVERAGEIFS, and COUNTIFS and SUMIFS with multiple conditions shows how to add a third condition, such as a month.
Common mistakes when ranking by group#
Check these when the ranks look wrong:
- Missing
$signs. Without them, the ranges shift as you fill down and later rows skip part of the data. - The operator outside the quotes. Write
">"&C2, not>C2or">C2". Inside quotes, C2 is just text. - Forgetting the +1. Without it, the top rep in each group gets rank 0.
- Extra spaces in group names. "North " with a trailing space is a different group from "North". Clean the column with TRIM.
- Numbers stored as text. Text values are not counted as greater or smaller. Convert them with VALUE first.
Practise it#
Ranking by group is a classic job-test question because it combines COUNTIFS, locked ranges and a little logic. Practise COUNTIFS and SUMIFS in a live spreadsheet, then practise LARGE and SMALL for top and bottom values: you type the formula and it checks it instantly. Or try the exercise on this page and rank Kofi within his region.
Try it yourself
This is a live spreadsheet. Type the formula in the green cell and press Enter.
Rank each rep's sales within their region. In D2, write a COUNTIFS formula that counts rows in the same region (A2:A9) with higher sales (C2:C9) than C2, then add 1. Lock the ranges with $ signs and press Enter.
| A | B | C | D | E | |
|---|---|---|---|---|---|
| 1 | Rank in region | ||||
| 2 | |||||
| 3 | |||||
| 4 | |||||
| 5 | |||||
| 6 | |||||
| 7 | |||||
| 8 | |||||
| 9 | |||||
| 10 | |||||
| 11 | |||||
| 12 |
Tip: Two pairs: the region range with A2, and the sales range with ">"&C2. Then add 1 after the closing bracket.
Practise this in a lesson
Frequently asked questions
How do I rank within a group in Excel?
Use COUNTIFS: =COUNTIFS($A$2:$A$9,A2,$C$2:$C$9,">"&C2)+1. It counts the rows in the same group with a higher value and adds 1, giving the rank inside that group.
Can RANK.EQ rank by group?
No. RANK.EQ ranks a number against one whole range and has no condition argument. Use COUNTIFS for a rank within each group, or sort the data by group and rank each block separately.
How do I rank by group from smallest to largest?
Change the operator to less than: =COUNTIFS($A$2:$A$9,A2,$C$2:$C$9,"<"&C2)+1. The smallest value in each group then gets rank 1.
How do I rank by group without ties?
Add a second COUNTIFS that counts earlier rows with the same group and value: =COUNTIFS($A$2:$A$9,A2,$C$2:$C$9,">"&C2)+COUNTIFS($A$2:A2,A2,$C$2:C2,C2). The first of two equal values gets the higher rank.
Does the data need to be sorted to rank by group?
No. The COUNTIFS formula looks at every row in the range, so groups can be mixed in any order.