[Looker Studio] How to Use the IF Function and Practical Examples | Calculated Fields
In this article, we will provide a clear explanation on how to use the IF function in Looker Studio, along with specific examples of its practical application. By utilizing the functions available in Looker Studio, you can process and visualize your data effectively.
When it comes to processing data from GA4 (Google Analytics 4), many tasks can be accomplished using calculated fields without the need for BigQuery. We will explain specific use cases based on GA4 data using connectors, so please take advantage of this information.
What are Calculated Fields in Looker Studio?
Calculated Fields in Looker Studio are a handy feature that allows you to create custom fields by using operators (such as addition, subtraction, multiplication, division), functions, and regular expressions based on existing items for use in reports.
Also, for basic usage of Looker Studio, please refer to the "How to Use Looker Studio" guide. Looker Studio is a very convenient tool that is free to use and allows for the creation of easy-to-understand reports by connecting to various data sources, so let's make active use of it.
What is the IF Function?
The IF function allows for different calculations or displays based on conditions for dimensions and metrics. It is very useful for classifying data according to conditions, calculating metrics based on conditions, or converting notation.
There are similar functions such as the CASE (simple) function and the CASE function. While the same results can sometimes be achieved regardless of which function is used, the official recommended usage is as follows:
- Use the IF function when you specify conditions for one or more dimensions or metrics, and there are only two possible outcomes (e.g., categorizing device types into PC and non-PC).
- Use the CASE (simple) function when you specify conditions for a single dimension or metric, and there are three or more possible outcomes (e.g., categorizing device types into PC, mobile, tablet, and others).
- Use the CASE function when you specify conditions for multiple dimensions or metrics, and there are three or more possible outcomes (e.g., categorizing by a combination of device type and OS type into categories such as mobile & Safari, mobile & Chrome, PC & Chrome, etc.).
Syntax
The syntax for the IF function is as follows
IF("condition", "value if condition is met", "value if condition is not met")
For "condition", specify the criteria using strings or numbers. You can set conditions such as a specific string or a number greater than a given value.
For "value if condition is met", specify the string or formula you want to display when the condition is met.
For "value if condition is not met", specify the string or formula you want to display when the condition is not met.
Points to Note
There are a few points to keep in mind when using the IF function.
Unifying the Type of Results
You can specify either strings or numbers as the result, but within the same IF function, you need to unify them to either one.
How to Use the IF Function
To use the IF function, you need to create a calculated field in Looker Studio. There are two types of calculated fields: data source calculated fields and chart-specific calculated fields. This article will focus on creating a data source calculated field, but for more detailed differences between the two, please see the differences between data source calculated fields and chart-specific calculated fields.
I will explain how to use the IF function in detail, using GA4 (Google Analytics 4) data as an example.
Use case:Distinguishing Specific Regions from Others in Looker Studio's GA4
To display a specific region separately from other regions, first create the following calculated fields.
① Field Name: Please enter any field name.
② Formula:IF(Region="California" OR Region="Florida","Key region","Other areas")
In this example, California and Florida are considered key regions, distinguished from other areas. If the region is California or Florida, it will be classified as a key region; otherwise, it will be classified as other areas.
③ Save: Once you have completed entering the information, click save.
A field called the Key region has been created as follows.
(Quote:Looker Studio)
Next, let's add the dimension we just created to make it available in the report. You will then see that it is correctly reflected in the table.
Use case: Displaying Specific Directories (Blog) in Looker Studio with GA4
We will categorize and display a specific directory within the site (in this case, the blog). First, create the following calculated field.
(Quote:Looker Studio)
① Field Name: Please enter any field name.
② Formula:if(REGEXP_CONTAINS(Page path and screen class,"^/blogs/.*")=true,"blogs","others")
Here, we use the REGEXP_CONTAINS function with regular expressions combined with the IF function. The expression instructs to display "blogs" if the page path contains "/blogs/", otherwise, it displays "others."
③ Save: Once you have completed entering the information, click save.
A field called the blogs has been created as follows.
(Quote:Looker Studio)
Next, let's add the dimension we just created to make it available in the report. You will then see that it is correctly reflected in the table.
(Quote:Looker Studio)
In this way, the IF function is simple yet useful as it can specify complex conditions when combined with other functions. Be sure to make the most of it.
Relevant Looker Studio Official Documentation
Looker Studio Official Help : About calculated fields
Looker Studio Official Help : Function list