01 / 20

Date Functions
in Tableau

Master date functions and date truncation to build powerful time series analyses and unlock temporal patterns in your data.

Tableau Data Analytics

What Are Date Functions?

Date functions are built-in Tableau calculations that let you extract, manipulate, and transform date and datetime fields. They turn raw timestamps into meaningful time-based dimensions for analysis.

Extract
Pull out the year, month, day, or quarter from any date field
Transform
Truncate dates to a specific level of granularity for grouping
Calculate
Compute differences between dates or add intervals to dates

Why Dates Matter in Analytics

Time is the most common axis in data visualization. Nearly every business question involves trends over time: monthly revenue, weekly active users, or quarterly growth. Date functions give you precise control over how time is represented.

The Date Hierarchy

YEAR QUARTER MONTH DAY Coarser 2024 Q2 2024 June 2024 June 15, 2024 Finer

Core Date Functions in Tableau

DATEPART
Returns an integer for a part of the date (e.g., month number 6)
DATENAME
Returns a string for a part of the date (e.g., "June")
DATETRUNC
Truncates a date to a specified level, returning a date
DATEDIFF
Returns the difference between two dates in specified units

Using DATEPART

DATEPART('month', [Order Date])
// Returns: 6 (for June)

DATEPART('year', [Order Date])
// Returns: 2024

DATEPART('weekday', [Order Date])
// Returns: 1-7 (Sunday-Saturday)

Understanding DATETRUNC

DATETRUNC rounds a date down to the beginning of a specified time period. It returns a full date value, not a number: making it ideal for time series grouping.

June 15, 2024 INPUT DATE DATETRUNC ('month', [date]) June 1, 2024 TRUNCATED TO MONTH START DATETRUNC('quarter', date) → April 1, 2024

DATEPART vs DATETRUNC

DATEPART DATETRUNC
Returns Integer Date
Example output 6 June 1, 2024
Best for Filtering, calculations Time series grouping
Axis type Discrete (categories) Continuous (timeline)
Preserves year? No: month 6 from any year Yes: \( \text{June 2024} \neq \text{June 2023} \)

DATEDIFF and DATEADD

// How many days between order and ship?
DATEDIFF('day', [Order Date], [Ship Date])
// Returns: 4

// What date is 30 days from the order?
DATEADD('day', 30, [Order Date])
// Returns: July 15, 2024
DATEDIFF
Measures the gap between two dates. Always: start date first, end date second.
DATEADD
Adds a specified number of intervals to a date. Use negative numbers to subtract.

Date Part String Values

Every date function takes a date_part string as its first argument. These are the valid values you can use:

String Level Example Use
'year' Year Annual trends
'quarter' Quarter Quarterly reports
'month' Month Monthly KPIs
'week' Week Weekly patterns
'day' Day Daily granularity
'weekday' Day of week Weekday vs weekend

How to Apply Date Functions

1
Identify the question. Are you grouping, filtering, or calculating with dates?
2
Choose the granularity. Decide if you need year, quarter, month, week, or day.
3
Select the function. Use DATETRUNC for time series axes, DATEPART for discrete grouping, DATEDIFF for intervals.
4
Write the calculated field. Syntax: \( \texttt{FUNCTION('date\_part', [Date Field])} \).
5
Verify the output. Check the data type: continuous date (green pill) vs discrete integer (blue pill).

Example: Monthly Sales Trend

Goal: Show total sales by month as a continuous line chart that preserves year context.

// Create a calculated field:
DATETRUNC('month', [Order Date])

// Place on Columns shelf as a continuous date
// Place SUM(Sales) on Rows shelf
// Result: smooth line chart, month by month

Example: Day-of-Week Analysis

Goal: Find which day of the week has the highest average order value.

// Create calculated fields:
DATENAME('weekday', [Order Date])
// Returns: "Monday", "Tuesday", etc.

DATEPART('weekday', [Order Date])
// Returns: 1 (Sun), 2 (Mon), ... 7 (Sat)

Example: Shipping Delay Calculation

Goal: Calculate the average number of days between order placement and shipment, grouped by quarter.

// Step 1: Calculate delay per order
DATEDIFF('day', [Order Date], [Ship Date])

// Step 2: Truncate to quarter for grouping
DATETRUNC('quarter', [Order Date])

// Step 3: In the view
// Columns: DATETRUNC (continuous)
// Rows: AVG(DATEDIFF calculation)

Common Mistakes

✕ Using \( \texttt{DATEPART} \) for Time Series
\( \texttt{DATEPART} \) loses year context. Month 3 from 2023 and 2024 become the same value. Use \( \texttt{DATETRUNC} \) for continuous time axes.
✕ Wrong DATEDIFF Argument Order
Putting end date before start date returns negative numbers. Always use: \( \texttt{(part, start, end)} \).
✕ Forgetting Quote Marks
Date part strings must be in single quotes: \( \texttt{'month'} \) not \( \texttt{month} \). Without quotes, Tableau throws a syntax error.

Function Summary Table

Function Returns Syntax Use Case
DATEPART Integer \( \texttt{DATEPART('part', date)} \) Filter, discrete group
DATENAME String \( \texttt{DATENAME('part', date)} \) Display labels
DATETRUNC Date \( \texttt{DATETRUNC('part', date)} \) Continuous time axis
DATEDIFF Integer \( \texttt{DATEDIFF('part', start, end)} \) Duration between dates
DATEADD Date \( \texttt{DATEADD('part', n, date)} \) Offset a date

Question 1 of 5

What does \( \texttt{DATETRUNC('month', \#June 15, 2024\#)} \) return?

Question 2 of 5

You want a continuous time series line chart showing sales by month. Which function should you use on the Columns shelf?

Question 3 of 5

What is the output of \( \texttt{DATEDIFF('day', \#March 1, 2024\#, \#March 10, 2024\#)} \)?

Question 4 of 5

Which function returns the string "Wednesday" from a date field?

Question 5 of 5

You use \( \texttt{DATEPART('month', [Order Date])} \) on a dataset spanning 2022-2024. What happens to data from January 2023 and January 2024?