Google Sheets Query to Extract All the Rows from Previous Month

Published on

Using the Google Sheets Query function, we can extract all the records—I mean, rows—from the previous month.

Let’s say the current month is July 2025, and you want to get all the rows from June 2025. How do you go about it?

Whether it’s the current month or the previous one, you can filter data accordingly using the QUERY function. The scalar functions we need here are month(), year(), and now().

These functions let you filter a Date or DateTime column to get just the previous month’s rows in Google Sheets.

Also, unlike current month filtering, extracting the previous month’s records has a small catch—especially when the current month is January (explained below).

So I thought this deserved its own post. Let’s begin with an example, as usual.

This tutorial is part of the Date Logic in QUERY hub.
Learn how date logic works in Google Sheets QUERY, including date criteria, month-based filtering, and DateTime handling.

Sample Data

Here’s some sample data from a statement of account (SOA). The range is A1:F.

Sample data table used to demonstrate how to use Google Sheets Query to extract all the rows from previous month

Note: The current month (as per my system clock) is July 2025.

In your actual data, I hope you’ve got more rows that fall in the previous month.

Query to Extract Previous Month Rows (If Current Month Is Not January)

Here’s the simple version—this will work as long as the current month is not January.

The following Google Sheets Query will extract all rows where the date column (column B) falls in the previous month:

=QUERY(SOA!A1:F, "SELECT * WHERE month(B)=month(now())-1 AND year(B)=year(now())", 1)

This works well for July (returns rows from June), October (returns rows from September), and so on.

Here’s what you’ll get for July 2025:

INV #DATEDEBITCREDITBALANCELPO #
II-167411/06/20257750.007750.000.004100085202
II-500314/06/20251473.731473.730.004100099180
II-500415/06/202511750.000.0011750.004100099590
II-500620/06/202542041.5042041.500.004100099994
II-501530/06/20252200.002200.000.004100086791

Why It Fails in January

Now here’s the catch: in QUERY, the month() function returns values from 0 to 11 (where 0 is January and 11 is December). So if the current month is January, month(now()) returns 0, and month(now()) - 1 becomes -1, which is invalid.

On top of that, year(now()) returns the current year, but the previous month—December—belongs to the previous year, so year(B) = year(now()) also fails.

That’s why the simple formula doesn’t work in January, and we need a more reliable solution.

Query to Extract Previous Month Rows (Works in Any Month)

Here’s the all-weather formula that works no matter which month you’re in—even January.

=QUERY(SOA!A1:F, "SELECT * WHERE month(B)="&MONTH(EOMONTH(TODAY(),-1))-1&" AND year(B)="&YEAR(EOMONTH(TODAY(),-1)), 1)

Let’s break it down:

  • EOMONTH(TODAY(), -1) gets the last day of the previous month.
  • MONTH(...) - 1: Adjusts to QUERY’s month numbering (0-based).
  • YEAR(...): Ensures it catches the right year, even if it’s December from the previous year.

Use this version if you want a Google Sheets Query to extract all the rows from the previous month that works throughout the year, including the edge case in January.

Bonus Tip: Sum Previous Month’s Column Values

Let’s say you want to sum the Credit column (column D) for the previous month.

Just modify the above query a bit:

=QUERY(SOA!A1:F, "SELECT sum(D) WHERE month(B)="&MONTH(EOMONTH(TODAY(),-1))-1&" AND year(B)="&YEAR(EOMONTH(TODAY(),-1))&" LABEL sum(D) ''", 1)

That’ll return the total credit amount from the previous month.

Wrapping Up

So, to recap:

  • If the current month isn’t January, you can use the simple version of the QUERY.
  • If you want a reliable solution that works every month (including January), go with the EOMONTH-based formula.
  • And if needed, you can even aggregate values like total debit or credit using the same approach.

Now you know how to use Google Sheets Query to extract all the rows from the previous month, dynamically and correctly.

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.

Top Discussions

More like this

Pivot Table Formatting, Output & Special Behavior in Google Sheets

Pivot Tables in Google Sheets are powerful—but they can get tricky once you move...

Pivot Table Calculations & Advanced Metrics in Google Sheets

When it comes to built-in tools for data analysis and visualization in Google Sheets,...

Google Sheets Pivot Table Tutorial: Basics, Setup, and Date Grouping

The easiest way to summarize, analyze, and report data in Google Sheets is by...

2 COMMENTS

    • Hi, Angela,

      Sorry for the unforeseen error.

      This formula may work, and I’ll update the post soon.

      =query(SOA!A1:F,"Select * where month(B)="&month(eomonth(today(),-2))
      &" and year(B)="&year(eomonth(today(),-1)))

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.