Transform you data with the CAST function in Looker Studio

Transform you data with the CAST function in Looker Studio

Among the myriad functionalities and features provided by Looker Studio (formerly Looker Studio), the CAST function stands out as a unique tool. This function allows users to transform, or 'cast', data from one specific type to another - a feature that can dramatically enhance your analysis and interpretation of data.

Understanding the Syntax and Working of the CAST Function

The general syntax of the CAST function is as follows:


CAST(field_expression AS type)

Here, 'field_expression' represents a field or a particular calculation, and 'type' refers to the data type to which you want to change the original data.

The Google Data Studio CAST function arms you with the ability to transform the following types of data:

  • Text
  • Number
  • Date
  • Date & Time

However, it must be noted that you cannot cast aggregated fields.

Examples of Using the CAST Function

Let's take a practical example to understand the application of the CAST function, using a hypothetical scenario with sales data.

Scenario: In a digital marketing firm, the session duration of website visitors is treated as a text dimension. You are required to count the number of sessions that exceed 600 seconds or 10 minutes.

Step 1: Begin by creating a new calculated field, 'Session_Duration_as_Number', with the following formula:


CAST(Session_Duration AS NUMBER)

This formula will effectively cast the text 'Session_Duration' into a numeric value.

Step 2: Now, we will quantify the length of the sessions by creating another new calculated field, 'Long_Sessions':


SUM(CASE WHEN Session_Duration_as_Number > 600 THEN 1 ELSE 0 END)

The combination of the CASE function with SUM in the formula ensures that sessions exceeding 10 minutes are counted.

Limitations of the CAST Function

Despite its utility, the CAST function comes with some restrictions. It cannot be used with aggregated fields. In addition, while converting from [DATE|DATETIME] to TEXT and vice versa, it only accepts or returns specific formats. However, you can use the FORMAT_DATETIME and PARSE_* functions respectively to return or parse other formats.

Handy Tips for Using the CAST Function

Remember that Google Data Studio will implicitly convert non-string values to a string when an expression requires a string. So, in certain scenarios, you may not need to explicitly use CAST(value AS TEXT). If you are dealing with DATE or DATETIME fields, be clear about the required formats as CAST has specific restrictions in this regard.

Ensure you use CAST judiciously and experiment with its potential combinations with other functions to unlock more analytical potential from your data.

Using the CAST function in Google Data Studio, you can boost your data analysis precision and develop a deeper understanding of your data. Always remember to leverage the power of such functions to let your data tell a compelling story.

More function to use with Looker Studio

HYPERLINK
:
Mastering the Use of HYPERLINK Function in Looker Studio for More Interactive Data Representation
CEIL
:
Working with number and the CEIL function in Looker Studio
CAST
:
Transform you data with the CAST function in Looker Studio
LEFT_TEXT 
:
Understanding and Utilizing the LEFT_TEXT Function in Looker Studio for Effective Data Analysis
MINUTE
:
Utilizing the MINUTE Function in Looker Studio for Precise Temporal Data Analysis and Report Generation

Become a Looker Studio expert
for free

Learn all the secrets of data analysis and create beautiful and effective dashboards thanks to our 30-second video courses! Join Catchr's community on YouTube.

Get more video lessons

All your data on Looker Studio

Build Looker Studio dashboard easily with your marketing data from all platforms and track your essential KPIs! No-code integration.

Start free trial now!
Looker studio

Optimize your data analysis

Get free Looker Studio dashboard template among a large collection of +50 stunning template! Elevate your data visualization.

Get free templates!