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)
SELECTCAST (floor((dayofyear(Ordered) -1+dayofweek(date_trunc('year', Ordered))) /7) +1AS INTEGER) AS result
FROM input
ORDERBY 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.
Ordered
Result
2021-01-0108:00
1
2021-01-0318:45
2
2024-02-2910:15
9
2024-12-2916:25:40
53
2024-12-3009:05
53
2025-01-3123:59:59
5
2025-03-0100:00
9
2025-03-0212:30:15
10
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)
SELECTdate_trunc('week', Ordered +INTERVAL1 DAY)
-INTERVAL1 DAY AS result
FROM input
ORDERBY id
For a date rather than a number, truncate. Shifting by one day makes Sunday land on DuckDB's Monday week start.
Ordered
Result
2021-01-0108:00
2020-12-2700:00
2021-01-0318:45
2021-01-0300:00
2024-02-2910:15
2024-02-2500:00
2024-12-2916:25:40
2024-12-2900:00
2024-12-3009:05
2024-12-2900:00
2025-01-3123:59:59
2025-01-2600:00
2025-03-0100:00
2025-02-2300:00
2025-03-0212:30:15
2025-03-0200: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
SELECTweek(Ordered) AS result
FROM input
ORDERBY id
SQL that matches
SELECTCAST (floor((dayofyear(Ordered) -1+dayofweek(date_trunc('year', Ordered))) /7) +1AS INTEGER) AS result
FROM input
ORDERBY id
Ordered
Tableau
Tempting SQL
SQL that matches
2021-01-0108:00
1
53
1
2021-01-0318:45
2
53
2
2024-02-2910:15
9
9
9
2024-12-2916:25:40
53
52
53
2024-12-3009:05
53
1
53
2025-01-3123:59:59
5
5
5
2025-03-0100:00
9
9
9
2025-03-0212:30:15
10
9
10
null
null
null
✓ 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
SELECTstrftime(Ordered, '%U') AS result
FROM input
ORDERBY id
SQL that matches
SELECTCAST (floor((dayofyear(Ordered) -1+dayofweek(date_trunc('year', Ordered))) /7) +1AS INTEGER) AS result
FROM input
ORDERBY id
Ordered
Tableau
Tempting SQL
SQL that matches
2021-01-0108:00
1
00
1
2021-01-0318:45
2
01
2
2024-02-2910:15
9
08
9
2024-12-2916:25:40
53
52
53
2024-12-3009:05
53
52
53
2025-01-3123:59:59
5
04
5
2025-03-0100:00
9
08
9
2025-03-0212:30:15
10
09
10
null
null
null
✓ 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
SELECTFLOOR((DAYOFYEAR(Ordered) -1+DAYOFWEEK(DATE_TRUNC('year', Ordered))) /7)
+1AS result
FROM input
ORDERBY id
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.