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.

Use XLOOKUP in a Structured Table in Google Sheets (Single and Multiple Conditions)

This tutorial is for users who want to apply XLOOKUP inside a structured table...

Reset SCAN by Another Column in Google Sheets and Excel

Resetting SCAN function results based on values in another column is a topic of...

How to Get the Fastest Time for Each Person in Google Sheets

Whether you’re tracking race results, event times, or any other timed activities, finding the...

Highlight the Smallest N Values in a Column in Google Sheets

Want to visually spotlight the lowest values in your data? In this tutorial, you'll...

More like this

Sort Each Row Individually in Excel Using a LAMBDA Formula

Sorting rows in Excel typically refers to rearranging entire datasets based on values in...

Sort by Field Labels Using the SORT and XMATCH Combo in Excel

Want to sort your Excel data by column names instead of column positions? Learn...

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

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.