Not everyone is a Tableau guru, at least not yet. To help Tableau rookies, we’re starting from square one with the Tableau Essentials blog series. The series is intended to be an easy-to-read reference on the basics of using Tableau Software, particularly Tableau Desktop. Since there are so many cool features to cover in Tableau, the series will include several different posts.
Today, we’ll continue our exploration of the functions you can use when you create a calculated field. We covered Logical Functions and Number Functions in the two previous articles on this topic.
Now, we’ll cover Date Functions. To bring up the Calculated Field window, right-click anywhere in the Data window (i.e. sidebar) to bring up that menu. Then, select Create Calculated Field …
The Calculated Field window will appear like so:
If you right-clicked on a particular dimension or measure to create your calculation, then it will appear in the Calculated Field window in the formula text area. To filter the functions to show only the Date Functions, use the drop-down box and select Date.
Now that we’ve got our functions selected, let’s first define the potential values for date_part, which is used in many of the Date Functions. Date_part can have the following values:
- Second (0-60)
- Minute (0-59)
- Hour (0-23)
- Day (1-31)
- Weekday (1-7 or by name, i.e. “Sunday,” etc.)
- Week (1-52)
- DayofYear (1-365)
- Month (1-12 or by name, i.e. “January,” etc.)
- Quarter (1-4)
- Year (four-digit representation)
Now that you know the different values, let’s examine the Date Functions:
DATEADD Function
DATEADD(date_part, interval, date)
The DATEADD function allows you to specify a portion of a date and increase it. The number that you use for the interval will change the date by increasing the date_part. Example:
DATEDIFF Function
DATEDIFF (date_part, date1, date2, start_of_week)
This function allows you to return the difference between date1 and date2 expressed in units determined by date_part. The start_of_week parameter is optional, and if it is not defined, then the start of the week is determined by the associated data source. Example:
DATENAME Function
DATENAME(date_part, date, [start_of_week])
You can use this function to return the date_part parameter of the date as a string. Again, the start_of_week parameter is optional. Example:
DATEPARSE Function
DATEPARSE(format, string)
This function essentially works in the reverse of DATENAME by converting a string into a date/time with your specified format. If the string does not match the date/time format, then it will return a value of Null. Example:
This function is only available for Microsoft Excel, MySQL, Oracle, PostgreSQL, Tableau data extract and text file data sources. More on that here.
DATEPART
DATEPART(date_part, date, start_of_week)
The DATEPART function allows you to return a specified date_part as an integer. Again, the start_of_week parameter is optional. Example:
When the date_part is set to weekday, the start_of_date parameter is not used because Tableau uses a fixed order to apply offsets.
DATETRUNC
DATETRUNC(date_part, date, start_of_week)
This function truncates the date to the accuracy of the date_part that you specify in the function. In other words, it rounds towards that date_part. Example:
If the start_of_week is omitted, then it is determined by the data source.
DAY
DAY(date)
This function returns the day of the specified date as an integer. Example:
ISDATE
ISDATE(string)
This is a logical test that is also included in the list of Logical Functions. It tests a string to determine if it is a valid date (true/false). Example:
MAX Function
MAX(expression) or MAX(expr1,expr2)
The MAX function exists in several categories of functions, including the Date Functions. The MAX function returns the maximum of a single expression across all records or the maximum of two expressions for each record. The two arguments must be the same type. This function will return a value of NULL if either argument is NULL. Example:
MIN Function
MIN(expression) or MIN(expr1,expr2)
Like the MAX function above, the MIN function is commonly used as a Number Function but can also be used with dates. The MIN function returns the minimum of a single expression across all records or the minimum of two expressions for each record. MIN returns a value of NULL if either of the two arguments is NULL. The two arguments must be of the same type. Example:
MONTH
MONTH(date)
This function returns the month of the specified date as an integer, just like the DAY function does for day. Example:
NOW
NOW()
Returns the current date and time. Example:
TODAY
TODAY()
Returns the current date. Example:
YEAR
YEAR(date)
The YEAR function returns the year of the specified date as an integer. Example:
Calculated Fields
Calculated fields can add a whole new layer of insight to your Tableau dashboards. The possibilities are practically endless, but we’ll be covering the fundamentals, especially functions, to help you build a foundational understanding of how and when to use them. Check back for more posts covering:
- Logical Functions
- Number Functions
- Date Functions
- String Functions
- Type Conversion
- Aggregate Functions
- User Functions
Another great resource for functions is Dan Murray’s best-selling guidebook, “Tableau Your Data!” It features a whole section devoted the functions we’ll be covering in this series and much, much more.
More Tableau Essentials
Want to learn more about Tableau? We have several posts outlining all of Tableau’s fantastic features. Check out the full list on our Tableau Essentials blog channel.
As always, let us know if you have any questions or comments about this post or Tableau in general. If you’re looking for personalized training or help with something bigger, contact us directly!