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.

Marking Case-Sensitive Unique Values in Excel

Marking case-sensitive unique values provides several benefits compared to merely extracting them in an...

Highlight Upcoming Birthdays in Google Sheets

When highlighting upcoming birthdays in Google Sheets, one important aspect is ignoring the year...

Compare Two Tables for Differences in Excel

To compare two tables with similar data for differences, you can use the XLOOKUP...

Calculate Weighted Average in Pivot Table in Google Sheets

You can calculate a weighted average in a Pivot Table using a custom formula...

More like this

Highlight Upcoming Birthdays in Google Sheets

When highlighting upcoming birthdays in Google Sheets, one important aspect is ignoring the year...

Calculate Weighted Average in Pivot Table in Google Sheets

You can calculate a weighted average in a Pivot Table using a custom formula...

Summarize Data and Keep the Last Record in Google Sheets

In Google Sheets, we can summarize data (like sum, average, min, max, count) using...

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.