Tableau function Date

Tableau DATEDIFF to SQL

DATEDIFF(date_part, start, end) counts how many date_part boundaries lie between two dates, not how many full periods passed: from 31 January to 1 February is 1 month. DuckDB's date_diff counts the same way for days, months, quarters and years. Weeks need a different SQL, because date_diff('week') counts whole 7-day spans.

Updated · Checked on the xmetric engine

What is the syntax?

DATEDIFF(date_part, start_date, end_date, [start_of_week])
date_part
a quoted string: 'second', 'minute', 'hour', 'day', 'week', 'month', 'quarter', 'year', or an ISO part such as 'iso-week'.
start_date, end_date
dates or date-times. The result is positive when end_date is later.
start_of_week
optional, for 'week' only: 'sunday' to 'saturday'. Without it, the data source's week start is used.

Returns an integer: end_date minus start_date in date_part boundaries. Null if either date is null.

What does it return, and what is the SQL?

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

How many days are there between order and shipment?

Tableau

DATEDIFF('day', [Ordered], [Shipped])

SQL (DuckDB)

SELECT date_diff('day', Ordered, Shipped) AS result
FROM input
ORDER BY id

Order 1 shipped 23 hours later but on the next calendar day, so it counts as 1. Order 4 shipped the same day: 0.

OrderedShippedResult
2026-01-31 09:002026-02-01 08:001
2026-03-07 23:302026-03-09 00:152
2025-12-31 18:002026-01-02 10:002
2026-04-10 08:002026-04-10 17:45:300
2026-01-05 12:002026-03-04 12:0058
2026-05-01 10:00null

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

How many calendar months lie between two dates?

Tableau

DATEDIFF('month', [Ordered], [Shipped])

SQL (DuckDB)

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

31 January to 1 February crosses one month boundary, so order 1 returns 1 after a single day.

OrderedShippedResult
2026-01-31 09:002026-02-01 08:001
2026-03-07 23:302026-03-09 00:150
2025-12-31 18:002026-01-02 10:001
2026-04-10 08:002026-04-10 17:45:300
2026-01-05 12:002026-03-04 12:002
2026-05-01 10:00null

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

How many weeks, if weeks start on Monday?

Tableau

DATEDIFF('week', [Ordered], [Shipped], 'monday')

SQL (DuckDB)

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

Truncate both dates to the start of their week, then count the weeks between the two starts. Order 2 goes from Saturday night to Monday, so it crosses one Monday.

OrderedShippedResult
2026-01-31 09:002026-02-01 08:000
2026-03-07 23:302026-03-09 00:151
2025-12-31 18:002026-01-02 10:000
2026-04-10 08:002026-04-10 17:45:300
2026-01-05 12:002026-03-04 12:008
2026-05-01 10:00null

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

Where does a straight translation go wrong?

Common mistake

Why does date_diff('week') give a smaller week count?

Tableau's week count is the number of week starts crossed (Sunday here). DuckDB's date_diff('week') is whole 7-day spans, so a Saturday-to-Sunday order is 1 week in Tableau and 0 in the SQL.

Tableau

DATEDIFF('week', [Ordered], [Shipped])

Tempting SQL

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

SQL that matches

SELECT
  date_diff('week', date_trunc('week', Ordered + INTERVAL 1 DAY), date_trunc('week', Shipped + INTERVAL 1 DAY)) AS result
FROM input
ORDER BY id
OrderedShippedTableauTempting SQLSQL that matches
2026-01-31 09:002026-02-01 08:00101
2026-03-07 23:302026-03-09 00:15101
2025-12-31 18:002026-01-02 10:00000
2026-04-10 08:002026-04-10 17:45:30000
2026-01-05 12:002026-03-04 12:00888
2026-05-01 10:00nullnullnull

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

Common mistake

Why is dividing seconds by 86,400 wrong for days?

Tableau counts midnights. Elapsed time divided by a day's seconds gives 0 for order 1, which shipped 23 hours later on the next day.

Tableau

DATEDIFF('day', [Ordered], [Shipped])

Tempting SQL

SELECT
  floor((epoch(Shipped) - epoch(Ordered)) / 86400) AS result
FROM input
ORDER BY id

SQL that matches

SELECT date_diff('day', Ordered, Shipped) AS result
FROM input
ORDER BY id
OrderedShippedTableauTempting SQLSQL that matches
2026-01-31 09:002026-02-01 08:00101
2026-03-07 23:302026-03-09 00:15212
2025-12-31 18:002026-01-02 10:00212
2026-04-10 08:002026-04-10 17:45:30000
2026-01-05 12:002026-03-04 12:00585858
2026-05-01 10:00nullnullnull

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_sub('month') give fewer months?

DuckDB's date_sub answers a different question: how many complete months have passed. From 5 January to 4 March is 1 full month but 2 month boundaries.

Tableau

DATEDIFF('month', [Ordered], [Shipped])

Tempting SQL

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

SQL that matches

SELECT date_diff('month', Ordered, Shipped) AS result
FROM input
ORDER BY id
OrderedShippedTableauTempting SQLSQL that matches
2026-01-31 09:002026-02-01 08:00101
2026-03-07 23:302026-03-09 00:15000
2025-12-31 18:002026-01-02 10:00101
2026-04-10 08:002026-04-10 17:45:30000
2026-01-05 12:002026-03-04 12:00212
2026-05-01 10:00nullnullnull

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 two columns with each date part. The SQL is DuckDB; week starts shift the dates so that the chosen day lands on Monday, DuckDB's week start. Columns 1 to 6 are the sample rows, by id.

CaseTableau, then SQL (DuckDB)123456
secondDATEDIFF('second', [Ordered], [Shipped])date_diff('second', Ordered, Shipped)82,80089,100144,00035,1305,011,200null
minuteDATEDIFF('minute', [Ordered], [Shipped])date_diff('minute', Ordered, Shipped)1,3801,4852,40058583,520null
hourDATEDIFF('hour', [Ordered], [Shipped])date_diff('hour', Ordered, Shipped)23254091,392null
dayDATEDIFF('day', [Ordered], [Shipped])date_diff('day', Ordered, Shipped)122058null
week (Sunday start)DATEDIFF('week', [Ordered], [Shipped])date_diff('week', date_trunc('week', Ordered + INTERVAL 1 DAY), date_trunc('week', Shipped + INTERVAL 1 DAY))11008null
week from SundayDATEDIFF('week', [Ordered], [Shipped], 'sunday')date_diff('week', date_trunc('week', Ordered + INTERVAL 1 DAY), date_trunc('week', Shipped + INTERVAL 1 DAY))11008null
week from MondayDATEDIFF('week', [Ordered], [Shipped], 'monday')date_diff('week', date_trunc('week', Ordered), date_trunc('week', Shipped))01008null
week from TuesdayDATEDIFF('week', [Ordered], [Shipped], 'tuesday')date_diff('week', date_trunc('week', Ordered - INTERVAL 1 DAY), date_trunc('week', Shipped - INTERVAL 1 DAY))00009null
week from WednesdayDATEDIFF('week', [Ordered], [Shipped], 'wednesday')date_diff('week', date_trunc('week', Ordered - INTERVAL 2 DAY), date_trunc('week', Shipped - INTERVAL 2 DAY))00009null
week from ThursdayDATEDIFF('week', [Ordered], [Shipped], 'thursday')date_diff('week', date_trunc('week', Ordered - INTERVAL 3 DAY), date_trunc('week', Shipped - INTERVAL 3 DAY))00108null
week from FridayDATEDIFF('week', [Ordered], [Shipped], 'friday')date_diff('week', date_trunc('week', Ordered - INTERVAL 4 DAY), date_trunc('week', Shipped - INTERVAL 4 DAY))00108null
week from SaturdayDATEDIFF('week', [Ordered], [Shipped], 'saturday')date_diff('week', date_trunc('week', Ordered - INTERVAL 5 DAY), date_trunc('week', Shipped - INTERVAL 5 DAY))00008null
monthDATEDIFF('month', [Ordered], [Shipped])date_diff('month', Ordered, Shipped)10102null
quarterDATEDIFF('quarter', [Ordered], [Shipped])date_diff('quarter', Ordered, Shipped)00100null
yearDATEDIFF('year', [Ordered], [Shipped])date_diff('year', Ordered, Shipped)00100null
dayofyearDATEDIFF('dayofyear', [Ordered], [Shipped])date_diff('day', Ordered, Shipped)122058null
weekdayDATEDIFF('weekday', [Ordered], [Shipped])date_diff('day', Ordered, Shipped)122058null
iso-weekDATEDIFF('iso-week', [Ordered], [Shipped])date_diff('week', date_trunc('week', Ordered), date_trunc('week', Shipped))01008null
iso-weekdayDATEDIFF('iso-weekday', [Ordered], [Shipped])date_diff('day', Ordered, Shipped)122058null
iso-quarterDATEDIFF('iso-quarter', [Ordered], [Shipped])(isoyear(Shipped) * 4 + least(4, floor((week(Shipped) - 1) / 13))) - (isoyear(Ordered) * 4 + least(4, floor((week(Ordered) - 1) / 13)))00000null
iso-yearDATEDIFF('iso-year', [Ordered], [Shipped])isoyear(Shipped) - isoyear(Ordered)00000null

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 DATEDIFF(day, Ordered, Shipped) AS result
FROM input
ORDER BY id

Snowflake documentation

BigQuery

SELECT DATETIME_DIFF(Shipped, Ordered, DAY) AS result
FROM input
ORDER BY id

BigQuery documentation

What does your own DATEDIFF formula return?

Edit the formula and run it on the 6 sample rows. Fields: [id], [Ordered], [Shipped].

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

Which functions go with it?

What do people ask about DATEDIFF?

Does DATEDIFF round, or count whole periods?

Neither. It counts boundaries crossed. 23:00 to 01:00 the next day is 1 day; 1 January to 31 December of the same year is 0 years.

Why is my DATEDIFF negative?

The start date is after the end date. DATEDIFF('day', [Shipped], [Ordered]) returns -1 where the order shipped the next day. Swap the arguments, or wrap it in ABS.

What does DATEDIFF return when a date is null?

Null. The sample's last order has no ship date, so every example returns null on that row. SQL date_diff does the same.

Which day does a Tableau week start on?

The data source's locale decides; for US English it's Sunday. The optional fourth argument overrides it: DATEDIFF('week', [Ordered], [Shipped], 'monday').

How do I count working days between two dates?

DATEDIFF has no business-day part. 'weekday' counts days, the same as 'day'. To skip weekends, take the day count and subtract the Saturdays and Sundays in between, or join to a calendar table in SQL.

Where is this documented?