Google Sheets Rank If: How to Rank with Conditions

How-to basics · Updated November 23, 2026

To rank values in Google Sheets based on a condition, use the COUNTIFS function to count how many values are greater than (or less than) the current value within a filtered group, then add 1. For example, to rank sales in column B only for rows where column A equals “East”, enter =COUNTIFS(A$2:A$100, A2, B$2:B$100, ">"&B2)+1 in C2 and drag down.

Why Regular RANK() Fails with Conditions

The built-in RANK function (and its cousins RANK.EQ and RANK.AVG) ranks a number within an entire list. It has no built-in way to restrict the ranking to a subset of rows. If you try to rank only the “East” region sales, RANK will still consider all rows, giving incorrect positions for the filtered group. The workaround is to build a conditional ranking using COUNTIFS, which counts only the rows that meet your criteria.

How the COUNTIFS Ranking Formula Works

The formula =COUNTIFS(A$2:A$100, A2, B$2:B$100, ">"&B2)+1 works in three parts:

  • Range 1 and Criterion 1: A$2:A$100 is the column with your group labels (e.g., region), and A2 is the current row’s group. This restricts counting to rows in the same group.
  • Range 2 and Criterion 2: B$2:B$100 is the column with values to rank, and ">"&B2 counts rows in that group where the value is greater than the current row’s value.
  • +1: Adding 1 converts the count of greater values into a rank. If 3 values are greater, the current value is the 4th largest, so rank = 3 + 1 = 4.

For descending ranking (largest = 1), use ">". For ascending ranking (smallest = 1), use "<" instead.

Step-by-Step Example with Real Data

Assume you have a sheet with:

  • A1: Region, B1: Sales
  • A2:A10: East, West, East, North, East, West, North, East, West
  • B2:B10: 100, 200, 150, 300, 120, 250, 180, 90, 210

In C2, enter =COUNTIFS(A$2:A$10, A2, B$2:B$10, ">"&B2)+1 and drag down to C10. The results:

  • Row 2 (East, 100): East values are 100, 150, 120, 90. Greater than 100: 150, 120 → count 2, rank 3.
  • Row 3 (West, 200): West values are 200, 250, 210. Greater than 200: 250, 210 → count 2, rank 3.
  • Row 4 (East, 150): Greater than 150: none (only 100, 120, 90 are lower) → count 0, rank 1.
  • Row 5 (North, 300): North values are 300, 180. Greater than 300: none → count 0, rank 1.
  • Row 6 (East, 120): Greater than 120: 150 → count 1, rank 2.
  • Row 7 (West, 250): Greater than 250: none → count 0, rank 1.
  • Row 8 (North, 180): Greater than 180: 300 → count 1, rank 2.
  • Row 9 (East, 90): Greater than 90: 100, 150, 120 → count 3, rank 4.
  • Row 10 (West, 210): Greater than 210: 250 → count 1, rank 2.

This gives a per-group rank where 1 is the highest sales in that region.

Handling Ties and Duplicate Values

The COUNTIFS method gives tied values the same rank. For example, if two East rows both have 150, the formula counts how many values are greater than 150 (say 1), so both get rank 2, and the next value (120) gets rank 4 (since 3 values are greater: 150, 150, 100). This is equivalent to RANK.EQ behavior. If you want average ranks for ties (like RANK.AVG), use a more complex array formula: =ARRAYFORMULA(IF(A2:A="",,COUNTIFS(A2:A, A2:A, B2:B, ">"&B2:B) + (COUNTIFS(A2:A, A2:A, B2:B, B2:B)+1)/2)) entered in C2. This adds half the count of ties to the base rank.

Using FILTER and RANK for a Cleaner Alternative

If you prefer a two-step approach, you can create a helper column that extracts the group values and ranks them. For example, in D2 enter =IF(A2="East", RANK(B2, FILTER(B$2:B$100, A$2:A$100="East")), ""). This uses FILTER to get only the East sales, then RANK ranks the current value within that filtered array. Drag down for each row. This works but repeats the filter for every row, which can be slower on large datasets. The COUNTIFS approach is generally faster and more compact.

Ranking with Multiple Conditions

You can extend COUNTIFS to rank within multiple criteria. For instance, rank sales within each region AND product type. If column A is Region, column B is Product, and column C is Sales, use =COUNTIFS(A$2:A$100, A2, B$2:B$100, B2, C$2:C$100, ">"&C2)+1. This counts rows where both the region and product match, and the sales are greater. The same logic applies: add more criteria pairs as needed.

Common Mistakes and Troubleshooting

  • Absolute vs. relative references: Use $ to lock the range (e.g., A$2:A$100) but leave the current cell reference relative (e.g., A2). If you lock both, every row will use the same comparison value.
  • Text vs. number criteria: If your group labels are text, use quotes: "East". If they are numbers, no quotes needed. The comparison operator for values must be in quotes and concatenated with &.
  • Blank cells: COUNTIFS ignores blank cells in the range, but if your value column has blanks, they won't be counted, which can affect ranks. Consider using IF to handle blanks, e.g., =IF(B2="","",COUNTIFS(...)+1).
  • Sorting: The formula works regardless of row order. You don't need to sort the data first.
  • Array formulas: If you use ARRAYFORMULA, ensure the ranges match in size. For a single formula in C2, use =ARRAYFORMULA(IF(A2:A="",,COUNTIFS(A2:A, A2:A, B2:B, ">"&B2:B)+1)) and it will fill down automatically.

Frequently Asked Questions

Can I use RANKIF in Google Sheets?

There is no dedicated RANKIF function in Google Sheets. The recommended workaround is to use COUNTIFS as described, or combine FILTER with RANK. Excel also lacks RANKIF; the same COUNTIFS method works there.

How do I rank in descending order (largest value = 1)?

Use the greater-than operator: =COUNTIFS(A$2:A$100, A2, B$2:B$100, ">"&B2)+1. For ascending (smallest = 1), use "<" instead.

How do I rank only the top 5 within each group?

You can wrap the formula in an IF to show only ranks 1-5: =IF(COUNTIFS(A$2:A$100, A2, B$2:B$100, ">"&B2)+1<=5, COUNTIFS(...)+1, ""). Or use conditional formatting to highlight rank 1-5.

What if my data has blank rows or errors?

Blank rows are ignored by COUNTIFS, but they may cause the formula to return 1 for a blank value. Add an IF to check for blanks: =IF(B2="","",COUNTIFS(...)+1). If you have errors in the value column, the formula may return 0 or incorrect counts; use IFERROR to handle them.

Does this work with dates or text values?

Yes, the comparison operator works with dates (e.g., ">"&DATE(2024,1,1)) and text (e.g., ">"&"apple"). For text, ranking is alphabetical, with A being highest.

Is there a faster way for large datasets?

For thousands of rows, the COUNTIFS formula is efficient. If you need even faster performance, consider using a pivot table to rank within groups, or use a single ARRAYFORMULA to calculate all rows at once, which reduces recalculation overhead.

Related

Try it: the Date Difference Calculator: days, months, years and weekdays between two dates, with the DATEDIF formulas.

More in How-to basics

Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources

Spotted an error in this guide? Let us know.