Date Time Functions In Tableau Explained

by Afnan Foyez

Date and time are crucial aspects of data analysis in Tableau. They allow us to visualize time-related data effectively. In this blog post, we'll explore some of the most essential date and time functions Tableau has to offer, along with real-world examples of how to use them.

DATEPART: Extracting Date Components

The DATEPART function in Tableau is your key to extracting specific date components, such as year, month, day, or even hour. It outputs an integer representing the specified part of a date.

Example:

	DATEPART('year', [Order Date])

Imagine you have an "Order Date" field, and it contains the date '2023-09-07'. Applying DATEPART('year', [Order Date]) to this field would return the year, which is 2023.
The output is of the integer data type.

DATETRUNC: Truncating Dates

Sometimes, you need to truncate a date to a specific level of detail. This is where the DATETRUNC function comes in handy. It outputs a date value truncated to the specified level of detail.

Example:

	DATETRUNC('quarter', [Order Date])

Suppose your "Order Date" is '2023-09-07'.
Applying DATETRUNC('quarter', [Order Date]) would round this date down to the beginning of the quarter, which is July 1, 2023.
The output would be a date data type.

DATEDIFF: Calculating Differences in Dates

The DATEDIFF function calculates the difference between two dates in terms of a specified date part. It returns an integer representing the difference.

Example:

	DATEDIFF('day', [Order Date], [Shipment Date])

Imagine you have an "Order Date" of '2023-09-05' and a "Shipment Date" of '2023-09-10'. Applying DATEDIFF('day', [Order Date], [Shipment Date]) would calculate the number of days between these dates, which is 5 days.
The output is an integer data type.

DATEADD: Adding or Subtracting Dates

Need to add or subtract a specific number of date parts to a date?

The DATEADD function has you covered. It outputs a date value after performing the operation.

Example:

	DATEADD('month', 3, [Order Date])

Suppose your "Order Date" is '2023-09-07'. Applying DATEADD('month', 3, [Order Date]) adds 3 months to this date, resulting in '2023-12-07'.
The output is a date data type.

NOW and TODAY: Current Date and Time

Tableau offers two functions for getting the current date and time. NOW() returns the current date and time as a datetime data type, while TODAY() returns the current date without the time component as a date data type.

Examples:

	NOW() // Returns datetime data type
	TODAY() // Returns date data type

DATE and TIME: Creating Custom Date and Time Values

You can create custom date and time values using the DATE and TIME functions. DATE combines year, month, and day components to produce a date data type. TIME combines hour, minute, and second components to create a time data type.

Examples:

	DATE(2023, 9, 7) // Creates the date September 7, 2023
	TIME(14, 30, 0) // Creates the time 2:30 PM

DATETIME: Combining Date and Time

If you have separate date and time components and want to combine them into a single datetime data type, use the DATETIME function.

Example:

	DATETIME([Order Date], [Order Time])

Suppose you have an "Order Date" of '2023-09-07' and an "Order Time" of '14:30:00'. The DATETIME function combines these into a datetime value.

MAKEDATE and MAKETIME: Creating Date and Time from Components

MAKEDATE and MAKETIME functions allow you to create date and time values based on specific components. MAKEDATE creates a date from a year and day of the year, while MAKETIME creates a time from hours, minutes, and seconds.

Examples:

	MAKEDATE(2023, 10, 2) // Creates the date October 2, 2023
	MAKETIME(12, 15, 30) // Creates the time 12:15:30 PM

You can also combine these functions to create a datetime value:

	MAKEDATETIME(MAKEDATE(2023, 10, 2), MAKETIME(12, 30, 0))

This would create the datetime value October 2, 2023, 12:30:00 PM.

DATEPARSE: Converting Text to Date

Lastly, DATEPARSE is used to convert a text string into a date value using a specified format. It's particularly useful when your date and time data is stored as a string.

Example:

	DATEPARSE("%m/%d/%Y", "09/07/2023") 
	// Converts the string to a Date value representing September 7, 2023

In conclusion, mastering these date and time functions in Tableau opens up a world of possibilities for analyzing and visualizing your data. Also, understanding the data types they output is crucial for accurate calculations and visualizations.