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. For example, you can convert numbers to strings, such as age values (numbers) to string values so that Tableau does not try to aggregate them.
The calculation for such a task might look similar to the following:
STR([Age])
Type conversion functions available in Tableau:
The result of any expression in a calculation can be converted
to a specific data type. The conversion functions are STR()
, DATE()
, DATETIME()
, INT()
,
and FLOAT()
. For example, if you want to cast a
floating point number like 3.14 as an integer, you could write INT(3.14)
.
The result would be 3
, which is an integer. The
casting functions are described below.
A boolean can be cast to an integer, float, or string,
It cannot be cast to a date. True
is 1, 1.0, or
“1”, while False is 0, 0.0 or “0”. Unknown
maps
to Null
.
Function |
Syntax |
Description |
DATE |
|
Returns a date given a number, string, or date expression. Examples:
Quotation marks are required in the second and third examples. |
DATETIME |
|
Returns a datetime given a number, string, or date expression. Example:
|
|
Converts a string to a datetime in the specified format. Support for some locale-specific formats is determined by the computer's system settings. Letters that appear in the data and do not need to be parsed should be surrounded by single quotes (' '). For formats that do not have delimiters between values (for example, MMddyy), verify that they are parsed as expected. The format must be a constant string, not a field value. This function returns This function is available for several connectors. For more information, see Convert a Field to a Date Field. Examples:
|
|
FLOAT |
|
Casts its argument as a floating point number. Examples:
|
INT |
|
Casts its argument as an integer. For expressions, this function truncates results to the closest integer toward zero. Examples:
When a string is converted to an integer it is first converted to a float and then rounded. |
STR |
|
Casts its argument as a string. Example:
This expression takes all of
the values in the measure called |
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, do the following:
-
Name the calculated field, Postal Code String.
-
Enter the following formula:
STR([Postal Code])
This calculation converts the Postal Code field from a number to a string.
-
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 visualizations.
Converting this field from a number to a string ensures that Tableau treats it as a string and not a number (i.e. Tableau does not aggregate it).
See Also
Convert a Field to a Date Field
Formatting Calculations in Tableau