Filter Max N Values in Google Sheets (Step-by-Step Guide)

Published on

To filter the top N values in Google Sheets, you can use a custom formula in the FILTER menu or the SORTN function to extract them.

Assume you have student names in column A and their marks in column B. Let’s first filter the max N values using formulas, and then apply filtering using the Google Sheets Data Menu.

Filter Max N Values Using Formulas in Google Sheets

Sample Data:

Sample data for filtering max N scorers

Formulas and Their Features

FormulaPurpose
=SORTN(data, n, 0, 2, FALSE)Top N (Exactly N, No Extra Ties): Extracts exactly the first N rows from a descending sorted range.
=SORTN(data, n, 1, 2, FALSE)Top N + All Duplicates of Nth: Extracts the first N rows from a descending sorted range and includes any additional rows identical to the Nth value.
=SORTN(data, n, 2, 2, FALSE)Top N (Distinct Values Only): Extracts the first N unique values from a descending sorted range, ignoring duplicates.
=SORTN(data, n, 3, 2, FALSE)Top N (Distinct Values + All Occurrences): Extracts the first N unique values from a descending sorted range and includes all their duplicates.

Where data is a two-column range (Column A for names, Column B for marks), and n determines the number of top values to extract.

Example 1: Extract Top 3 (Exactly 3, No Extra Ties)

Formula:

=SORTN(A2:B, 3, 0, 2, FALSE)

This formula extracts exactly the top 3 highest-scoring students:

NameMarks
Student 199
Student 298
Student 398

Example 2: Extract Top 3 + All Duplicates of 3rd

Formula:

=SORTN(A2:B, 3, 1, 2, FALSE)

This formula extracts the top 3 highest scores, including all ties at the 3rd position:

NameMarks
Student 199
Student 298
Student 398
Student 498

Example 3: Extract Top 3 Distinct Values

Formula:

=SORTN(A2:B, 3, 2, 2, FALSE)

This formula extracts the top 3 distinct scores, ignoring duplicate values:

NameMarks
Student 199
Student 298
Student 797

Example 4: Extract Top 3 Distinct Values + All Occurrences

Formula:

=SORTN(A2:B, 3, 3, 2, FALSE)

This formula extracts the top 3 distinct values and includes all students with those scores:

NameMarks
Student 199
Student 298
Student 398
Student 498
Student 797

How Does This Formula Filter Max N Values?

The SORTN function is designed to extract the top N rows from a sorted range. It offers four tie modes to control whether duplicates are included or excluded.

  • We sort the data in descending order based on marks.
  • The tie modes (0 to 3) determine how duplicates are handled.

These formulas help you filter the top N in Google Sheets while offering flexibility based on whether you want to include or exclude ties.

Filter Max N Using the Data Menu in Google Sheets

To filter the largest N values using the Data menu, you can apply custom formulas in Google Sheets:

Formula for Top N + All Duplicates of Nth (Example 2 Equivalent)

=RANK(value, data) <= n

Formula for Top N Distinct + All Duplicates (Example 4 Equivalent)

=RANK(value, UNIQUE(data)) <= 3

Steps to Apply the Filter:

  • Select Cell B1 (or the header of your column).
  • Click Data > Create a filter.
  • Open the filter drop-down in cell B1.
  • Click “Filter by Condition”, then select “Custom formula is”.
  • Copy-paste one of the formulas above.
  • Click OK.

This method will filter the top N values in Google Sheets dynamically, based on your selected formula.

Example of filtering max N values in Google Sheets

Resources

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 Sort Pie Slices in Google Sheets

To sort pie slices in a pie chart, you need to sort the data...

Filter Items Unique to Groups in Google Sheets

In this tutorial, we'll learn how to filter items unique to groups in Google...

Find Common Items Across Multiple Columns in Google Sheets

This tutorial explains how to find common items across multiple columns in Google Sheets....

Sort Column by Length of Text in Google Sheets

To sort a column by length of text, you can either use the QUERY...

More like this

How to Sort Pie Slices in Google Sheets

To sort pie slices in a pie chart, you need to sort the data...

Filter Items Unique to Groups in Google Sheets

In this tutorial, we'll learn how to filter items unique to groups in Google...

Find Common Items Across Multiple Columns in Google Sheets

This tutorial explains how to find common items across multiple columns in Google Sheets....

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.