Type Conversion
This article introduces type conversion functions and their uses in Tableau. It also demonstrates how to create a type conversion calculation using an example.
Why use type conversion functions
Type conversion functions allow you to convert fields from one data type to another (this is called "casting"). For example, if you have date information in a field with a string data type, you won't be able to use that field in date calculations unless the field is cast (changed) to a date data type.
For example, to use a string date field in a DATEDIFF function, you'd also need to use a date type conversion:
DATEDIFF('day', [Date Field], DATE([String Date Field]) )
Without the DATE function changing the data type, you'd get an error that "DATEDIFF is being called with (string, date, string)".
Tip: Fields have default Data Types in the Data pane. In many instances, you don't need to use a type conversion function at all. You can change the field's data type by clicking the icon. However, in some cases it can be useful to change the data type only for a specific calculation, or Tableau may not be able to correctly cast the field's contents to the desired data type (such as not recognising the date format). Then you would need to use a type conversion function like those listed below.
Type conversion functions available in Tableau
DATE
Syntax | DATE(expression) |
Output | Date |
Definition | Returns a date, given a number, string or date expression. |
Example | DATE([Employee Start Date]) DATE("September 22, 2018") DATE("9/22/2018") DATE(#2018-09-22 14:52#) |
Notes | Unlike DATEPARSE(Link opens in a new window), there is no need to provide a pattern as DATE automatically recognises many standard date formats. If DATE does not recognise the input, however, try using DATEPARSE and specifying the format. MAKEDATE(Link opens in a new window) is another similar function, but MAKEDATE requires the input of numeric values for year, month and day. |
DATETIME
Syntax | DATETIME(expression) |
Output | Datetime |
Definition | Returns a datetime given a number, string or date expression. |
Example | DATETIME("April 15, 2005 07:59:00") = April 15, 2005 07:59:00 |
FLOAT
Syntax | FLOAT(expression) |
Output | Floating point number (decimal) |
Definition | Casts its argument as a floating point number. |
Example | FLOAT(3) = 3.000 |
Notes | See also INT, which returns an integer. |
INT
Syntax | INT(expression) |
Output | Integer |
Definition | Casts its argument as an integer. For expressions, this function truncates results to the closest integer toward zero. |
Example | INT(8/3) = 2 INT(-9.7) = -9 |
Notes | When a string is converted to an integer it is first converted to a float and then rounded. See also FLOAT, which returns a decimal. |
MAKEDATE
Syntax | MAKEDATE(year, month, day) |
Output | Date |
Definition | Returns a date value constructed from the specified numerical year, month and date. |
Example | MAKEDATE(1986,3,25) = #1986-03-25# Note that incorrectly entered values will be adjusted into a date, such as MAKEDATE(2020,4,31) = May 1, 2020 rather than returning an error that there is no 31st day of April. |
Notes | Available for Tableau Data Extracts. Check for availability in other data sources. MAKEDATE requires numerical inputs for the parts of a date. If your data is a string that should be a date, try the DATE function. DATE automatically recognises many standard date formats. If DATE does not recognise the input try using DATEPARSE. |
MAKEDATETIME
Syntax | MAKEDATETIME(date, time) |
Output | Datetime |
Definition | Returns a datetime that combines a date and a time. The date can be a date, datetime or a string type. The time must be a datetime. |
Example | MAKEDATETIME("1899-12-30", #07:59:00#) = #12/30/1899 7:59:00 AM# MAKEDATETIME([Date], [Time]) = #1/1/2001 6:00:00 AM# |
Notes | This function is available only for MySQL-compatible connections (which for Tableau are MySQL and Amazon Aurora). MAKETIME is a similar function available for Tableau Data Extracts and some other data sources. |
MAKELINE
Syntax | MAKELINE(<Spatial Point1>,<Spatial Point2>) |
Output | Geometry spatial object (line) |
Definition | Generates a line mark between two points. |
Example | MAKELINE(OriginPoint, DestinationPoint) MAKELINE( MAKEPOINT(OriginLat],[OriginLong]) , MAKEPOINT([DestinationLat],[DestinationLong] ) |
Notes | Useful for building origin-destination maps or, with MAKEPOINT, turning latitude and longitude into spatial objects. |
MAKEPOINT
Syntax | MAKEPOINT(<latitude>, <longitude>) |
Output | Geometry spatial object (point) |
Definition | Converts data from latitude and longitude columns into spatial objects. |
Example | MAKEPOINT([AirportLatitude],[AirportLongitude]) |
Notes | You can use MAKEPOINT to spatially-enable a data source so that it can be joined with a spatial file using a spatial join. For more information, see Join Spatial Files in Tableau. To use MAKEPOINT, your data must contain latitude and longitude coordinates. |
MAKETIME
Syntax | MAKETIME(hour, minute, second) |
Output | Datetime. See Notes. |
Definition | Returns a date value constructed from the specified hour, minute and second. |
Example | MAKETIME(14, 52, 40) = #1/1/1899 14:52:40# |
Notes | Because Tableau does not support a time data type, only datetime, the output is a datetime. The date portion of the field will be 1/1/1899. Similar function to MAKEDATETIME, which is only available for MYSQL-compatible connections. |
STR
Syntax | STR(expression) |
Output | String |
Definition | Casts its argument as a string. |
Example | STR([ID]) |
Cast Boolean expressions
A Boolean can be cast to an integer, float or string, but not a date.
True
maps to 1, 1.0 or "1"False
maps to 0, 0.0 or "0"Unknown
maps toNull
Create a type conversion calculation
Follow along with the steps below to learn how to create a type conversion calculation.
- In Tableau Desktop, connect to the Sample - Superstore saved data source, which comes with Tableau.
- Navigate to a worksheet.
- Select Analysis > Create Calculated Field.
- In the calculation editor that opens, converts the Postcode field from a number to a string:
- Name the calculated field, Post Code String.
- Enter the following formula:
STR([Postal Code])
- When finished, click OK.
The new calculated field appears under Dimensions in the Data pane. Just like your other fields, you can use it in one or more visualisations.
Converting this field from a number to a string ensures that Tableau treats it as a string and not a number (and therefore doesn't aggregate it).
See Also
Convert a Field to a Date Field
Formatting Calculations in Tableau