Looker Studio Function : CAST
CAST Function in Looker Studio – Convert Data Types Easily
Category: Text Function
The CAST function in Looker Studio is used to convert a value from one data type to another, such as converting text into numbers, numbers into text, or text into dates. This function becomes extremely important when working with real-world data that is not always stored in the correct format.
In many reporting scenarios, numeric values are stored as text, dates come as strings, or IDs are incorrectly interpreted as numbers. These issues can break calculations, sorting, filtering, and chart behavior. CAST allows analysts to fix such problems directly inside Looker Studio without modifying the source data.
From a BI perspective, CAST is commonly used in calculated fields to enable proper aggregations, comparisons, and visualizations. It helps ensure that metrics behave correctly and dimensions display as expected. Since CAST works at row level and only affects calculated output, it is safe and efficient for large datasets.
Overall, CAST is an essential function for data preparation and type consistency in Looker Studio dashboards.
Purpose of CAST Function
1. Data Type Conversion
Converts values between text, number, and date formats for accurate analysis.
2. Enable Calculations
Allows mathematical operations on fields originally stored as text.
3. Fix Formatting Issues
Resolves incorrect field interpretation caused by inconsistent source data types.
4. Improve Chart Behavior
Ensures proper sorting, aggregation, and visualization by using correct data types.
Type of Calculation & Practical Use Cases
1. Data Preparation
Prepares raw fields for analysis by converting them into correct data types.
2. Calculated Metrics
Used to create metrics from text-based numeric fields.
3. Date Handling
Converts text dates into date format for time-based analysis.
4. Visualization Accuracy
Improves chart accuracy by ensuring correct field interpretation.
CAST(expression AS data_type)
CAST Function Function – Parameters Table
| Parameter | Type | Description |
|---|
| expression | Any | The value or field to be converted |
| data_type | Data Type | Target data type such as TEXT, NUMBER, or DATE |
How Does the CAST Function Work?
CAST takes the given expression and attempts to convert it into the specified target data type. If the conversion is valid, the function returns the converted value.
If the value cannot be converted, the function usually returns NULL. CAST processes each row independently and does not modify the source data. Proper data formatting is essential for successful conversion.
When Should You Use CAST Function?
When numeric values are stored as text
When date fields are coming as strings
When calculations are failing due to wrong data types
When sorting or filtering behaves incorrectly
When preparing data for accurate visualizations
Examples of CAST Function
Example 1: Convert Text to Number
CAST(Sales_Amount AS NUMBER)
Example 2: Convert Number to Text
CAST(Order_ID AS TEXT)
Example 3: Convert Text to Date
CAST(Order_Date AS DATE)
Tips & Best Practices
Always verify source data format before casting
Handle NULL values after conversion
Use CAST only when necessary
Test converted fields in tables
Avoid repeated casting in complex formulas
CAST supports common data types like TEXT, NUMBER, and DATE, depending on the original field structure and compatibility.
No, CAST only affects calculated fields inside Looker Studio and does not modify the original data source.
If the conversion is not possible, CAST usually returns NULL for that specific row.
Yes, CAST is often used inside metrics to convert text values into numbers for calculations.
CAST is generally lightweight, but excessive use in complex formulas may slightly impact performance.