HomeExcel FormulaFilter Data from the Previous Month Using a Formula in Excel

Filter Data from the Previous Month Using a Formula in Excel

Published on

Filtering data from the previous month may be useful for comparative analysis, generating reports, problem identification, forecasting, etc.

This tutorial explains how to use the FILTER function to filter data from the previous month in Excel for Microsoft 365.

If you are currently using something like this formula: =FILTER(filter_range, MONTH(date_column) = MONTH(TODAY())-1), I recommend against it.

While it might return the correct result at present, it will fail in two scenarios:

  1. It will fail when the current month is January because MONTH(TODAY())-1 will result in 0, instead of 12 in January.
  2. If your data spreads across two or more years, the formula may filter data from multiple years as the year part is not used in the criterion.

The proper formula to filter data from the previous month in Excel is:

=FILTER(
   filter_range, MAP(date_column, 
   LAMBDA(r, EOMONTH(r, -1)+1)) = EOMONTH(TODAY(), -2)+1
)

When using this formula:

After filtering, the date column in the result will return as date values. To format them, select the column and choose a desired date format (e.g., “Short Date”) from the Number group on the Home tab.

Example: Filter Previous Month’s Data in Excel

The sample data range is A1:C10, where column A contains dates, column B contains item names, and column C contains quantities.

We will filter the range A2:C10, excluding the header row A1:C1, as it contains field labels. Including the header row in the filter range may cause the formula to fail because it employs a date criterion.

Before proceeding with the sample data, it’s important to note that the current month is April 2024. Therefore, the formula will filter the data from March 2024.

However, when using this formula, it’s essential to understand that it will take the current month from the date you use the formula (based on your system clock date on that day).

Example of Filtering Data from the Previous Month in Excel

Formula:

=FILTER(
   A2:C10, 
   MAP(A2:A10, LAMBDA(r, EOMONTH(r,-1)+1))=EOMONTH(TODAY(), -2)+1
)

Where A2:C10 is the filter_range and A2:A10 is the date_column range. Don’t forget to apply “Short Date” formatting to the date column range in the result.

The above demonstrates the correct method for filtering data from the previous month using a formula in Excel.

Formula Breakdown

Before delving into the explanation of the formula, let me address a limitation I’ve encountered with dynamic array formulas in Excel.

Unfortunately, dynamic array formulas are not capable of spilling all date functions. For instance, while the formula =MONTH(A2:A10) will return the month numbers from A2:A10, it won’t spill EOMONTH(A2:A10, 0).

With that in mind, let’s examine how the formula filters the previous month’s data.

Syntax:

FILTER(array, include, [if_empty])

Where:

  • array: A2:C10
  • include: MAP(A2:A10, LAMBDA(r, EOMONTH(r,-1)+1))=EOMONTH(TODAY(), -2)+1

The crucial aspect of filtering by the previous month lies in the ‘include’ part. Let’s delve deeper into it.

  • MAP(A2:A10, LAMBDA(r, EOMONTH(r, -1)+1)): This segment converts the dates in A2:A10 to the beginning of the month dates. Since EOMONTH doesn’t spill on its own in Excel, we utilize the MAP lambda function to iterate through each row in the range A2:A10 within EOMONTH.
  • EOMONTH(TODAY(), -2)+1: This expression returns the beginning of the month date of the previous month.

The formula filters A2:C10 if the beginning of the month dates in A2:A10 are equal to the beginning of the month date in the previous month.

This approach is the correct method to filter the previous month’s data from a table in Excel.

The advantage lies in using a date as the criterion rather than a month number. Using a month number can lead to issues, especially when the current month is January (1), as subtracting 1 would result in 0, not 12.

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.

Counting XLOOKUP Results with COUNTIFS in Excel and Google Sheets

We can use COUNTIF or COUNTIFS alongside an XLOOKUP formula to conditionally count the...

Appointment Schedule Template in Google Sheets

An appointment schedule template in Google Sheets can assist you in efficiently managing your...

Creating Sequential Dates in Equally Merged Cells in Google Sheets

Do you know how to create sequential dates in equally merged cells across a...

Running Total By Month in Excel

This tutorial demonstrates how to calculate the running total by month in a range...

More like this

Running Total By Month in Excel

This tutorial demonstrates how to calculate the running total by month in a range...

Get Top N Values Using Excel’s FILTER Function

When using the FILTER function in Excel to extract the top N values, it's...

XLOOKUP in Excel: Working with Visible Rows in a Table

In Excel, the XLOOKUP function isn't inherently designed to exclusively work with visible rows;...

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.