Highlight Unique Top N Values in Google Sheets

Published on

We can easily highlight unique top N values in columns or rows in Google Sheets using a combination of the UNIQUE and RANK functions.

Additionally, we can optionally use the COUNTIF function to apply a white fill color to duplicates. Let’s start with an example before moving on to the formulas.

Consider the numbers {1;2;3;4;5;6;7;8;9;10;1;2;3;4;5;6;7;8;9;10} in a column (range B2:B21) in Google Sheets.

If N=5, the unique top N numbers to highlight in B2:B21 will be 10, 9, 8, 7, and 6, not 10, 10, 9, 9, and 8.

Additionally, you may choose whether to apply the fill color to multiple occurrences of the top N numbers. If needed, we can use a COUNTIF rule to exclude duplicates.

Here are examples (screenshots) of single-color highlighting approaches:

  • Unique Top 5 (All Occurrences):
Highlighted Unique Top N Values - All Occurrences
  • Unique Top 5 (First Occurrence Only):
Highlighted Unique Top N Values - First Occurrence

Highlight Unique Top N Values in Columns in Google Sheets

Follow these steps to apply conditional formatting:

  1. Select Format > Conditional formatting.
  2. Under the Single color tab, adjust the settings as follows:
    • Apply to range: B2:B21
    • Format rules: Custom formula is (insert the formula below)
    • Formatting style: Choose a fill color (e.g., Green)
  3. Click Done.

All Occurrences:

=RANK(B2, UNIQUE(B$2:B$21))<=5

If you want to apply this rule to an entire column (B2:B), replace B$2:B$21 with B$2:B.

To highlight the unique top 10 numbers instead, replace 5 with 10.

This formula highlights all occurrences of unique top N values.

First Occurrence:

To highlight only the first occurrence, add another condition:

=AND(RANK(B2, UNIQUE(B$2:B$21))<=5, COUNTIF(B$2:B2, B2)<2)

Highlight Unique Top N Values in Multiple Columns

To highlight the largest unique N numbers in each column in B2:E21, update the “Apply to range” field to B2:E21.

Highlighting the Largest 5 Unique Values in Each Column

Formula Explanation

  • RANK(B2, UNIQUE(B$2:B$21)) ranks the value in B2 among the unique values in B2:B21.
  • If the rank is <=5, the formula returns TRUE, highlighting the top 5 unique values.
  • To eliminate second and later occurrences, COUNTIF(B$2:B2, B2)<2 ensures that only the first occurrence is highlighted.

Highlight Unique Top N Values in Rows in Google Sheets

We have seen how to highlight unique top N values in columns. What about rows?

For row-based highlighting, use Apply to Range as B2:U2 (or B2:U).

Highlighting the Largest 5 Unique Value in a Row

Formula for Unique Top N Values in Rows:

  • All occurrences:=RANK(B2, UNIQUE($B2:$U2, TRUE))<=5
  • First occurrence only:=AND(RANK(B2, UNIQUE($B2:$U2, TRUE))<=5, COUNTIF($B2:B2, B2)<2)

Additional Tips: Highlight Unique Top N Values with Multiple Colors

If you want to differentiate between unique top N values using different colors, follow these steps:

For Top 1, Top 2, Top 3, etc., create separate rules with different colors:

  • Rule 1 (Top 1 Value):=AND(RANK(B2, UNIQUE(B$2:B$21))=1, COUNTIF(B$2:B2, B2)<2)
  • Rule 2 (Top 2 Value):=AND(RANK(B2, UNIQUE(B$2:B$21))=2, COUNTIF(B$2:B2, B2)<2)

Continue adding more rules, changing =1 to =2, =3, etc., and selecting different fill colors.

Largest Unique N Values Highlighted with Multiple Colors in Google Sheets
Prashanth KV
Prashanth KV
Your Trusted Google Sheets and Excel Guide Prashanth KV brings a wealth of experience in Google Sheets and Excel, cultivated through years of work with multinational corporations in Mumbai and Dubai. As a recognized Google Product Expert in Docs Editors, Prashanth shares his expertise through insightful blogging since 2012. Explore his blog for practical tips and guidance on maximizing your spreadsheet skills.

How to Break RANK Ties Alphabetically in Google Sheets

The RANK function in Google Sheets is commonly used to assign rankings to numerical...

Google Sheets: Highlight an Entire Column If Any Cell Has an Error

Google Sheets allows you to highlight an entire column if any cell has an...

Filter Top N per Category in Excel (Using FILTER & COUNTIFS)

You can use a combination of FILTER and COUNTIFS to filter the top N...

Rank per Group in Excel

You have two groups of 20 students each. How do you determine the rank...

More like this

How to Break RANK Ties Alphabetically in Google Sheets

The RANK function in Google Sheets is commonly used to assign rankings to numerical...

Google Sheets: Highlight an Entire Column If Any Cell Has an Error

Google Sheets allows you to highlight an entire column if any cell has an...

Google Sheets: Extract Top N per Group from Query Aggregation

When working with grouped and aggregated data in Google Sheets, you might need to...

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.