Date and time functions

Calculations in Tableau

Maarten Van den Broeck

Content Developer at DataCamp

What is time series analysis?

Time series analysis - study of data in particular periods or intervals, which often includes trend analysis, seasonal comparisons of data and study of rolling time-periods

  • Examples of time series analysis:

Line chart presenting various types of time-series analysis

Calculations in Tableau

Date and time as data types

Date or DateTime data types:

  • Data type storing calendar date/time
  • May be auto-recognized by Tableau
  • Dependent on data locale (1)
  • Represented in Tableau with calendar symbol:

Tableau pictograms representing Date and DateTime data types

Date can have various output formats:

  • 21-Jan-2020
  • Monday, 31. December 19
  • 31/10/12
  • 10/31/12
  • jan.22
  • 2022-W05
  • ...
1 Data locale: set of parameters describing the user's language & region settings.
Calculations in Tableau

Deriving Date(Time) from other columns

Example: A table with year, month, day columns

A table with hour, minute and second columns

A table with year, month, day, hour, minute and second columns

White spacing MakeDate ([Year], [Month], [Day])

Result of Make Date formula

MakeTime ([Hour], [Minute], [Second])

Result of Make Time formula

White spacing

MakeDateTime ([Date], [Time])

Result of Make DateTime formula

Calculations in Tableau

Parsing dates

Parsing: analyzing a string into logical syntactic components

Example:

A table with transaction date record based on a UTC timestamp

White spacing

Transaction date as a string in Tableau

White spacing

DateParse ( format, string )

A visual of dateparse calculation in Tableau

White spacing

Transaction date as a timestamp in Tableau

Calculations in Tableau

Extracting date parts

Extracting a discrete year, but also possible with quarter, month, day, dayofyear, weekday, hour, minute, etc.

Example:

DATEPART ( 'year', [Order Date] )

White spacing

DATEPART ( 'quarter', [Order Date] )

White spacing

Example of use of Datepart function - year

Example of use of Datepart function - quarter

Calculations in Tableau

Truncating dates

Truncating: shortening, cutting

Truncating the specified date to the accuracy specified by the date part.

DATETRUNC ( 'month', #2022-06-15# )

White spacing

DATETRUNC ( 'quarter', #2022-06-15# )

White spacing

Example of use of Datetrunc function - day

Example of use of Datetrunc function - day

Calculations in Tableau

Calculating with dates

Adding intervals to dates:

DATEADD (date_part, interval, [Date])

  • Adds specified amount of intervals (e.g., 3 months, 2 days, 10 weeks, etc.) to the date

    White spacing

Subtracting intervals from dates:

DATEDIFF ( date_part, date1, date2 )

  • Returns the difference between two dates, expressed in requested date part intervals

E.g.

  • DATEADD ('month', 3, [Date])
  • DATEADD ('week', 10, [Date])
  • Returns a date

    White spacing

E.g.

  • DATEDIFF ('month', [Start], [End])
  • DATEDIFF ('day', [Start], [End])
  • Returns a number
Calculations in Tableau

Referring to a date in a calculation

Using hard-coded dates in calculations:

Enclose the date value between # #

E.g. #2010-12-31#

White spacing

A table with yearly sales

White spacing

Example of a calculation with hard-coded date

White spacing

A table with yearly sales as of 2011

Calculations in Tableau

Let's practice!

Calculations in Tableau

Preparing Video For Download...