Google Data Studio Functions, Formulas Tutorial – Looker Studio

My step-by-step blueprint for using Looker Studio (formerly Google Data Studio)

Get the FREE ebook on Looker Studio (75 Pages)

Yes I want the FREE ebook

 

Last Updated: November 26, 2022

Note: Google Data Studio is now known as Looker Studio.

Functions in Looker Studio are formulas that are used to: 

  • Aggregate data. 
  • Do mathematical calculations on your data.
  • Manipulate time data. 
  • Manipulate geographic location data. 
  • Manipulate string data. 

Functions make your calculated fields more powerful. They are used inside of calculated fields as a formula.

If you remember, we used the ‘CASE’ function as a formula when dealing with boolean values:

highlighted formula 2

‘CASE’ is just one of the many functions provided by Looker Studio.

Looker Studio provides over 50 functions that you can use inside of calculated fields as a formula.

Following are the typical attributes of a function in Looker Studio

  1. Every function in Looker Studio has some purpose. 
  2. Every function in Looker Studio has syntax
  3. All functions expect one or more parameters
  4. Parameters are listed in the syntax
  5. All functions return a value
  6. Almost all functions have certain restrictions regarding how they can be used.

#1 Every function in Looker Studio has some purpose

For example, the ‘CASE’ function is used to create new fields that use conditional logic to determine the field values.

The ‘AVG’ function is used to calculate the average value of a numeric field.

Similarly, the ‘COUNT’ function is used to count the number of items in a field.

Note: You can learn more about the purpose of each function from the Google help documentation on each function: https://support.google.com/datastudio/topic/7019880?hl=en&ref_topic=7570421   

#2 Every function in Looker Studio has syntax

Syntax defines how a function must be used. It defines how parameters (more about them later) must be used. 

For example, the following is the syntax for the CASE function:

CASE

    WHEN condition THEN result

    WHEN condition THEN result

    WHEN condition THEN result

    … 

    ELSE result 

END

Following is the syntax for ‘AVG’ function:

AVG(value)

Following is the syntax for ‘COUNT’ function:

COUNT(value)

Note: You can learn more about the syntax of each function from the Google help documentation on each function: https://support.google.com/datastudio/topic/7019880?hl=en&ref_topic=7570421   

#3 All functions expect one or more parameters

Parameters (also called ‘arguments’) are the input a function expects in Looker Studio. The parameter tells the function of what data to act upon. 

Parameters can be field names or expressions. 

An expression can be a number, literal text, or a statement that evaluates to a field name in your data source. 

Parameters can also provide additional instructions or formatting information.

For example, CASE function expects the following parameters:

  1. Condition
  2. Result
  3. Else result

#1 Condition—It is an expression which evaluates to a boolean value (true or false). Conditions can include dimensions or metrics but not both:

condition 1

#2 Result – the value to return. It can be a dimension, metric, or literal value:

result 1

#3 ELSE result (optional) – It is the default value to return if no other condition is met.

Format formula 1
formatted formula 1

Note(1): When providing parameters, be sure to enclose the literal text in single or double-quotes.
Note(2): You can learn more about the parameters used by each function from the Google help documentation on each function: https://support.google.com/datastudio/topic/7019880?hl=en&ref_topic=7570421
Note(3): You can click on the ‘Format Formula’ button, to make your formula more readable.

#4 Parameters are listed in the syntax

All the parameters which the CASE function expect, are listed in the syntax of the CASE function:

CASE

    WHEN condition THEN result

    WHEN condition THEN result

    WHEN condition THEN result

    … 

    ELSE result 

END

So if you remember the syntax, you will remember how to use the CASE function.

#5 All functions return a value

For example, ‘CASE’ function returns dimensions or metrics based on conditional expressions.

The ‘AVG’ function returns the average for all values in a field or expression.

Similarly, the COUNT function returns the total number of items in a field or expression.

Note: You can learn more about the values returned by each function from the Google help documentation on each function: https://support.google.com/datastudio/topic/7019880?hl=en&ref_topic=7570421   

#6 Almost all functions have certain restrictions regarding how they can be used

formula error 2

For example, when using the CASE function you can not compare dimensions to dimensions or metrics to metrics:

In Looker Studio, you can compare a dimension or metric only with a literal value. 

Similarly, you can not apply the AVG function or COUNT function to a pre-aggregated field.

Note: You can learn more about such restrictions on each function from the Google help documentation on each function: https://support.google.com/datastudio/topic/7019880?hl=en&ref_topic=7570421   

More information on each function, including examples, is also available in the formula editor.

When you type a function name in the formula editor, Looker Studio provides information about the function like its syntax, its purpose, and its use case:

formula error 1

The bottom of the formula editor sometimes provides additional information about how a function is being used and how it should be used.  

For example, in the screenshot above, the Looker Studio was expecting arguments (Expected arguments, but none were provided).

Types of Looker Studio functions

All of the Looker Studio functions are grouped into the following categories:

  1. Aggregation functions
  2. Arithmetic functions
  3. Date functions
  4. Geo functions
  5. Text functions
  6. Conditional functions
  7. Miscellaneous functions

Aggregation functions

These functions are used to aggregate data. 

Following are examples of aggregation functions:

  1. AVG : AVG(X)
  2. COUNT : COUNT(X)
  3. COUNT_DISTINCT : COUNT_DISTINCT(X)
  4. MAX : MAX(X)
  5. MEDIAN : MEDIAN(X)
  6. MIN : MIN(X)
  7. PERCENTILE : PERCENTILE(X,N)
  8. STDDEV : STDDEV(X)
  9. SUM : SUM(X)
  10. VARIANCE : VARIANCE(X)

Note: Aggregation functions can not be applied to already aggregated data. This includes most metrics found in Google Analytics and Google Ads. For example, Sessions is already summed in your data set, so the formula SUM(Sessions) will produce an error.

Arithmetic functions

These functions are used to do mathematical calculations on your data.

Following are examples of Arithmetic functions:

  1. ABS : ABS(X)
  2. ACOS : ACOS(X)
  3. ASIN : ASIN(X)
  4. ATAN : ATAN(X)
  5. CEIL : CEIL(X)
  6. COS : COS(X)
  7. FLOOR : FLOOR(X)
  8. LOG : LOG(X)
  9. LOG10 : LOG10(X)
  10. NARY_MAX : NARY_MAX(X, Y [,Z]*)
  11. NARY_MIN : NARY_MIN(X, Y [,Z]*)
  12. POWER : POWER(X, Y)
  13. ROUND : ROUND(X, Y)
  14. SIN : SIN(X)
  15. SQRT : SQRT(X)
  16. TAN : TAN(X)

Date functions

These functions are used to manipulate time data. 

Following are examples of Date functions:

  1. CURRENT_DATE : CURRENT_DATE([time_zone])
  2. CURRENT_DATETIME : CURRENT_DATETIME([time_zone])
  3. DATE : DATE(year, month, day)
  4. DATE_DIFF : DATE_DIFF(X, Y)
  5. DATE_FROM_UNIX_DATE : DATE_FROM_UNIX_DATE(integer)
  6. DATETIME : DATETIME(year, month, day, hour, minute, second)
  7. DATETIME_ADD : DATETIME_ADD(datetime_expression, INTERVAL integer part)
  8. DATETIME_DIFF : DATETIME_DIFF(date_expression, date_expression, part)
  9. DATETIME_SUB : DATETIME_SUB(datetime_expression, INTERVAL integer part)
  10. DATETIME_TRUNC : DATETIME_TRUNC(date_expression, part)
  11. DAY : Day(date_expression)
  12. EXTRACT : EXTRACT(part FROM date_expression)
  13. FORMAT_DATETIME : FORMAT_DATETIME(format_string, datetime_expression)
  14. HOUR : HOUR(datetime_expression)
  15. MINUTE : MINUTE(datetime_expression)
  16. MONTH : MONTH(date_expression)
  17. PARSE_DATE : PARSE_DATE(format_string, text)
  18. PARSE_DATETIME : PARSE_DATETIME(format_string, text)
  19. QUARTER : QUARTER(date_expression)
  20. SECOND : SECOND(datetime_expression)
  21. TODATE : TODATE(X, Input Format, Output Format)
  22. TODAY : TODAY([time_zone])
  23. UNIX_DATE : UNIX_DATE(date_expression)
  24. WEEK : WEEK(Date)
  25. WEEKDAY : WEEKDAY(Date)
  26. YEAR : YEAR(Date)
  27. YEARWEEK : YEARWEEK(Date)

Note: Date functions assume UTC as their timezone. Date functions can optionally take either an input or output (or both) format string.

Geo functions

These functions are used to manipulate geographic location data. 

Following are examples of Geo functions:

  1. TOCITY : TOCITY(X [,Input Format])
  2. TOCONTINENT : TOCONTINENT(X [,Input Format])
  3. TOCOUNTRY : TOCOUNTRY(X [,Input Format])
  4. TOREGION : TOREGION(X [,Input Format])
  5. TOSUBCONTINENT : TOSUBCONTINENT(X [,Input Format])

Text functions

These functions are used to manipulate string data. 

Following are examples of text functions:

  1. CONCAT : CONCAT(X, Y)
  2. CONTAINS_TEXT : CONTAINS_TEXT(X, text)
  3. ENDS_WITH  : ENDS_WITH(X, text)
  4. LEFT_TEXT  : LEFT_TEXT(X, length)
  5. LENGTH : LENGTH(X)
  6. LOWER : LOWER(X)
  7. REGEXP_CONTAINS : REGEXP_CONTAINS(X, regular_expression)
  8. REGEXP_EXTRACT : REGEXP_EXTRACT(X, regular_expression)
  9. REGEXP_MATCH : REGEXP_MATCH(X, regular_expression)
  10. REGEXP_REPLACE : REGEXP_REPLACE(X, regular_expression, replacement)
  11. REPLACE : REPLACE(X, Y, Z)
  12. RIGHT_TEXT  : RIGHT_TEXT(X, length)
  13. STARTS_WITH  : STARTS_WITH(X, text)
  14. SUBSTR : SUBSTR(X, start index, length)
  15. TRIM : TRIM(X)
  16. UPPER : UPPER(X)

Conditional functions

These functions are used to evaluate a certain condition and then return the values.

Following are examples of text functions and their syntax:

  1. CASE:  Its Syntax is like below
CASE
   WHEN condition1 THEN result1
   [WHEN condition2 THEN result2]
   [...]
   [ELSE else_result3]
END
  1. CASE  (Simple): Its syntax is like below
CASE input_expression
WHEN expression_to_match1 THEN result1
[WHEN expression_to_match2 THEN result2]
[...]
[ELSE result3]
END
  1. COALESCE: Returns the first non-missing value found in a list of fields. Its syntax is like below
COALESCE(field_expression[,field_expression, ...])

  1. IFNull: Returns a result if the input is null, otherwise, returns the input. Its syntax is like below
IFNULL(input_expression, null_result)

  1. IF : Normal IF condition and its suntax is like below
IF(condition, true_result, false_result)

Miscellaneous functions

Functions that do not fall in any of the below categories come under the ‘miscellaneous functions’ category:

  1. Aggregation functions
  2. Arithmetic functions
  3. Date functions
  4. Geo functions
  5. Text functions
  6. Conditional functions

Following are the examples of miscellaneous functions:

  1. CAST: CAST : CAST(field_expression AS TYPE)
  2. HYPERLINK : HYPERLINK : HYPERLINK(URL, link label)
  3. IMAGE : IMAGE : IMAGE(Image URL, [Alternative Text])

Frequently Asked Questions About Looker Studio Functions, Formulas Tutorial

What are functions in Looker Studio?

Functions in Looker Studio are formulas which are used to manipulate data, aggregate data and do mathematical calculations on data.

Functions make your calculated fields more powerful. They are used inside of calculated fields as a formula.

How many functions does Looker Studio provide?

Looker Studio provides over 50 functions that you can use inside of calculated fields as a formula.

What are the attributes of a function in Looker Studio?

Following are the typical attributes of a function in Looker Studio
– Every function in Looker Studio has some purpose. 
– Every function in Looker Studio has syntax
– All functions expect one or more parameters
– Parameters are listed in the syntax
– All functions return a value
– Almost all functions have certain restrictions regarding how they can be used.

What types of functions are available in Looker Studio?

All of the Looker Studio functions are grouped into the following categories:
– Aggregation functions
– Arithmetic functions
– Date functions
– Geo functions
– Text functions
– Miscellaneous functions

  1. Looker Studio Alternative – Whatagraph
  2. Looker Studio Parameters explained with examples
  3. Looker Studio Number Formats / Data Types
  4. How to Change Language in Looker Studio
  5. Looker Studio Date Format and Time Explained
  6. Looker Studio Tutorial with FREE PDF ebook
  7. How to work with the Boolean field in Looker Studio
  8. Looker Studio Page Navigation Tutorial
  9. Understanding Report Editor in Looker Studio
  10. Best practices for creating a report in Looker Studio
  11. How to share reports in Looker Studio
  12. Seven methods to create a new report in Looker Studio
  13. Looker Studio Report Tutorial
  14. How to invite people to view or edit a report in Looker Studio
  15. How to share the link of your report in Looker Studio
  16. Schedule email delivery of a report in Looker Studio
  17. How to download Looker Studio report as PDF
  18. How to embed a Looker Studio report on a website
  19. Guide to Data Types in Looker Studio
  20. Image function in Looker Studio
  21. Image Link data type in Looker Studio
  22. Image data type in Looker Studio
  23. Looker Studio Geo Map – Latitude Longitude
  24. Why You Should Avoid Using Functions and Calculated Fields in Looker Studio
  25. Looker Studio Calculated Fields Tutorial
  26. Working with the Text data type in Looker Studio
  27. The Data Set Configuration Error in Looker Studio
  28. Data Source Fields in Looker Studio – Tutorial
  29. Refresh data source schema fields in Looker Studio
  30. Looker Studio Data Sources – Tutorial
  31. Looker Studio Dimension vs Metric
  32. How to filter by email in Looker Studio
  33. Looker Studio – Sharing Data Sources (aka data source schema)
  34. Field editing in reports – Looker Studio
  35. Data Source Version History in Looker Studio
  36. Community Visualization Access in Looker Studio
  37. Understanding Data Source Credentials in Looker Studio
  38. Understanding Data Freshness in Looker Studio
  39. How to create and configure a data source in Looker Studio
  40. Looker Studio Aggregation Explained
  41. How to Edit a Calculated Field in Looker Studio
  42. Formula Rejection in Looker Studio
  43. Doing Basic Maths on Numeric Fields via Calculated Fields

Register for the FREE TRAINING...

"How to use Digital Analytics to generate floods of new Sales and Customers without spending years figuring everything out on your own."



Here’s what we’re going to cover in this training…

#1 Why digital analytics is the key to online business success.

​#2 The number 1 reason why most marketers are not able to scale their advertising and maximize sales.

#3 Why Google and Facebook ads don’t work for most businesses & how to make them work.

#4 ​Why you won’t get any competitive advantage in the marketplace just by knowing Google Analytics.

#5 The number 1 reason why conversion optimization is not working for your business.

#6 How to advertise on any marketing platform for FREE with an unlimited budget.

​#7 How to learn and master digital analytics and conversion optimization in record time.



   

My best selling books on Digital Analytics and Conversion Optimization

Maths and Stats for Web Analytics and Conversion Optimization
This expert guide will teach you how to leverage the knowledge of maths and statistics in order to accurately interpret data and take actions, which can quickly improve the bottom-line of your online business.

Master the Essentials of Email Marketing Analytics
This book focuses solely on the ‘analytics’ that power your email marketing optimization program and will help you dramatically reduce your cost per acquisition and increase marketing ROI by tracking the performance of the various KPIs and metrics used for email marketing.

Attribution Modelling in Google Analytics and BeyondSECOND EDITION OUT NOW!
Attribution modelling is the process of determining the most effective marketing channels for investment. This book has been written to help you implement attribution modelling. It will teach you how to leverage the knowledge of attribution modelling in order to allocate marketing budget and understand buying behaviour.

Attribution Modelling in Google Ads and Facebook
This book has been written to help you implement attribution modelling in Google Ads (Google AdWords) and Facebook. It will teach you, how to leverage the knowledge of attribution modelling in order to understand the customer purchasing journey and determine the most effective marketing channels for investment.

About the Author

Himanshu Sharma

  • Founder, OptimizeSmart.com
  • Over 15 years of experience in digital analytics and marketing
  • Author of four best-selling books on digital analytics and conversion optimization
  • Nominated for Digital Analytics Association Awards for Excellence
  • Runs one of the most popular blogs in the world on digital analytics
  • Consultant to countless small and big businesses over the decade

Get the FREE ebook on Looker Studio (formerly Google Data Studio) (75 pages)

X
error: Alert: Content is protected !!