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.

Hierarchical Number Sorting in Excel with Modern Functions

A hierarchical numbering system in Excel allows you to organize data into a clear,...

Dynamic Formula to Sum Every 7 Rows in Excel

To sum every 7 rows, you can use either a drag-down formula or a...

How to Extract Numbers from Text in Excel with Regex

You can use the REGEXEXTRACT or REGEXREPLACE functions to easily extract numbers from text...

Using OFFSET and MATCH Together in Google Sheets: Advanced Tips

One powerful and flexible way to look up values is by combining the OFFSET...

More like this

Using OFFSET and MATCH Together in Google Sheets: Advanced Tips

One powerful and flexible way to look up values is by combining the OFFSET...

Running Count with Structured References in Google Sheets

Running a count with structured references is achievable in Google Sheets tables using the...

Running Total with Structured Table References in Google Sheets

You can use two types of formulas to create a running total with structured...

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.