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 Expert Prashanth KV is a Diamond Product Expert in Google Sheets, officially recognized by Google for his contributions to the Docs Editors Help Community and featured in the Google Product Experts Directory. Explore his blog to learn advanced formulas, automation tips, and problem-solving techniques to elevate your spreadsheet skills.

Google Sheets Reading List Tracker Template (Free Download)

Looking for a smarter, more visual way to manage your reading goals? This Google...

Custom Order for QUERY Pivot Headers in Google Sheets

By default, when you use the PIVOT clause in a Google Sheets QUERY, the...

Calculate Trip Days by Month (Start, End, and Full Days) in Google Sheets

If you're managing business travel in Google Sheets, you may need to calculate how...

Mode of Comma-Separated Numbers in Excel (Dynamic Array)

There is no dedicated function in Excel to directly find the mode of comma-separated...

More like this

Mode of Comma-Separated Numbers in Excel (Dynamic Array)

There is no dedicated function in Excel to directly find the mode of comma-separated...

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

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.