Tableau function Date

Tableau DATETRUNC to SQL

DATETRUNC(date_part, date, [start_of_week]) returns the first moment of the period that holds the date: DATETRUNC('month', #2025-01-31 23:59:59#) is 1 January 2025 at midnight. DuckDB's date_trunc does the same for most parts, but its weeks start on Monday, where Tableau's default week (US English) starts on Sunday. Shift the date by a day, truncate, and shift back.

Updated · Checked on the xmetric engine

What is the syntax?

DATETRUNC(date_part, date, [start_of_week])
date_part
a quoted string: 'year', 'quarter', 'month', 'week', 'day', 'hour', 'minute', 'second', or an ISO part such as 'iso-week'.
date
a date or date-time.
start_of_week
optional, for 'week': 'sunday' to 'saturday'. Without it, the data source's week start is used.

Returns a date or date-time (the same kind as date): the start of the date_part period. 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.

On which date does each order's month start?

Tableau

DATETRUNC('month', [Ordered])

SQL (DuckDB)

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

Order 6 at 23:59:59 on 31 January truncates to 1 January; the time goes to midnight.

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

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

On which Sunday does each order's week start?

Tableau

DATETRUNC('week', [Ordered])

SQL (DuckDB)

SELECT
  date_trunc('week', Ordered + INTERVAL 1 DAY)
    - INTERVAL 1 DAY AS result
FROM input
ORDER BY id

Add a day so Sunday becomes Monday, truncate to DuckDB's Monday week, take the day back. Order 1 (Friday 1 January 2021) goes back to Sunday 27 December 2020.

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

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

What is the month start of a date column?

Tableau

DATETRUNC('month', [Due])

SQL (DuckDB)

SELECT CAST (date_trunc('month', Due) AS DATE) AS result
FROM input
ORDER BY id

Cast back to DATE, so the result stays a date like the input.

DueResult
2021-01-012021-01-01
2021-01-032021-01-01
2024-02-292024-02-01
2024-12-292024-12-01
2024-12-302024-12-01
2025-01-312025-01-01
2025-03-012025-03-01
2025-03-022025-03-01
null

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

Where does a straight translation go wrong?

Common mistake

Why does date_trunc('week') land on Monday?

DuckDB weeks start on Monday (ISO). Tableau's default week here starts on Sunday, so Sunday orders such as 2 and 4 truncate to the day itself in Tableau and to the previous Monday in DuckDB.

Tableau

DATETRUNC('week', [Ordered])

Tempting SQL

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

SQL that matches

SELECT
  date_trunc('week', Ordered + INTERVAL 1 DAY)
    - INTERVAL 1 DAY AS result
FROM input
ORDER BY id
OrderedTableauTempting SQLSQL that matches
2021-01-01 08:002020-12-27 00:002020-12-28 00:002020-12-27 00:00
2021-01-03 18:452021-01-03 00:002020-12-28 00:002021-01-03 00:00
2024-02-29 10:152024-02-25 00:002024-02-26 00:002024-02-25 00:00
2024-12-29 16:25:402024-12-29 00:002024-12-23 00:002024-12-29 00:00
2024-12-30 09:052024-12-29 00:002024-12-30 00:002024-12-29 00:00
2025-01-31 23:59:592025-01-26 00:002025-01-27 00:002025-01-26 00:00
2025-03-01 00:002025-02-23 00:002025-02-24 00:002025-02-23 00:00
2025-03-02 12:30:152025-03-02 00:002025-02-24 00:002025-03-02 00:00
nullnullnull

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

Common mistake

Why does date_trunc turn a date column into a timestamp?

DuckDB's date_trunc returns a timestamp for most parts even on a DATE, so 1 December comes back as 2024-12-01 00:00:00. DATETRUNC keeps a date a date, which matters in joins and comparisons.

Tableau

DATETRUNC('month', [Due])

Tempting SQL

SELECT date_trunc('month', Due) AS result
FROM input
ORDER BY id

SQL that matches

SELECT CAST (date_trunc('month', Due) AS DATE) AS result
FROM input
ORDER BY id
DueTableauTempting SQLSQL that matches
2021-01-012021-01-012021-01-01 00:002021-01-01
2021-01-032021-01-012021-01-01 00:002021-01-01
2024-02-292024-02-012024-02-01 00:002024-02-01
2024-12-292024-12-012024-12-01 00:002024-12-01
2024-12-302024-12-012024-12-01 00:002024-12-01
2025-01-312025-01-012025-01-01 00:002025-01-01
2025-03-012025-03-012025-03-01 00:002025-03-01
2025-03-022025-03-012025-03-01 00:002025-03-01
nullnullnull

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

Common mistake

Why does casting to DATE not match DATETRUNC('day')?

On a date-time, DATETRUNC keeps the date-time type and sets the time to midnight. CAST(... AS DATE) changes the type; date_trunc('day') matches.

Tableau

DATETRUNC('day', [Ordered])

Tempting SQL

SELECT CAST (Ordered AS DATE) AS result
FROM input
ORDER BY id

SQL that matches

SELECT date_trunc('day', Ordered) AS result
FROM input
ORDER BY id
OrderedTableauTempting SQLSQL that matches
2021-01-01 08:002021-01-01 00:002021-01-012021-01-01 00:00
2021-01-03 18:452021-01-03 00:002021-01-032021-01-03 00:00
2024-02-29 10:152024-02-29 00:002024-02-292024-02-29 00:00
2024-12-29 16:25:402024-12-29 00:002024-12-292024-12-29 00:00
2024-12-30 09:052024-12-30 00:002024-12-302024-12-30 00:00
2025-01-31 23:59:592025-01-31 00:002025-01-312025-01-31 00:00
2025-03-01 00:002025-03-01 00:002025-03-012025-03-01 00:00
2025-03-02 12:30:152025-03-02 00:002025-03-022025-03-02 00:00
nullnullnull

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

What does each date part return, and what is its SQL?

The same order date-times with every date part and week start. The SQL is DuckDB; week starts shift the date so that the chosen day lands on Monday, DuckDB's week start, then shift back. Columns 1 to 9 are the sample rows, by id.

CaseTableau, then SQL (DuckDB)123456789
yearDATETRUNC('year', [Ordered])date_trunc('year', Ordered)2021-01-01 00:002021-01-01 00:002024-01-01 00:002024-01-01 00:002024-01-01 00:002025-01-01 00:002025-01-01 00:002025-01-01 00:00null
quarterDATETRUNC('quarter', [Ordered])date_trunc('quarter', Ordered)2021-01-01 00:002021-01-01 00:002024-01-01 00:002024-10-01 00:002024-10-01 00:002025-01-01 00:002025-01-01 00:002025-01-01 00:00null
monthDATETRUNC('month', [Ordered])date_trunc('month', Ordered)2021-01-01 00:002021-01-01 00:002024-02-01 00:002024-12-01 00:002024-12-01 00:002025-01-01 00:002025-03-01 00:002025-03-01 00:00null
week (Sunday start)DATETRUNC('week', [Ordered])date_trunc('week', Ordered + INTERVAL 1 DAY) - INTERVAL 1 DAY2020-12-27 00:002021-01-03 00:002024-02-25 00:002024-12-29 00:002024-12-29 00:002025-01-26 00:002025-02-23 00:002025-03-02 00:00null
week from SundayDATETRUNC('week', [Ordered], 'sunday')date_trunc('week', Ordered + INTERVAL 1 DAY) - INTERVAL 1 DAY2020-12-27 00:002021-01-03 00:002024-02-25 00:002024-12-29 00:002024-12-29 00:002025-01-26 00:002025-02-23 00:002025-03-02 00:00null
week from MondayDATETRUNC('week', [Ordered], 'monday')date_trunc('week', Ordered)2020-12-28 00:002020-12-28 00:002024-02-26 00:002024-12-23 00:002024-12-30 00:002025-01-27 00:002025-02-24 00:002025-02-24 00:00null
week from TuesdayDATETRUNC('week', [Ordered], 'tuesday')date_trunc('week', Ordered - INTERVAL 1 DAY) + INTERVAL 1 DAY2020-12-29 00:002020-12-29 00:002024-02-27 00:002024-12-24 00:002024-12-24 00:002025-01-28 00:002025-02-25 00:002025-02-25 00:00null
week from WednesdayDATETRUNC('week', [Ordered], 'wednesday')date_trunc('week', Ordered - INTERVAL 2 DAY) + INTERVAL 2 DAY2020-12-30 00:002020-12-30 00:002024-02-28 00:002024-12-25 00:002024-12-25 00:002025-01-29 00:002025-02-26 00:002025-02-26 00:00null
week from ThursdayDATETRUNC('week', [Ordered], 'thursday')date_trunc('week', Ordered - INTERVAL 3 DAY) + INTERVAL 3 DAY2020-12-31 00:002020-12-31 00:002024-02-29 00:002024-12-26 00:002024-12-26 00:002025-01-30 00:002025-02-27 00:002025-02-27 00:00null
week from FridayDATETRUNC('week', [Ordered], 'friday')date_trunc('week', Ordered - INTERVAL 4 DAY) + INTERVAL 4 DAY2021-01-01 00:002021-01-01 00:002024-02-23 00:002024-12-27 00:002024-12-27 00:002025-01-31 00:002025-02-28 00:002025-02-28 00:00null
week from SaturdayDATETRUNC('week', [Ordered], 'saturday')date_trunc('week', Ordered - INTERVAL 5 DAY) + INTERVAL 5 DAY2020-12-26 00:002021-01-02 00:002024-02-24 00:002024-12-28 00:002024-12-28 00:002025-01-25 00:002025-03-01 00:002025-03-01 00:00null
dayDATETRUNC('day', [Ordered])date_trunc('day', Ordered)2021-01-01 00:002021-01-03 00:002024-02-29 00:002024-12-29 00:002024-12-30 00:002025-01-31 00:002025-03-01 00:002025-03-02 00:00null
dayofyearDATETRUNC('dayofyear', [Ordered])date_trunc('day', Ordered)2021-01-01 00:002021-01-03 00:002024-02-29 00:002024-12-29 00:002024-12-30 00:002025-01-31 00:002025-03-01 00:002025-03-02 00:00null
weekdayDATETRUNC('weekday', [Ordered])date_trunc('day', Ordered)2021-01-01 00:002021-01-03 00:002024-02-29 00:002024-12-29 00:002024-12-30 00:002025-01-31 00:002025-03-01 00:002025-03-02 00:00null
hourDATETRUNC('hour', [Ordered])date_trunc('hour', Ordered)2021-01-01 08:002021-01-03 18:002024-02-29 10:002024-12-29 16:002024-12-30 09:002025-01-31 23:002025-03-01 00:002025-03-02 12:00null
minuteDATETRUNC('minute', [Ordered])date_trunc('minute', Ordered)2021-01-01 08:002021-01-03 18:452024-02-29 10:152024-12-29 16:252024-12-30 09:052025-01-31 23:592025-03-01 00:002025-03-02 12:30null
secondDATETRUNC('second', [Ordered])date_trunc('second', Ordered)2021-01-01 08:002021-01-03 18:452024-02-29 10:152024-12-29 16:25:402024-12-30 09:052025-01-31 23:59:592025-03-01 00:002025-03-02 12:30:15null
iso-yearDATETRUNC('iso-year', [Ordered])date_trunc('isoyear', Ordered)2019-12-30 00:002019-12-30 00:002024-01-01 00:002024-01-01 00:002024-12-30 00:002024-12-30 00:002024-12-30 00:002024-12-30 00:00null
iso-quarterDATETRUNC('iso-quarter', [Ordered])date_trunc('isoyear', Ordered) + to_weeks(CAST(13 * least(3, floor((week(Ordered) - 1) / 13)) AS INTEGER))2020-09-28 00:002020-09-28 00:002024-01-01 00:002024-09-30 00:002024-12-30 00:002024-12-30 00:002024-12-30 00:002024-12-30 00:00null
iso-weekDATETRUNC('iso-week', [Ordered])date_trunc('week', Ordered)2020-12-28 00:002020-12-28 00:002024-02-26 00:002024-12-23 00:002024-12-30 00:002025-01-27 00:002025-02-24 00:002025-02-24 00:00null
iso-weekdayDATETRUNC('iso-weekday', [Ordered])date_trunc('day', Ordered)2021-01-01 00:002021-01-03 00:002024-02-29 00:002024-12-29 00:002024-12-30 00:002025-01-31 00:002025-03-01 00:002025-03-02 00:00null

On every row, each Tableau formula and its SQL return the same value.

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 DATE_TRUNC('month', Ordered) AS result
FROM input
ORDER BY id

Snowflake documentation

BigQuery

SELECT DATETIME_TRUNC(Ordered, MONTH) AS result
FROM input
ORDER BY id

BigQuery documentation

What does your own DATETRUNC 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 DATETRUNC?

Does DATETRUNC return a date or a date-time?

It keeps the input's kind: a date-time comes back as a date-time at midnight, a date as a date. In DuckDB, date_trunc on a DATE returns a timestamp, so cast it back with CAST(... AS DATE).

Should I use DATETRUNC to hide the time?

Tableau's help says no: to stop showing the time, change the field's format. Truncate when you need to group by the period.

Which day does an ISO week start on?

Always Monday. DATETRUNC('iso-week', ...) ignores the week start; 'iso-year' starts on the Monday of ISO week 1, which can be in late December.

Where is this documented?