Tableau function Date

Tableau WEEK to SQL

WEEK(date) returns the week of the year as an integer. Week 1 is the week that holds 1 January, and each new week starts on the data source's first day of week (Sunday in US English), so a year can have 53 or 54 partial weeks. DuckDB's week() is the ISO week, which is a different number around New Year. The matching SQL counts days from the start of the year.

Updated · Checked on the xmetric engine

What is the syntax?

WEEK(date)
date
a date or date-time. The week start comes from the data source (Sunday for US English).

Returns an integer from 1 to 54: the week of the year, with week 1 holding 1 January. 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 week of the year was each order placed in?

Tableau

WEEK([Ordered])

SQL (DuckDB)

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

Count the days since 1 January, add the weekday of 1 January (Sunday = 0), and divide by 7. Order 2 on Sunday 3 January 2021 starts week 2; order 4 on Sunday 29 December 2024 starts week 53.

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

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

On which date 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

For a date rather than a number, truncate. Shifting by one day makes Sunday land on DuckDB's Monday week start.

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.

Where does a straight translation go wrong?

Common mistake

Why does week() give 53 for 1 January 2021?

DuckDB's week() is the ISO week: Monday starts, and week 1 is the week with the year's first Thursday. 1 January 2021 is in ISO week 53 of 2020, but it is week 1 in Tableau.

Tableau

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 does strftime('%U') count one week fewer?

%U also starts weeks on Sunday, but days before the year's first Sunday are week 00. Tableau calls those days week 1, so every row of a year that doesn't start on Sunday is one lower, and the result is text.

Tableau

WEEK([Ordered])

Tempting SQL

SELECT strftime(Ordered, '%U') 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:001001
2021-01-03 18:452012
2024-02-29 10:159089
2024-12-29 16:25:40535253
2024-12-30 09:05535253
2025-01-31 23:59:595045
2025-03-01 00:009089
2025-03-02 12:30:15100910
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
  FLOOR((DAYOFYEAR(Ordered) - 1 + DAYOFWEEK(DATE_TRUNC('year', Ordered))) / 7)
    + 1 AS result
FROM input
ORDER BY id

Snowflake documentation

BigQuery

SELECT
  EXTRACT(WEEK FROM Ordered)
    + IF(EXTRACT(DAYOFWEEK FROM DATETIME_TRUNC(Ordered, YEAR)) = 1, 0, 1) AS result
FROM input
ORDER BY id

BigQuery documentation

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

Is WEEK the same as ISOWEEK?

No. 29 December 2024 is week 53 by WEEK (Sunday starts, counted from 1 January) and week 52 by ISOWEEK. 1 January 2021 is week 1 by WEEK and week 53 of 2020 by ISOWEEK.

Can I make WEEK start on Monday?

WEEK takes no week-start argument. Use DATEPART('week', [Ordered], 'monday'), or change the data source's first day of week.

Why does the first week of the year have only a few days?

Week 1 starts on 1 January whatever day that is, and ends before the first week start. In 2025 (1 January is a Wednesday) week 1 is four days long.

Where is this documented?