Coupler.io Blog

Google Data Studio (Looker Studio) Formulas & Functions: A Comprehensive Guide

Looker Studio Formulas & Functions A Comprehensive Guide

Each formula you write in Google Data Studio (ex. Looker Studio) unlocks a new insight from your data. Formulas help unravel the story behind your numbers. They “calculate” answers to critical questions like the average sales value, conversion rates, changes in subscriber count, etc.

Yet, Data Studio offers more than just formulas. It has many built-in functions that handle everything from simple sums to tricky conditional tasks.

Data Studio formula and function fundamentals

Data Studio formulas

Formulas help you to manipulate your data in order to derive new information. It can be a mathematical operation, string manipulation, date calculation, etc.

Data Studio formulas can be used within calculated fields (to create custom metrics and dimensions) and filters. Here’s an example of a formula used to create a calculated field:

Let’s say you want to create a new calculated field called Total that substracts a subtotal field (Subtotal) by a discount field (Discount). The Data Studio formula for this calculated field would be ‘Subtotal – Discount’:

This calculated field can then be used in reports and dashboards just like any other field in the data model:

Note: It’s common for users to use the term “formulas” when referring to “calculated fields”. This is because all calculated fields in Data Studio are built using formulas. Yet, it’s important to note the distinction between the two for accurate understanding.

Moreover, in Data Studio Pro, you can use AI to help you create formulas. Learn more about this in our blog post where we’ve compared Data Studio vs Data Studio Pro.

Using functions in formulas

Data Studio provides over 80 functions. You can use them inside your calculated field’s formula. 

Each function handles specific needs. For example, the MAX and MIN functions return the maximum and minimum values. The UPPER function converts a text to uppercase. You can use the YEAR, MONTH, and DAY functions to extract the year, month, and day of a date or datetime value. 

Let’s look at an example. Say you want to display the average order value (AOV) of your online sales in a scorecard. You can do this by creating a calculated field with an AVG function in its formula:

Note: In the latter section, we’ll explain various Data Studio functions based on their types.

Data Studio formula syntax: Simplifying complexity

Formulas are written using certain rules. These rules are known as syntax. 

It’s important to understand how to structure your formulas in Data Studio correctly to get accurate results.

Now, let’s make it easy and break down some main points about Data Studio formula syntax:

How to use Data Studio formulas: Your step-by-step guide

We’ll show you how to apply formulas in Data Studio calculated fields, which you can create either in the data source or directly in charts.

The exciting part is that we’ll also explain how to use Coupler.io to create custom fields with formulas. So, keep reading!

Using formulas in the data source’s calculated fields

The benefit of creating calculated fields in a data source is that you can use them in any report that uses the data source.

To create a formula in a data source’s calculated field:

  1. Click the Data sources tab in the top menu bar.
  1. Click a data source where you want to add the formula. This will open the data source in edit mode.
  2. Click the Add a field button in the top section. Then, select Add calculated field from the context menu that appears.
  1. Enter Field Name and Formula. Note: You can reference an existing field by typing its name directly in the formula field or by clicking it from the Available Fields list.
  1. Click Save. Review the newly calculated field in your data source. If you want, you can change its data type and default aggregation to ensure it displays the correct value and format in charts.

Using formulas in chart-specific calculated fields

When you add a calculated field in a chart level, it only exists in that specific chart. It won’t affect your original data source.

To create a formula in a chart-specific calculated field:

  1. Open your report in edit mode.
  2. Click a chart where you want to apply the formula.
  3. In the Setup tab, click Add dimension or Add metric, depending on what you want to create.
  1. In the menu container that appears, click Add field.
  1. Enter a Name and Formula for the new field. For example, here, we add a new dimension named Category and use a formula with a function: UPPER(ProductCategory).
  1. Click Apply. Now, you can see the new field in the chart.

Create calculated fields for the data sources connected to Data Studio 

Please keep in mind, though, that creating too many calculated fields might slow down your Data Studio reports and dashboards, especially with large datasets. In this case, we suggest creating the fields before loading your data to Data Studio. You can use Coupler.io, a reporting automation platform for business teams, which provides a Data Studio connector to automatically collect, transform, and refresh data from 400+ apps.

With this approach, the formulas are carried out on the Coupler.io side before the data ends in Data Studio. This will lead to a better performance.

As an example, here, we select Google Sheets as the source. You can try it yourself for free to load data from your source to Data Studio. Choose the app in the form below to continue:

Then, follow the steps below to configure your data flow.

Step 1: Collect data

In this step, you configure the data source to extract data from. 

For example, to connect with Google Sheets, you must connect your Google account. Then, select a File and specify the Sheet you will extract as the data source. When done, click Finish and Proceed. 

Review your data source. If you want, you can add another data source to the data flow. To apply data transformations and add new fields with formulas, open the Transformation step to add new fields with formulas.

Step 2: Transform data & apply formulas

In this step, you can manipulate your data in different ways. You can hide some columns, apply filtering and sorting, blend data from several apps into one dataset, and add new custom columns with formulas. 

To create a new field or column, add a formula column and enter the Column name and Formula.

You can also check out our documentation on how to add formula-based columns. 

Once you’re done with your data transformation setup, load the data to Data Studio by generating the data source.

You will then be redirected to Data Studio and see the following screen. Authorize and connect the data flow.

A new data source will be added to the Data Studio. This will also include the calculated field we created using Coupler.io.

As you can see, Coupler.io’s data transformation feature can help you create custom fields with formulas in Data Studio data sources. It works for both new metrics and dimensions. You can also create fields with various data types.

Step 3: Schedule your data flow

Coupler.io lets you set up an automatic data refresh. You can set your data flow to refresh automatically.

To do this, go back to Coupler.io and refresh your browser. You’ll see a screen similar to this below to choose an interval and other settings.

Data Studio formula examples

Let’s explore more examples of how formulas and functions can be applied in different use cases. We’ll show some real-world examples that you can apply in your own work. However, please note that these merely scratch the surface of what’s possible with Data Studio’s formulas and functions. 

Data Studio basic formulas

You can use basic operators, such as arithmetic, logical, and comparison operators, in your formulas to help you perform calculations. 

Here are some examples:

DataStudio formulas with functions

By using Data Studio functions in formulas, you can simplify calculations and data manipulation.

Here are some examples:

Data Studio advanced  formulas

Advanced formulas and functions in Data Studio are useful for in-depth data analysis and manipulation. 

Some examples:

Suppose you want to segment your customers based on their purchase history. A CASE statement can help you achieve this:

CASE
  WHEN SUM(OrderTotal)> 100000 THEN 'High-value Customer'
  WHEN SUM(OrderTotal) BETWEEN 50000 AND 100000 THEN 'Mid-value Customer'
  ELSE 'Low-value Customer'
END

If you have a dataset with URLs in various formats and want to standardize them to start with ‘www.’, you can use REGEXP_REPLACE for this:

REGEXP_REPLACE(WebsiteUrl, '^(http[s]?://)?', 'www.')

This formula matches any URL starting with http:// or https:// (the ? in the regex expression makes the s optional) and replaces it with www. This ensures all URLs in your dataset follow a consistent format.

Exploring the different types of Data Studio functions

There are over 80 built-in functions in Data Studio. You don’t need to memorize all of them. In reality, you’ll find yourself using some of them regularly. 

The following are the different types of Data Studio functions:

Data Studio aggregation functions

Aggregation functions allow you to summarize and group your data, providing insights that might not be apparent at first glance.

Some commonly used aggregation functions include:

Function nameSyntaxWhat it does
SUMSUM(X)Returns the sum of all values of X.
AVGAVG(X)Returns the average of all values of X.
MAXMAX(X)Returns the maximum value of X.
MINMIN(X)Returns the minimum value of X.
COUNTCOUNT(X)Returns the number of values of X.
COUNT_DISTINCTCOUNT_DISTINCT(X)Returns the number of unique values of X.

Other aggregation functions include MEDIAN, PERCENTILE, STDEV, and VARIANCE.

Data Studio arithmetic functions

Arithmetic functions help you perform mathematical calculations. 

Here are some arithmetic functions in Data Studio:

Function nameSyntaxWhat it does
ROUNDROUND(X, Y)Returns X rounded to Y precision digits. 
CEILCEIL(X)Returns the nearest integer greater than X. For example, if the value of X is v, CEIL(X) is greater than or equal to v.
FLOORFLOOR(X)Returns the nearest integer less than X. For example, if the value X is v, FLOOR(X) is equal to or less than v.
POWERPOWER(X, Y)Returns result of raising X to the power Y. 
SQRTSQRT(X)Returns the square root of X. 
ABSABS(X)Returns the absolute value of a number.

Other arithmetic functions include NARY_MIN, NARY_MAX, trigonometry functions (SIN, COS, TAN, ASIN, ACOS, ATAN), and logarithmic functions (LOG, LOG10).

Data Studio string/text functions

String functions manipulate text data. They can help you clean and format your data. 

Common string functions include:

Function nameSyntaxWhat it does
SUBSTRSUBSTR(X, index, n)Returns a text that is a substring of X. The substring begins at index and is n characters long.
TRIMTRIM(X)Returns X with leading and trailing spaces removed.
UPPERUPPER(X)Converts X to uppercase.
LOWERLOWER(X)Converts X to lowercase.
REPLACEREPLACE(X, Y, Z)Returns a copy of X with all occurrences of Y in X replaced by Z.
LENGTHLENGTH(X)Returns the number of characters in X.
CONCATCONCAT(X,Y)Returns a text that is the concatenation of X and Y.

Other string functions include STARTS_WITH, ENDS_WITH, CONTAINS_TEXT, LEFT_TEXT, RIGHT_TEXT, REGEXP_CONTAINS, REGEXP_EXTRACT, REGEXP_MATCH, and REGEXP_REPLACE.

Data Studio date functions

Date functions allow you to work with dates and times.

Common date functions include:

Function nameSyntaxWhat it does
DATEDATE(year, month, day)Constructs a Date field or value from numbers or from a Date & Time field or expression.
DATE_DIFFDATE_DIFF(X, Y)Returns the difference in days between X and Y (X – Y).
YEARYEAR(date_expression)Returns the year of a given date expression.
MONTHMONTH(date_expression)Returns the month of a given date expression.
DAYDAY(date_expression)Returns the day of a given date expression.
TODAYTODAY([time_zone])Returns the current date as of the specified or default timezone.
CURRENT_DATECURRENT_DATE([time_zone])Returns the current date as of the specified or default timezone.
PARSE_DATEPARSE_DATE(format_string, text)Converts text to a date.

Other date functions include DATETIME, CURRENT_DATETIME, DATETIME_DIFF, EXTRACT, PARSE_DATETIME, YEARWEEK, WEEK, QUARTER, and many more.

Data Studio conditional functions

Conditional functions are used to perform different actions based on different conditions.

Conditional functions in Data Studio include:

Function nameSyntaxWhat it does
IFIF(condition, true_result, false_result)Returns true_result if condition is true. Else, returns false_result. 
IFNULLIFNULL(expression, result)Returns result if the input expression is null. 
NULLIFNULLIF(expression1, expression2)Returns null if expression1 matches expression1.
COALESCECOALESCE(field_expression[,field_expression, …])Returns the first non-missing value found in a list of fields.
CASECASE    WHEN condition THEN result    [WHEN condition THEN result]    […]    [ELSE else_result]END”Evaluates the condition of each successive WHEN clause. If the first condition is true, any remaining clauses are not evaluated. If all conditions are false or NULL, returns else_result if present; if not present, returns NULL. 
CASE (Simple)CASE input_expression    WHEN expression_to_match THEN result    [WHEN expression_to_match THEN result]    […]    [ELSE result]ENDCompares input_expression to expression_to_match of each successive WHEN clause and returns the first result where this comparison returns true

Data Studio geo functions

Here are Data Studio functions that allow you to work with geographic data:

Function nameSyntaxWhat it does
TOCITYTOCITY(X [,Input Format])Returns the city name for X.
TOCONTINENTTOCONTINENT(X [,Input Format])Returns the continent name for X.
TOCOUNTRYTOCOUNTRY(X [,Input Format])Returns the country name for X.
TOREGIONTOREGION(X [,Input Format])Returns the region name for X.
TOSUBCONTINENTTOSUBCONTINENT(X [,Input Format])Returns the sub-continent name for X.

Data Studio miscellaneous functions

There are also several miscellaneous functions in Data Studio that don’t fit into the categories above but are still very useful.

Function nameSyntaxWhat it does
CASTCAST(field_expression AS TYPE)Cast field or expression into TYPE. Aggregated fields are not allowed inside CAST.TYPE can be NUMBER, TEXT, or DATETIME.
HYPERLINKHYPERLINK(URL, link label)Returns a hyperlink to the URL, labeled with the link label.
IMAGEIMAGE(Image URL, [Alternative Text])Creates Image fields in your data source.
NATIVE_DIMENSIONNATIVE_DIMENSION(“JSON_VALUE(‘{“name”: “Dana”}’, ‘$.name’)”,”STRING”)Returns the result of an SQL expression as evaluated by the underlying dataset. The expression cannot include any aggregations.

Using Data Studio, formulas, and functions efficiently with Coupler.io

In conclusion, DataStudio formulas and functions are powerful tools for data analysis. However, they come with their own set of challenges. One of them is the potential slowdown in performance with extensive calculations. This is where Coupler.io can make a real difference. 

By taking care of these complex calculations outside of Data Studio, Coupler.io ensures your data analytics process runs efficiently and smoothly. This enhancement in speed and efficiency can be a game-changer for your data analysts. 

We highly recommend checking out our Data Studio tutorial to enhance your Data Studio experience.

Exit mobile version