HomeGoogle DocsSpreadsheetGoogle Sheets: Find the Closest Value with FILTER & QUERY (Step-by-Step Guide)

Google Sheets: Find the Closest Value with FILTER & QUERY (Step-by-Step Guide)

Finding the closest value in Google Sheets is a common requirement — whether you’re comparing scores, analyzing sales numbers, or matching weights and quantities. Google Sheets doesn’t have a direct function for this, but you can achieve it using formulas.

In this tutorial, we’ll cover:

  1. How to find the closest value (higher, lower, or exact).
  2. How to find the closest value greater than or equal to your lookup.
  3. How to find the closest value less than or equal to your lookup.

All formulas work with open-ended ranges (A2:B) and handle empty rows safely.

Example Dataset

Let’s say you’re tracking players and their scores:

Google Sheets sample dataset with player names and scores for closest match lookup

In cell D2, enter a lookup score (e.g., 140). We’ll now find the closest match.

Find the Closest Match (Higher or Lower)

Use this formula to return the player(s) with the closest score to your lookup:

=FILTER(A2:B, ABS(B2:B-D2)=SORTN(1/(ABS(B2:B-D2)*(B2:B<>""))^-1))
Google Sheets FILTER formula showing closest score match to a lookup value

Why this works:

  • ABS(B2:B-D2) → calculates the difference between each score and the lookup.
  • SORTN(...) → finds the minimum difference.
  • (B2:B<>"") → ensures empty rows are ignored.
  • FILTER(...) → returns the row(s) where the difference equals the minimum.

👉 Note: If two values are equally close, both will be returned. For example, if D2 = 310:

Kiran300
John320

If you want only one, wrap the formula with CHOOSEROWS:

  • First row: =CHOOSEROWS(FILTER(...), 1)
  • Last row: =CHOOSEROWS(FILTER(...), -1)

Closest Match (Greater Than or Equal To)

To always return the next highest score:

=QUERY(A2:B, "select A, B where B >= "&D2&" order by B limit 1", 0)
Google Sheets QUERY formula returning the closest score greater than or equal to the lookup value

Closest Match (Less Than or Equal To)

To always return the next lowest score:

=QUERY(A2:B, "select A, B where B <= "&D2&" order by B desc limit 1", 0)
Google Sheets QUERY formula returning the closest score less than or equal to the lookup value

Comparison Table

RequirementFormulaNotes
Closest match (higher or lower)=FILTER(A2:B, ABS(B2:B-D2)=SORTN(1/(ABS(B2:B-D2)*(B2:B<>""))^-1))May return 2 rows if equally close. Use CHOOSEROWS to pick one.
Closest ≥ lookup=QUERY(A2:B, "select A, B where B >= "&D2&" order by B limit 1", 0)Returns the next highest score.
Closest ≤ lookup=QUERY(A2:B, "select A, B where B <= "&D2&" order by B desc limit 1", 0)Returns the next lowest score.

FAQs

Q. What happens if the lookup value is smaller than the minimum score?

  • The “less than or equal to” formula will return nothing.
  • The “greater than or equal to” formula will return the smallest value.

Q. What happens if the lookup value is greater than the maximum score?

  • The “greater than or equal to” formula will return #N/A. To avoid this, wrap the formula with IFNA().
  • The “less than or equal to” formula will return the largest value.
Prashanth K V
Prashanth K V
Your Trusted Google Sheets and Excel Expert Prashanth K V is a Diamond Product Expert in Google Sheets, officially recognized by Google for his contributions to the Docs Editors Help Community and featured in the Google Product Experts Directory. Explore his blog to learn advanced formulas, automation tips, and problem-solving techniques to elevate your spreadsheet skills.

Top Discussions

More like this

How to Build a Dynamic Nonogram Clue Generator in Google Sheets

Over the past few months, I've built a couple of games in Google Sheets,...

Carpool Cost Splitter & Rotation Tracker in Google Sheets (Free Template)

Managing a carpool can be difficult, especially when you need to track whose vehicle...

How to Build a Road Trip Fuel Cost Splitter Formula in Google Sheets

Need a fair formula to split fuel costs among travelers on a long road...

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.