Bird
Raised Fist0
Tableaubi_tool~8 mins

Date calculations (DATEDIFF, DATEADD) in Tableau - Dashboard Guide

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Dashboard Mode - Date calculations (DATEDIFF, DATEADD)
Dashboard Goal

Understand how sales change over time by calculating the difference in days between order and ship dates, and analyze sales trends by shifting dates forward or backward.

Sample Data
Order IDOrder DateShip DateSales
10012024-01-012024-01-05250
10022024-01-032024-01-06450
10032024-01-072024-01-10300
10042024-01-102024-01-15500
10052024-01-122024-01-14150
Dashboard Components
  • KPI Card: Average Shipping Time (Days)
    Formula: DATEDIFF('day', [Order Date], [Ship Date])
    Calculation: Calculate days between order and ship dates for each order, then average.
    Result: (4 + 3 + 3 + 5 + 2) / 5 = 3.4 days
  • Bar Chart: Sales by Order Date
    Shows total sales for each order date.
    Formula: SUM([Sales]) grouped by [Order Date]
  • Line Chart: Sales Trend Shifted by 7 Days
    Formula: DATEADD('day', 7, [Order Date]) shifts order dates 7 days forward.
    Shows sales trend as if orders happened one week later.
  • Table: Orders with Shipping Delay
    Columns: Order ID, Order Date, Ship Date, Shipping Delay (Days)
    Shipping Delay Formula: DATEDIFF('day', [Order Date], [Ship Date])
Dashboard Layout
+-----------------------------+-----------------------------+
| Average Shipping Time (KPI) | Sales by Order Date (Bar)   |
|                             |                             |
+-----------------------------+-----------------------------+
| Sales Trend Shifted by 7 Days (Line Chart)               |
|                                                         |
+---------------------------------------------------------+
| Orders with Shipping Delay (Table)                       |
+---------------------------------------------------------+
Interactivity

A date filter allows selecting a range of order dates. When the user changes the date range:

  • The Sales by Order Date bar chart updates to show sales only for orders in the selected date range.
  • The Average Shipping Time KPI recalculates the average shipping days for orders in the filtered range.
  • The Sales Trend Shifted by 7 Days line chart updates to reflect sales shifted by 7 days but only for filtered orders.
  • The Orders with Shipping Delay table shows only orders within the selected date range.
Self Check

If you add a filter to show only orders with Order Date after 2024-01-05, which components update and what changes occur?

  • Average Shipping Time KPI: Recalculates average shipping days for orders from 2024-01-07, 2024-01-10, and 2024-01-12 only.
  • Sales by Order Date Bar Chart: Shows sales for 2024-01-07, 2024-01-10, and 2024-01-12 only.
  • Sales Trend Shifted by 7 Days Line Chart: Updates to show sales shifted by 7 days for filtered orders only.
  • Orders with Shipping Delay Table: Displays only orders with order dates after 2024-01-05.
Key Result
Dashboard shows average shipping time, sales by order date, and sales trends shifted by 7 days using date calculations.

Practice

(1/5)
1. What does the Tableau function DATEDIFF('day', #2024-01-01#, #2024-01-10#) return?
easy
A. 10
B. 11
C. 9
D. Error

Solution

  1. Step 1: Understand DATEDIFF parameters

    DATEDIFF counts the number of boundaries crossed between two dates in the specified unit, here 'day'.
  2. Step 2: Calculate days between 2024-01-01 and 2024-01-10

    From Jan 1 to Jan 10, there are 9 full days difference, but DATEDIFF counts the number of day boundaries crossed, which is 10 (Jan 1 to Jan 2 is 1, ... Jan 9 to Jan 10 is 9, plus the starting boundary counts as well).
  3. Final Answer:

    10 -> Option A
  4. Quick Check:

    DATEDIFF('day', start, end) counts days crossed = 10 [OK]
Hint: DATEDIFF counts boundaries crossed, not total days [OK]
Common Mistakes:
  • Counting both start and end dates as full days
  • Confusing DATEDIFF with DATEADD
  • Using wrong unit like 'month' instead of 'day'
2. Which of the following is the correct syntax to add 3 months to a date field [Order Date] in Tableau?
easy
A. DATEADD([Order Date], 3, 'month')
B. DATEADD('3 months', [Order Date])
C. DATEDIFF('month', [Order Date], 3)
D. DATEADD('month', 3, [Order Date])

Solution

  1. Step 1: Recall DATEADD syntax

    DATEADD takes three arguments: date part as string, number to add, and the date field.
  2. Step 2: Match correct argument order

    DATEADD('month', 3, [Order Date]) matches syntax: DATEADD('month', 3, [Order Date]). Others have wrong order or wrong function.
  3. Final Answer:

    DATEADD('month', 3, [Order Date]) -> Option D
  4. Quick Check:

    Correct DATEADD syntax = DATEADD('month', 3, [Order Date]) [OK]
Hint: DATEADD('unit', number, date) is the correct order [OK]
Common Mistakes:
  • Swapping argument order
  • Using DATEDIFF instead of DATEADD
  • Passing units as part of number argument
3. What is the result of this Tableau calculation?
DATEDIFF('week', #2024-01-01#, DATEADD('day', 15, #2024-01-01#))
medium
A. 2
B. 3
C. 1
D. 0

Solution

  1. Step 1: Calculate DATEADD('day', 15, #2024-01-01#)

    Adding 15 days to Jan 1, 2024 results in Jan 16, 2024.
  2. Step 2: Calculate DATEDIFF in weeks between Jan 1 and Jan 16

    Weeks crossed: Jan 1 to Jan 8 (1 week), Jan 8 to Jan 15 (2 weeks), Jan 15 to Jan 16 does not complete another week. So total 2 weeks difference.
  3. Final Answer:

    2 -> Option A
  4. Quick Check:

    DATEDIFF('week', start, end) counts full weeks crossed = 2 [OK]
Hint: Add days first, then count weeks crossed [OK]
Common Mistakes:
  • Counting partial weeks as full weeks
  • Mixing up order of DATEADD and DATEDIFF
  • Using wrong date formats
4. You wrote this Tableau formula but it gives an error:
DATEADD('day', '5', [Ship Date])
What is the problem?
medium
A. The date field [Ship Date] must be a string
B. The date part 'day' should be in uppercase
C. The number of days should not be in quotes
D. DATEADD cannot add days, only months

Solution

  1. Step 1: Check argument types in DATEADD

    DATEADD expects the second argument as a number, not a string.
  2. Step 2: Identify error cause

    Using '5' (string) instead of 5 (number) causes a type error.
  3. Final Answer:

    The number of days should not be in quotes -> Option C
  4. Quick Check:

    Number argument must be numeric, not string [OK]
Hint: Numbers in DATEADD must be numeric, no quotes [OK]
Common Mistakes:
  • Putting numbers in quotes
  • Assuming case sensitivity for 'day'
  • Thinking DATEADD only works with months
5. You want to find how many full quarters have passed between #2023-02-15# and a date 200 days later. Which Tableau formula correctly calculates this?
hard
A. DATEDIFF('month', #2023-02-15#, DATEADD('day', 200, #2023-02-15#)) / 3
B. DATEDIFF('quarter', #2023-02-15#, DATEADD('day', 200, #2023-02-15#))
C. DATEDIFF('quarter', DATEADD('day', 200, #2023-02-15#), #2023-02-15#)
D. DATEADD('quarter', 200, #2023-02-15#)

Solution

  1. Step 1: Calculate the date 200 days after Feb 15, 2023

    Using DATEADD('day', 200, #2023-02-15#) gives the target date.
  2. Step 2: Use DATEDIFF with 'quarter' to count full quarters passed

    DATEDIFF('quarter', start_date, end_date) counts how many quarter boundaries are crossed.
  3. Step 3: Evaluate options

    DATEDIFF('quarter', #2023-02-15#, DATEADD('day', 200, #2023-02-15#)) correctly uses DATEDIFF with 'quarter' and correct date order. DATEDIFF('month', #2023-02-15#, DATEADD('day', 200, #2023-02-15#)) / 3 divides months by 3 but may give decimals, not full quarters. DATEDIFF('quarter', DATEADD('day', 200, #2023-02-15#), #2023-02-15#) reverses dates causing negative result. DATEADD('quarter', 200, #2023-02-15#) misuses DATEADD.
  4. Final Answer:

    DATEDIFF('quarter', #2023-02-15#, DATEADD('day', 200, #2023-02-15#)) -> Option B
  5. Quick Check:

    Use DATEDIFF('quarter', start, end) for full quarters [OK]
Hint: Use DATEDIFF with 'quarter' and correct date order [OK]
Common Mistakes:
  • Dividing months by 3 instead of using 'quarter'
  • Swapping start and end dates
  • Using DATEADD instead of DATEDIFF for difference