Query to Filter Previous N Dates in Google Sheets

Published on

To filter rows that contain dates of previous n dates in Google Sheets, we can use the function Query. Previous n dates can be last 1 day, 2 days, 3 days etc. This tutorial is based on the current date based Query filtering in Google Sheets.

Actually, I do have a similar tutorial on this site that I have posted in the recent past – Find the Past or Future Closest Date to Today in Google Sheets. But that approach is different. Here I am using the function Query to filter last n dates.

How to Filter Previous N Dates in Google Sheets Using Query

Without a sample data, we can’t proceed further. So here is that demo data and the expected output. Here the ‘n’ is considered as 1. The formula that I am going to provide you is flexible enough. So you don’t face any issue with changing the ‘n’ in the formula.

As per my example, today’s date is 04/Dec/2018 (my system clock date). That means I want to filter the rows in the range that contain the dates closest to 04/Dec/2018.

Example:

Filter Previous N Dates in Google Sheets

The Query Formula to Filter Last N Days in Google Doc Sheets:

=query({A2:C},"Select * where Col1<date '"&text(today(),"yyyy-mm-dd")&"' order by Col1 Desc Limit 1")

This formula would return only one row (previous 1 day). To return previous/last two dates change the 1 in the last part of the formula to 2, I mean change the clause Limit 1 to Limit 2.

Formula Logic: How The Query Filters Previous N Dates in Google Sheets

The formula filters all the rows in the range if the dates in column 1 are less than today’s date.

Note: Unlike the Filter function, you should use the date criteria in Query carefully. For the time being, you can follow my above formula to use the today() function in Query.

If you want to know more about the use of dates as the criterion in Query, do check my guide about converting a date to a string using the Long-winded Approach or the Query date criteria guide.

Now back to the formula logic. As mentioned, I have used the today() function in Query to filter previous n dates in Google Sheets. Then sorted that output within the same Query in descending order.

Why the sorting in descending order?

After sorting the filtered output in descending order, the dates closest to today() will be on the top. Now use the Limit clause in Query to limit the number of rows to ‘n’.

More Resources:

1. How to Use Query With Importrange in Google Sheets.

2. Combine Similar Rows and Sum Values in Google Sheets.

3. How to Sum, Avg, Count, Max, and Min in Google Sheets Query.

4. How to Use Arithmetic Operators in Query in Google Sheets.

5. How to Create a Search Box Using Query 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.

Sort Data but Keep Blank Rows in Excel and Google Sheets

Using a dynamic array formula or the FILTER command, we can sort data and...

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

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.