Filter the Last 7 Days in Excel Using the FILTER Function

Published on

You may have records spanning several days and want to filter the last 7 days’ data for sampling or some other purpose. Additionally, the date column may contain either dates or timestamps. Is there a common formula to filter the last 7 days’ data in Excel regardless of whether the column has dates or timestamps?

The answer is yes. But there’s one thing to note. If you want the report to show data from the last 7 days including today as the most current day, you should use one formula. To exclude today, use a different formula.

Formula to Exclude Today’s Date:

=FILTER(filter_range, (INT(date_range) >= TODAY() - 7) * (INT(date_range) < TODAY()))

Formula to Include Today’s Date:

=FILTER(filter_range, (INT(date_range) >= TODAY() - 6) * (INT(date_range) <= TODAY()))

Both formulas are capable of handling dates and datetimes.

How to Use the Formula:

  • Replace filter_range with the range you want to filter.
  • Replace date_range with the reference to your date column.

Ensure that the filter_range and date_range have the same vertical size because the formula filters based on dates or timestamps in a column.

Example: Filtering Last 7 Days’ Sales Data

Assume you want to filter the last 7 days’ sales data excluding today from the following sales report spanning A1:E17:

Sample data for filtering the last 7 days of data in Excel

To filter data excluding today, use this formula:

=FILTER(A2:E17, (INT(A2:A17) >= TODAY() - 7) * (INT(A2:A17) < TODAY()))

This will return the records from rows 8 to 14 (as a side note, in our example, today’s date is 28-11-2024).

To filter data including today, use this formula:

=FILTER(A2:E17, (INT(A2:A17) >= TODAY() - 6) * (INT(A2:A17) <= TODAY()))

This will return the records from rows 9 to 15.

Formula Explanation

Let me explain the first formula, which will also help you understand the second one.

The FILTER function in Excel has the following syntax:

FILTER(array, include, [if_empty])

We’ve used the first two arguments:

  1. array: A2:E17 – This is the range to filter.
  2. include: (INT(A2:A17) >= TODAY() - 7) * (INT(A2:A17) < TODAY()) – Filters rows where the formula evaluates to 1.

This include condition works as follows:

  • INT(A2:A17): Removes the time component from datetime values, leaving only the date.
  • INT(A2:A17) >= TODAY() - 7: Returns TRUE if the date is greater than or equal to 7 days ago, counting from today. Refer to column G in the following screenshot.
  • INT(A2:A17) < TODAY(): Returns TRUE if the date is before today. Refer to column H in the following screenshot.
Explanation of the logic to filter data for the last 7 days in Excel using the FILTER function

When these two conditions are multiplied, rows where both conditions are TRUE return 1, while others return 0.

Why Include INT?

If the date column contains timestamps, the INT function ensures that only the date part is evaluated. Without INT, times in the datetime values might prevent accurate filtering.

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.

How to Sort Pie Slices in Google Sheets

To sort pie slices in a pie chart, you need to sort the data...

Filter Items Unique to Groups in Google Sheets

In this tutorial, we'll learn how to filter items unique to groups in Google...

Find Common Items Across Multiple Columns in Google Sheets

This tutorial explains how to find common items across multiple columns in Google Sheets....

Sort Column by Length of Text in Google Sheets

To sort a column by length of text, you can either use the QUERY...

More like this

Dynamic Way to Insert Blank Rows in a Table in Excel

Do you want to easily insert n number of blank rows in a table...

Insert N Empty Cells Between Values in Excel (Dynamic Array)

Do you want to space out data by inserting a specific number of empty...

How to Extract the Last N Non-Blank Rows in Excel Dynamically

You can use the following formula to extract the last N non-blank rows in...

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.