Tableau function Date

Tableau QUARTER to SQL

QUARTER(date) returns the calendar quarter as an integer: January to March is 1, October to December is 4. It equals DATEPART('quarter', date). In DuckDB use quarter(). Hand-rolled arithmetic on the month is easy to get one off.

Updated · Checked on the xmetric engine

What is the syntax?

QUARTER(date)
date
a date or date-time.

Returns an integer from 1 to 4. Null if the date is null.

What does it return, and what is the SQL?

Each example runs on the same 9 sample rows twice: the Tableau formula on the xmetric engine, and the SQL in DuckDB.

Which quarter was each order placed in?

Tableau

QUARTER([Ordered])

SQL (DuckDB)

SELECT quarter(Ordered) AS result
FROM input
ORDER BY id

The December orders are quarter 4; the March orders are quarter 1.

OrderedResult
2021-01-01 08:001
2021-01-03 18:451
2024-02-29 10:151
2024-12-29 16:25:404
2024-12-30 09:054
2025-01-31 23:59:591
2025-03-01 00:001
2025-03-02 12:30:151
null

The Tableau formula and the SQL return the same 9 rows.

On which date does each order's quarter start?

Tableau

DATETRUNC('quarter', [Ordered])

SQL (DuckDB)

SELECT date_trunc('quarter', Ordered) AS result
FROM input
ORDER BY id

Use DATETRUNC when you need a date, QUARTER when you need the number.

OrderedResult
2021-01-01 08:002021-01-01 00:00
2021-01-03 18:452021-01-01 00:00
2024-02-29 10:152024-01-01 00:00
2024-12-29 16:25:402024-10-01 00:00
2024-12-30 09:052024-10-01 00:00
2025-01-31 23:59:592025-01-01 00:00
2025-03-01 00:002025-01-01 00:00
2025-03-02 12:30:152025-01-01 00:00
null

The Tableau formula and the SQL return the same 9 rows.

Where does a straight translation go wrong?

Common mistake

Why does floor(month / 3) + 1 give quarter 5 in December?

Dividing the month by 3 is off by one at every quarter end: March gives 2 and December 5. The month must be shifted to start at 0 first. Use quarter().

Tableau

QUARTER([Ordered])

Tempting SQL

SELECT floor(month(Ordered) / 3) + 1 AS result
FROM input
ORDER BY id

SQL that matches

SELECT quarter(Ordered) AS result
FROM input
ORDER BY id
OrderedTableauTempting SQLSQL that matches
2021-01-01 08:00111
2021-01-03 18:45111
2024-02-29 10:15111
2024-12-29 16:25:40454
2024-12-30 09:05454
2025-01-31 23:59:59111
2025-03-01 00:00121
2025-03-02 12:30:15121
nullnullnull

The SQL that matches returns the Tableau result on every row; the marked cells are where the tempting SQL differs.

How do I write it in Snowflake or BigQuery?

The first example, written for each warehouse from its documentation. These are not run here; DuckDB is.

Snowflake

SELECT QUARTER(Ordered) AS result
FROM input
ORDER BY id

Snowflake documentation

BigQuery

SELECT EXTRACT(QUARTER FROM Ordered) AS result
FROM input
ORDER BY id

BigQuery documentation

What does your own QUARTER formula return?

Edit the formula and run it on the 9 sample rows. Fields: [id], [Ordered], [Due], [Text], [Local], [Y], [M], [D].

Runs in your browser on the xmetric engine. Nothing is uploaded.

Which functions go with it?

What do people ask about QUARTER?

How is QUARTER different from ISOQUARTER?

QUARTER uses calendar months. ISOQUARTER groups ISO weeks 1-13, 14-26, 27-39 and 40-53, so 1 January 2021 (ISO week 53 of 2020) is quarter 1 by QUARTER and 4 by ISOQUARTER.

How do I label quarters like 2025 Q1?

STR(YEAR([Ordered])) + " Q" + STR(QUARTER([Ordered])); in SQL CAST(year(Ordered) AS VARCHAR) || ' Q' || CAST(quarter(Ordered) AS VARCHAR).

What does QUARTER return for a null date?

Null, in Tableau and in SQL.

Where is this documented?