Formula to Conditionally Filter Last N Rows in Google Sheets

By using the Query function, we can conditionally filter last n rows in a ‘data’ (range) in Google Sheets. For this purpose, we can use the WHERE and OFFSET clauses in the Query function in Sheets.

The WHERE clause enables us to apply the required condition to filter the data. Then with the help of the OFFSET clause, we can offset a ‘certain number’ of rows from the front to get the last N rows.

Actually, the purpose of the OFFSET in Query is to offset n number of rows. But we can use this clause dynamically to filter last n rows. Yes! That trick I am going to share with you in this post.

Before coming to the formula, let me tell you where this kind of filtering comes to use. I mean the real-life use of conditionally filtering last n rows in a dataset in Google Sheets.

Assume you have a list of teams and their score in the last 10 tournaments. To find the sum, average, max, and min of the last n results (scores) of a team, you can use my dynamic last n row filtering, which also conditionally.

A Similar Example:

Example - Conditionally Filter Last N Rows in Google Sheets

Query Formula to Conditionally Filter Last N Rows in Google Sheets

As usual, let’s start with the sample data table. Just type the above values in columns B and C in your sheet.

Then type the filter condition/criterion in cell E2 and the last n rows that you want to filter in F2.

Done? Then let’s start to learn how to conditionally filter last n rows in Google Sheets.

Apply Condition Using WHERE in Query

Conditional filtering is just a child’s play if you know the use of Query. To master this, I suggest you read my following guide in your leisure time – Examples to the Use of Literals in Query in Google Sheets.

Also, find time to learn AND, OR, NOT in Query, Substring Match Using Contains, Like, Matches and Date Criteria Use in Query.

Here our task is just simple. In our topic, conditionally filtering last n rows, I am going to use only a single column, single condition filter.

We just want to filter the data in the range B2:C, if the name in B2:B matches the criterion in cell E2, i.e. “John”.

Here is the required formula for that.

Criterion within formula;
=query(B2:C,"Select * where B='John'")
Criterion as cell reference;
=query(B2:C,"Select * where B='"&E2&"'")

Result:

Filter Data to Offset N - Sheets

COUNTIF in Query OFFSET to Filter Last N Rows in Google Sheets

We have completed the first part, i.e. conditionally filtering a dataset. Now we want to filter the last n rows from this output.

There is no clause in Query syntax to filter or conditionally filter last n rows in Google Sheets. We need to write a formula for that.

We can make use of the OFFSET clause in a certain way for this. The purpose of this clause is to offset a certain number of rows no matter whether the data is filtered or not.

To conditionally filter the last n rows, we can use the below logic.

=countif(B2:B,E2)

Result: 5

The above COUNTIF will return the conditional count, which means the number of rows containing the criteria “John” in column B.

To find the number of rows to offset, use the above formula – n.

=countif(B2:B,E2)-F2

If the value in F2 = 1, the above Countif formula would return 4. That means offset 4 rows and return the last 1 row.

The next question is how to connect the above Countif in Query. For that, we can follow the below approach.

We can use the just above Query formula as the data in another Query and offset the rows using the above Countif formula.

Conditionally Filter Last N Rows in Google Sheets – Syntax

=query(Query_formula,"Select * offset "&countif_formula)

So here is the formula to conditionally filter last n rows in Google Sheets.

=query(query(B2:C,"Select * where B='"&E2&"'"),"Select * offset "&countif(B2:B,E2)-F2)

Errors

The Query formula above would return #VALUE! error in two cases.

  1. When the filter condition is not available in the data range. I mean if the inner Query returns #N/A, the formula would return the said error.
  2. Last ‘n’ is greater the number of rows in the filtered output.

The formula would return #N/A! error, if the last ‘n’ is 0 or blank.

That’s all about how to conditionally filter last n rows using Query in Google Sheets.

Conditional SUM/AVERAGE/MAX/MIN Last N Results

Here are some real-life uses of the above type of last n row filtering. My following examples are based on the above data.

How to Sum Last N Values Conditionally in Google Sheets?

Formula:

=SUM(query(query(B2:C,"Select * where B='"&E2&"'"),"Select Col2 offset "&countif(B2:B,E2)-F2))

How to Max/Min Last N Values Conditionally in Google Sheets?

Same as above. The only change here is the use of MAX/MIN instead of SUM.

Formula:

=MAX(query(query(B2:C,"Select * where B='"&E2&"'"),"Select Col2 offset "&countif(B2:B,E2)-F2))

How to Average Last N Values Conditionally in Google Sheets?

For the conditional average of last n values, replace MAX with AVERAGE.

Formula:

=AVERAGE(query(query(B2:C,"Select * where B='"&E2&"'"),"Select Col2 offset "&countif(B2:B,E2)-F2))

Additional 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.

Lookup Values Under Categories in Excel and Google Sheets

We can use a combination of XLOOKUP and VLOOKUP in both Excel and Google...

Extract Items Under a Category from the Same Column in Excel

In Excel, you can use a combination of the XLOOKUP and DROP functions to...

How to Create a Searchable Table in Excel Using the FILTER Function

Finding specific records, or rows containing the required information, is straightforward in Excel using...

Time Sequences in Excel by Minute, Hour, or Second Increments

Creating time sequences, whether by hour, minute, or second increments in Excel, can be...

More like this

XMATCH Row by Row: Finding Values Across a Range in Google Sheets

Using the BYROW function with XMATCH in Google Sheets allows us to match values...

Limit Formula Expansion to a Specific Row in Google Sheets

In this tutorial, I’ll explain how to limit the expansion of an array formula...

3-D Referencing Structured Data Tables in Google Sheets

When you have several tables within a single sheet—not across multiple sheets in a...

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.