Tableau function Date

Tableau DATEPART to SQL

DATEPART(date_part, date, [start_of_week]) returns one part of a date as an integer: DATEPART('month', #2025-03-01#) is 3. Most parts map to a DuckDB function of the same name. Weeks don't: Tableau's 'week' counts from 1 January with Sunday starts (in US English), while DuckDB's week() is the ISO week, and 'weekday' is 1 for Sunday where dayofweek() gives 0.

Updated · Checked on the xmetric engine

What is the syntax?

DATEPART(date_part, date, [start_of_week])
date_part
a quoted string: 'year', 'quarter', 'month', 'dayofyear', 'day', 'weekday', 'week', 'hour', 'minute', 'second', or 'iso-year', 'iso-quarter', 'iso-week', 'iso-weekday'.
date
a date or date-time.
start_of_week
optional: 'sunday' to 'saturday'. Without it, the data source's week start is used.

Returns an integer: the requested part of date. 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 month number is each order in?

Tableau

DATEPART('month', [Ordered])

SQL (DuckDB)

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

Calendar parts (year, quarter, month, day, hour, minute, second) map one to one.

OrderedResult
2021-01-01 08:001
2021-01-03 18:451
2024-02-29 10:152
2024-12-29 16:25:4012
2024-12-30 09:0512
2025-01-31 23:59:591
2025-03-01 00:003
2025-03-02 12:30:153
null

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

Which day of the week is each order, with Sunday as 1?

Tableau

DATEPART('weekday', [Ordered])

SQL (DuckDB)

SELECT dayofweek(Ordered) + 1 AS result
FROM input
ORDER BY id

dayofweek() counts Sunday as 0, so add 1. Orders 2, 4 and 8 are Sundays (1); order 7 is a Saturday (7).

OrderedResult
2021-01-01 08:006
2021-01-03 18:451
2024-02-29 10:155
2024-12-29 16:25:401
2024-12-30 09:052
2025-01-31 23:59:596
2025-03-01 00:007
2025-03-02 12:30:151
null

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

Which week of the year is each order, if weeks start on Monday?

Tableau

DATEPART('week', [Ordered], 'monday')

SQL (DuckDB)

SELECT
  CAST (floor((dayofyear(Ordered) - 1 + (dayofweek(date_trunc('year', Ordered)) + 6) % 7) / 7) + 1 AS INTEGER) AS result
FROM input
ORDER BY id

Week 1 still starts on 1 January; only the later week starts move. Count the days since 1 January plus the weekday of 1 January in a Monday-first week, divide by 7.

OrderedResult
2021-01-01 08:001
2021-01-03 18:451
2024-02-29 10:159
2024-12-29 16:25:4052
2024-12-30 09:0553
2025-01-31 23:59:595
2025-03-01 00:009
2025-03-02 12:30:159
null

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

Where does a straight translation go wrong?

Common mistake

Why does week() not match DATEPART('week')?

DuckDB's week() is the ISO week. Tableau numbers weeks from 1 January with Sunday starts, so 1 January 2021 is week 1 in Tableau and 53 in DuckDB, and 29 December 2024 is 53 against 52.

Tableau

DATEPART('week', [Ordered])

Tempting SQL

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

SQL that matches

SELECT
  CAST (floor((dayofyear(Ordered) - 1 + dayofweek(date_trunc('year', Ordered))) / 7) + 1 AS INTEGER) AS result
FROM input
ORDER BY id
OrderedTableauTempting SQLSQL that matches
2021-01-01 08:001531
2021-01-03 18:452532
2024-02-29 10:15999
2024-12-29 16:25:40535253
2024-12-30 09:0553153
2025-01-31 23:59:59555
2025-03-01 00:00999
2025-03-02 12:30:1510910
nullnullnull

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

Common mistake

Why is dayofweek() one lower than DATEPART('weekday')?

DuckDB counts Sunday as 0 and Saturday as 6. Tableau, with a Sunday week start, counts Sunday as 1 and Saturday as 7.

Tableau

DATEPART('weekday', [Ordered])

Tempting SQL

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

SQL that matches

SELECT dayofweek(Ordered) + 1 AS result
FROM input
ORDER BY id
OrderedTableauTempting SQLSQL that matches
2021-01-01 08:00656
2021-01-03 18:45101
2024-02-29 10:15545
2024-12-29 16:25:40101
2024-12-30 09:05212
2025-01-31 23:59:59656
2025-03-01 00:00767
2025-03-02 12:30:15101
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 year() not match DATEPART('iso-year')?

The ISO year follows ISO weeks: 1 January 2021 belongs to 2020, 30 December 2024 to 2025. Use isoyear().

Tableau

DATEPART('iso-year', [Ordered])

Tempting SQL

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

SQL that matches

SELECT isoyear(Ordered) AS result
FROM input
ORDER BY id
OrderedTableauTempting SQLSQL that matches
2021-01-01 08:002,0202,0212,020
2021-01-03 18:452,0202,0212,020
2024-02-29 10:152,0242,0242,024
2024-12-29 16:25:402,0242,0242,024
2024-12-30 09:052,0252,0242,025
2025-01-31 23:59:592,0252,0252,025
2025-03-01 00:002,0252,0252,025
2025-03-02 12:30:152,0252,0252,025
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 dates with every date part and week start. The SQL is DuckDB; Tableau weeks count from 1 January, so the SQL adds the weekday of 1 January (shifted to the chosen week start) to the day of the year. Columns 1 to 9 are the sample rows, by id.

CaseTableau, then SQL (DuckDB)123456789
yearDATEPART('year', [Ordered])year(Ordered)2,0212,0212,0242,0242,0242,0252,0252,025null
quarterDATEPART('quarter', [Ordered])quarter(Ordered)11144111null
monthDATEPART('month', [Ordered])month(Ordered)1121212133null
dayDATEPART('day', [Ordered])day(Ordered)132929303112null
dayofyearDATEPART('dayofyear', [Ordered])dayofyear(Ordered)1360364365316061null
hourDATEPART('hour', [Ordered])hour(Ordered)8181016923012null
minuteDATEPART('minute', [Ordered])minute(Ordered)0451525559030null
secondDATEPART('second', [Ordered])second(Ordered)00040059015null
week (Sunday start)DATEPART('week', [Ordered])CAST(floor((dayofyear(Ordered) - 1 + dayofweek(date_trunc('year', Ordered))) / 7) + 1 AS INTEGER)12953535910null
week from SundayDATEPART('week', [Ordered], 'sunday')CAST(floor((dayofyear(Ordered) - 1 + dayofweek(date_trunc('year', Ordered))) / 7) + 1 AS INTEGER)12953535910null
week from MondayDATEPART('week', [Ordered], 'monday')CAST(floor((dayofyear(Ordered) - 1 + (dayofweek(date_trunc('year', Ordered)) + 6) % 7) / 7) + 1 AS INTEGER)1195253599null
week from TuesdayDATEPART('week', [Ordered], 'tuesday')CAST(floor((dayofyear(Ordered) - 1 + (dayofweek(date_trunc('year', Ordered)) + 5) % 7) / 7) + 1 AS INTEGER)11105353599null
week from WednesdayDATEPART('week', [Ordered], 'wednesday')CAST(floor((dayofyear(Ordered) - 1 + (dayofweek(date_trunc('year', Ordered)) + 4) % 7) / 7) + 1 AS INTEGER)11105353599null
week from ThursdayDATEPART('week', [Ordered], 'thursday')CAST(floor((dayofyear(Ordered) - 1 + (dayofweek(date_trunc('year', Ordered)) + 3) % 7) / 7) + 1 AS INTEGER)1110535361010null
week from FridayDATEPART('week', [Ordered], 'friday')CAST(floor((dayofyear(Ordered) - 1 + (dayofweek(date_trunc('year', Ordered)) + 2) % 7) / 7) + 1 AS INTEGER)119535361010null
week from SaturdayDATEPART('week', [Ordered], 'saturday')CAST(floor((dayofyear(Ordered) - 1 + (dayofweek(date_trunc('year', Ordered)) + 1) % 7) / 7) + 1 AS INTEGER)129535351010null
weekday (Sunday start)DATEPART('weekday', [Ordered])dayofweek(Ordered) + 161512671null
weekday from SundayDATEPART('weekday', [Ordered], 'sunday')dayofweek(Ordered) + 161512671null
weekday from MondayDATEPART('weekday', [Ordered], 'monday')(dayofweek(Ordered) + 6) % 7 + 157471567null
weekday from TuesdayDATEPART('weekday', [Ordered], 'tuesday')(dayofweek(Ordered) + 5) % 7 + 146367456null
weekday from WednesdayDATEPART('weekday', [Ordered], 'wednesday')(dayofweek(Ordered) + 4) % 7 + 135256345null
weekday from ThursdayDATEPART('weekday', [Ordered], 'thursday')(dayofweek(Ordered) + 3) % 7 + 124145234null
weekday from FridayDATEPART('weekday', [Ordered], 'friday')(dayofweek(Ordered) + 2) % 7 + 113734123null
weekday from SaturdayDATEPART('weekday', [Ordered], 'saturday')(dayofweek(Ordered) + 1) % 7 + 172623712null
iso-yearDATEPART('iso-year', [Ordered])isoyear(Ordered)2,0202,0202,0242,0242,0252,0252,0252,025null
iso-quarterDATEPART('iso-quarter', [Ordered])CASE WHEN week(Ordered) = 53 THEN 4 ELSE CAST(floor((week(Ordered) - 1) / 13) + 1 AS INTEGER) END44141111null
iso-weekDATEPART('iso-week', [Ordered])week(Ordered)53539521599null
iso-weekdayDATEPART('iso-weekday', [Ordered])isodow(Ordered)57471567null

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

Snowflake documentation

BigQuery

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

BigQuery documentation

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

What is the difference between DATEPART and DATENAME?

DATEPART returns an integer (3), DATENAME a string ("March"). For numeric parts DATENAME returns the same digits as text. DATEPART is the faster of the two.

Which day does a Tableau week start on?

The data source's first day of week, Sunday for US English. The third argument overrides it for 'week' and 'weekday': DATEPART('week', [Ordered], 'monday'). ISO parts always start on Monday.

What does DATEPART return for a null date?

Null, for every date part.

Where is this documented?