Date Functions
in Tableau
Master date functions and date truncation to build powerful time series analyses and unlock temporal patterns in your data.
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.
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
Core Date Functions in Tableau
Using DATEPART
// 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.
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
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
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
Example: Monthly Sales Trend
Goal: Show total sales by month as a continuous line chart that preserves year context.
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.
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.
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
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?