How to Use Month Function in Google Sheets Query

Published on

The Month function, one of the scalar functions in Google Sheets Query, returns the month numbers from a date or timestamp column.

Here are two salient features of the Month function in Query.

  1. It’s a scalar function, so it returns one value per row, unlike the aggregate functions, which return one value per group.
  2. It changes column names when wrapping it with columns. If the field label of column C is “Purchase Date,” the following formula will replace it with “month(Purchase Date).”
=query(C:C,"Select month(C)")

How does it differ from its counterpart worksheet function?

When we compare the Month scalar function in Google Sheets Query with the Month() worksheet function, which is part of Date Functions, the former returns a slightly different result.

Here, the month numbers are zero-based. As a result, the month number of January will be 0, February will be 1, and so on.

You can change this behavior with a simple trick which I’ll explain later.

Purpose of the Month Function in Google Sheets Query

Here are the two purposes of the Month scalar function in Google Sheets Query.

  1. Return month numbers from columns.
  2. Group a date or DateTime (timestamp) column by months.

Sample Data:

Sample Data: Google Sheets Query Month Function

Return Month Numbers:

Insert =query(A:A,"Select month(A)") in the top row of any blank column to extract month numbers from the dates in column A.

Group by Month Numbers:

Now let’s learn in detail how to convert and summarise data based on the month numbers.

We can use the above data and summarize “Product” month-wise.

The purpose is to get the total sales amount of each product in different months.

Here is our expected result.

Summary by Using Query Month Function

There are two steps involved in generating the above summary.

You can learn that below and get a clear picture of using the Month function in Google Sheets Query.

Using the Month Function in Google Sheets Query to Summarize Data

We are using only three columns in the above sample data: A (Date), B (Product), and F (Amount).

There are two steps: Group & Summarize and Pivot & Label Format.

With these, we can learn how to use the Month function in Google Sheets Query to convert dates to months and group by month numbers (not month text).

1. Grouping and Summarizing

There are two columns in the SELECT clause of the below Query formula: Month(A) and B, and the first one converts dates to zero-based month numbers.

=query(A1:F,"Select Month(A),B, Sum(F) where A is not null group by Month(A),B",1)

The formula groups these selected columns using the GROUP BY keyword combination (clause) and then aggregates (sum) column F.

Related: What is the Correct Clause Order in Google Sheets Query?

How to Convert Zero-Based Month in Query?

=query(A1:F,"Select Month(A)+1,B, Sum(F) where A is not null group by Month(A)+1,B",1)

I used the Month function in the above Query as Month(A)+1 in both the SELECT and GROUP BY clauses.

It is simply because, in Query, the months are counted/numbered from 0 to 11 (zero-based).

So, the Month function would return 0 for January, 1 for February, etc.

By adding 1 to the returned value, we can get the usual month number for our purpose.

Please see the result of the above formula.

how to use month to return proper month number in query

It is not our expected result! We should pivot this output and format the labels for that.

Find those steps below.

2. Pivot Grouped Data and Formatting Labels

To pivot the data by “Products,” remove the relevant column B from the grouping.

Then use it with the PIVOT clause.

=query(A1:F,"Select Month(A)+1, Sum(F) where A is not null group by Month(A)+1 pivot B",1)

Finally, use the LABEL clause to rename sum(month(Date)1()) (please see the above image), the column name “Date” replaced by the Month scalar function, with “Month.”

=query(A1:F,"Select Month(A)+1, Sum(F) where A is not null group by Month(A)+1 pivot B label Month(A)+1'Month'",1)

Scroll up and see the above Screenshot # 2 for the output.

This way, we can use the Month function in Google Sheets Query and make a month-wise summary report.

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.

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

How to Find Mode of Comma-Separated Numbers in Google Sheets

Working with comma-separated numbers inside a single cell is a common scenario in Google...

How to Count Merged Cells in Google Sheets (and Get Their Size)

Sometimes, you may have blocks of merged cells in a column or row. But...

More like this

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

How to Find Mode of Comma-Separated Numbers in Google Sheets

Working with comma-separated numbers inside a single cell is a common scenario in Google...

How to Count Merged Cells in Google Sheets (and Get Their Size)

Sometimes, you may have blocks of merged cells in a column or row. But...

2 COMMENTS

    • Hi, Arvind Pradhan,

      I’ve already explained the same within this tutorial.

      It’s numbered 0-11. To get 1-12, you should add 1 to the returned month numbers.

      Eg.

      Use Month(A)+1 instead of Month(A).

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.