Tableau function Date

Tableau YEAR to SQL

YEAR(date) returns the calendar year as an integer: 1 January 2021 is 2021. It is the same as DATEPART('year', date). In DuckDB the match is year(). Don't reach for isoyear(): the ISO year moves the first days of January into the previous year.

Updated · Checked on the xmetric engine

What is the syntax?

YEAR(date)
date
a date or date-time.

Returns an integer: the calendar year 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 year was each order placed in?

Tableau

YEAR([Ordered])

SQL (DuckDB)

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

Orders 1 and 2 are in the first days of 2021, so they return 2021 even though their ISO week belongs to 2020.

OrderedResult
2021-01-01 08:002,021
2021-01-03 18:452,021
2024-02-29 10:152,024
2024-12-29 16:25:402,024
2024-12-30 09:052,024
2025-01-31 23:59:592,025
2025-03-01 00:002,025
2025-03-02 12:30:152,025
null

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

Which fiscal year starting in July does each order fall in?

Tableau

YEAR(DATEADD('month', 6, [Ordered]))

SQL (DuckDB)

SELECT year(Ordered + INTERVAL 6 MONTH) AS result
FROM input
ORDER BY id

Shift the date forward six months, then take the year: March 2025 is fiscal 2025, December 2024 is fiscal 2025 too.

OrderedResult
2021-01-01 08:002,021
2021-01-03 18:452,021
2024-02-29 10:152,024
2024-12-29 16:25:402,025
2024-12-30 09:052,025
2025-01-31 23:59:592,025
2025-03-01 00:002,025
2025-03-02 12:30:152,025
null

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

Where does a straight translation go wrong?

Common mistake

Why does isoyear() put 1 January 2021 in 2020?

The ISO year follows ISO weeks, and 1 January 2021 is a Friday in ISO week 53 of 2020. YEAR is the plain calendar year, so orders 1 and 2 are 2021 in Tableau and 2020 with isoyear().

Tableau

YEAR([Ordered])

Tempting SQL

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

SQL that matches

SELECT year(Ordered) AS result
FROM input
ORDER BY id
OrderedTableauTempting SQLSQL that matches
2021-01-01 08:002,0212,0202,021
2021-01-03 18:452,0212,0202,021
2024-02-29 10:152,0242,0242,024
2024-12-29 16:25:402,0242,0242,024
2024-12-30 09:052,0242,0252,024
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.

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

Snowflake documentation

BigQuery

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

BigQuery documentation

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

Is YEAR the same as DATEPART('year', ...)?

Yes. Both return an integer. DATENAME('year', ...) returns the same digits as a string.

What does YEAR return for a null date?

Null. The last sample row has no order date, and YEAR returns null there, as does year() in SQL.

Why does Tableau show 2,021 instead of 2021?

YEAR returns a number, and the default number format adds a thousands separator. Format the field as a number with no separator, or use DATENAME('year', ...) for a string.

Where is this documented?