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

DATE(expression)

Returns a date given a number, string, or date expression.

Examples:

DATE([Employee Start Date])
DATE("April 15, 2004") = #April 15, 2004#
DATE("4/15/2004")
DATE(#2006-06-15 14:52#) = #2006-06-15#

Quotation marks are required in the second and third examples.

DATETIME

DATETIME(expression)

Returns a datetime given a number, string, or date expression.

Example:

DATETIME(“April 15, 2005 07:59:00”) = April 15, 2005 07:59:00

DATEPARSE

DATEPARSE(format, string)

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 Null if the data does not match the format.

This function is available for several connectors. For more information, see Convert a Field to a Date Field.

Examples:

DATEPARSE ("dd.MMMM.yyyy", "15.April.2004") = #April 15, 2004#
DATEPARSE ("h'h' m'm' s's'", "10h 5m 3s") = #10:05:03#

FLOAT

FLOAT(expression)

Casts its argument as a floating point number.

Examples:

FLOAT(3) = 3.000
FLOAT([Age]) converts every value in the Age field to a floating point number.

INT

INT(expression)

Casts its argument as an integer. For expressions, this function truncates results to the closest integer toward zero.

Examples:

INT(8.0/3.0) = 2
INT(4.0/1.5) = 2
INT(0.50/1.0) = 0
INT(-9.7) = -9

When a string is converted to an integer it is first converted to a float and then rounded.

STR

STR(expression)

Casts its argument as a string.

Example:

STR([Age])

This expression takes all of the values in the measure called Age and converts them to strings.


Create a type conversion calculation

Follow along with the steps below to learn how to create a type conversion calculation.

  1. In Tableau Desktop, connect to the Sample - Superstore saved data source, which comes with Tableau.

  2. Navigate to a worksheet.

  3. Select Analysis > Create Calculated Field.

  4. 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

Data Types

Convert a Field to a Date Field

Formatting Calculations in Tableau

Functions in Tableau

Tableau Functions (Alphabetical)

Tableau Functions (by Category)

Thanks for your feedback!