CAST

The CAST function converts the semantic type of a field or expression to text or number.

In this article:

Sample Usage

CAST(num AS TEXT)
CAST(timestamp_str AS NUMBER)

Syntax

CAST(field_expression AS type)

Output types

TEXT—convert the input to text.

NUMBER—convert the input to a number.

Example

Goal: count website sessions longer than N seconds.

In a Google Analytics data source, Session Duration is a text dimension. The first step to counting sessions longer than N is to convert this to a number using CAST. The second step is to use the CASE statement to identify sessions longer than a certain value—we'll say 60 seconds for this example. We'll combine CASE with SUM to do the counting.

Step 1: Create a new calculated field Session Duration (number) with the following formula:

CAST(Session Duration AS NUMBER)

Step 2: Create a new calculated field Long Sessions:

SUM(CASE WHEN Session Duration > 60 THEN 1 ELSE 0 END)

Notes

You cannot CAST aggregated fields.

Was this article helpful?
How can we improve it?