How to Filter Previous N Dates in Google Sheets Using Query

Are you tired of manually filtering rows that contain dates from previous days in Google Sheets? Well, worry no more! In this tutorial, I’ll show you a simple solution using the powerful Query function. Whether you need to filter the last 1 day, 2 days, or any number of previous dates, this flexible formula has got you covered.

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

Before we dive into the details, let’s take a look at a demo data set and the expected output. For this example, let’s assume today’s date is 04/Dec/2018. Our goal is to filter the rows in a specific range that contain the dates closest to this reference date.

Filter Previous N Dates in Google Sheets

To achieve this, we’ll use the following Query formula:

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

This formula will return only one row, which represents the previous 1 day. If you need to return the previous 2 dates, simply change the clause “Limit 1” to “Limit 2” accordingly.

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

Let’s break down the formula logic step by step. First, the formula filters all the rows in the specified range where the dates in column 1 (Col1) are less than today’s date. This step ensures that we exclude any future dates.

Next, we sort the filtered output in descending order using the “order by Col1 Desc” clause. This sorting arrangement allows us to identify the dates closest to today’s date, which will be on the top.

Finally, we use the “Limit” clause to determine the number of rows to be returned. By adjusting this value, you can easily filter the desired number of previous dates.

It’s important to note that when using dates as criteria in Query, extra caution should be taken. In this tutorial, we’ve used the “today()” function within the Query formula. However, it’s recommended to refer to additional resources for a more in-depth understanding of date criteria in Query.

More Resources

If you’re interested in further expanding your knowledge of Query and its capabilities, here are some additional resources to check out:

  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

Now that you’ve mastered the Query function for filtering previous N dates in Google Sheets, you’ll save a significant amount of time and effort. So go ahead and give it a try! Happy filtering!

Note: This article is based on the original content from infoinspired.com.

Related posts