[Analytics] Functions and Operators
The content in this article is appropriate for: Administrators and Supervisors
This article can also be found on our partner website: Looker functions and operators | Google Cloud
This article covers:
If your admin has granted you the permissions to create table calculations, you can use the following features to quickly perform common functions without needing to create SC Analytics (Looker) expressions:
Shortcut Calculations to quickly perform common calculations on numeric fields that are in an Explore's data table
If your admin has granted you the permissions to create custom fields, you can use the following features to quickly perform common functions without needing to create SC Analytics (Looker) expressions:
Custom groups to quickly group values under custom labels without needing to develop
CASE WHEN
logic insql
parameters ortype: case
fieldsCustom bins to group numeric type dimensions in custom tiers without needing to develop
type: tier
LookML fields
Looker expressions (sometimes referred to as Lexp) are used to perform calculations for:
Table calculations (which include expressions used in data tests)
A major part of these expressions is the functions and operators that you can use in them. The functions and operators can be divided into a few basic categories:
Mathematical: Number-related functions
String: Word- and letter-related functions
Dates: Date- and time-related functions
Logical transformation: Includes boolean (true or false) functions and comparison operators
Positional transformation: Retrieving values from different rows or pivots
Some functions are only available for table calculations
Expressions for custom filters and custom fields do not support SC Analytics (Looker) functions that convert datatypes, aggregate data from multiple rows, or refer to other rows or pivot columns. These functions are supported only for table calculations (including table calculations used in the expression
parameter of a data test).
This page is organized to clarify which functions and operators are available, depending on where you are using your expression.
Mathematical Functions and Operators
Mathematical functions and operators work in one of two ways:
Some mathematical functions perform calculations based on a single row. For example, rounding, taking a square root, multiplying, and similar functions can be used for values in a single row, returning a distinct value for each and every row. All mathematical operators, such as
+
, are applied one row at a time.Other mathematical functions, like averages and running totals, operate over many rows. These functions take many rows and reduce them to a single number, then display that same number on every row.
Functions for any expression
Function | Syntax | Purpose |
---|---|---|
|
| Returns the absolute value of |
|
| Returns e to the power of |
|
| Returns the natural logarithm of |
|
| Returns |
|
| Returns a random number between 0 and 1. |
|
| Returns |
|
| Returns the square root of |
Functions for table calculations only
Many of these functions operate over many rows and will only consider the rows returned by your query.
Function | Syntax | Purpose |
---|---|---|
|
| Returns the inverse cosine of |
|
| Returns the inverse sine of |
|
| Returns the inverse tangent of |
|
| Returns the position of |
|
| Returns the position of |
|
| Returns the probability of getting |
|
| Returns the smallest number |
|
| Returns the position of |
|
| Returns the position of |
|
| Returns the probability for the chi-squared test for independence between |
|
| Returns the number of ways of choosing |
|
| Returns half the width of the normal confidence interval at significance level |
|
| Returns half the width of the Student's t-distribution confidence interval at significance level |
|
| Returns the correlation coefficient of |
|
| Returns the cosine of |
|
| Returns the count of non- |
|
| Returns the count of distinct non- |
|
| Returns the population covariance of |
|
| Returns the sample covariance of |
|
| Converts |
|
| Returns the position of |
|
| Returns the position of |
|
| Returns the position of |
|
| Returns the factorial of |
|
| Returns the position of |
|
| Returns the position of |
|
| Returns the geometric mean of the column created by |
|
| Returns the probability of getting |
|
| Returns the intercept of the linear regression line through the points determined by |
|
| Returns the sample excess kurtosis of the column created by |
|
| Returns the |
|
| Returns the row number of the first occurrence of |
|
| Returns the max of the column created by |
|
| Returns the mean of the column created by |
|
| Returns the median of the column created by |
|
| Returns the min of the column created by |
|
| Returns the mode of the column created by |
|
| Returns the factorial of the sum of the arguments divided by the product of each of their factorials. |
|
| Returns the probability of getting |
|
| Returns the position of |
|
| Returns the position of |
|
| Returns the position of |
|
| Returns the position of |
|
| Returns the rank of Sample Usage:
|
|
| Returns the value from the column created by |
|
| Returns the value of pi. |
|
| Returns the position of |
|
| Returns the product of the column created by |
|
| Converts |
|
| Returns the rank of |
|
| Returns the average rank of |
|
| Returns a running product of the values in |
|
| Returns a running total of the values in |
|
| Returns the sine of |
|
| Returns the sample skewness of the column created by |
|
| Returns the slope of the linear regression line through points determined by |
|
| Returns the |
|
| Returns the standard deviation (population) of the column created by |
|
| Returns the standard deviation (sample) of the column created by |
|
| Returns the sum of the column created by |
|
| Returns the position of |
|
| Returns the position of |
|
| Returns the result of a Student's t-test on the data from |
|
| Returns the tangent of |
|
| Returns the variance (population) of the column created by |
|
| Returns the variance (sample) of the column created by |
|
| Returns the position of |
|
| Returns the one-tailed p-value of the z-test using the existing |
Operators for any SC Analytics (Looker) expression
You can use the following standard mathematical operators:
Operator | Syntax | Purpose |
---|---|---|
|
| Adds |
|
| Subtracts |
|
| Multiplies |
|
| Divides |
String Functions
String functions operate on sentences, words, or letters, which are collectively called "strings." You can use string functions to capitalize words and letters, extract parts of a phrase, check to see if a word or letter is in a phrase, or replace elements of a word or phrase. String functions can also be used to format the data returned in the table.
Functions for any SC Analytics (Looker) expression
Function | Syntax | Purpose |
---|---|---|
|
| Returns |
|
| Returns |
|
| Returns the number of characters in |
|
| Returns |
|
| Returns the start index of |
|
| Returns |
|
| Returns the substring of |
|
| Returns |
Functions for table calculations only
Function | Syntax | Purpose |
---|---|---|
|
| Returns a list of strings in |
|
| Returns the number represented by |
|
| Returns the string representation of |
Date Functions
Date functions enable you to work with dates and times.
Functions for any SC Analytics (Looker) expression
Function | Syntax | Purpose |
---|---|---|
|
| Adds |
|
| Adds |
|
| Adds |
|
| Adds |
|
| Adds |
|
| Adds |
|
| Returns " |
|
| Returns |
|
| Returns the number of days between |
|
| Returns the number of hours between |
|
| Returns the number of minutes between |
|
| Returns the number of months between |
|
| Returns the number of seconds between |
|
| Returns the number of years between |
|
| Extracts the days from |
|
| Extracts the hours from |
|
| Extracts the minutes from |
|
| Extracts the months from |
|
| Extracts the seconds from |
|
| Extracts the years from |
|
| Returns the current date and time. |
|
| Truncates |
|
| Truncates |
|
| Truncates |
|
| Truncates |
|
| Truncates |
Functions for table calculations only
Function | Syntax | Purpose |
---|---|---|
|
| Returns the date and time corresponding to |
Logical Functions, Operators, and Constants
Logical functions and operators are used to assess whether something is true or false. Expressions using these elements take a value, evaluate it against some criteria, return Yes
if the criteria are met, and No
if the criteria are not met. There are also various logical operators for comparing values and combining logical expressions.
Functions for any SC Analytics (Looker) expression
Function | Syntax | Purpose |
---|---|---|
|
| Allows conditional logic with multiple conditions and outcomes. Returns |
|
| Returns the first non- |
|
| If |
|
| Returns |
Operators for any SC Analytics (Looker) expression
The following comparison operators can be used with any data type:
Operator | Syntax | Purpose |
---|---|---|
|
| Returns |
|
| Returns |
The following comparison operators can be used with numbers, dates, and strings:
Operator | Syntax | Purpose |
---|---|---|
|
| Returns |
|
| Returns |
|
| Returns |
|
| Returns |
You also can combine expressions with these logical operators:
Operator | Syntax | Purpose |
---|---|---|
|
| Returns |
|
| Returns |
|
| Returns |
⚠️ These logical operators must be capitalized. Logical operators written in lowercase will not work.
Logical constants
You can use logical constants in Looker expressions. These constants are always written in lowercase and have the following meanings:
Constant | Meaning |
---|---|
| True |
| False |
| No value |
Note that the constants yes
and no
are the special symbols that mean true or false in expressions. In contrast, using quotes such as in "yes"
and "no"
creates literal strings with those values.
Logical expressions evaluate to true or false without requiring an if
function. For example, this:
if(${field} > 100, yes, no)
is equivalent to this:
${field} > 100
You also can use null
to indicate no value. For example, you may want to determine if a field is empty, or assign an empty value in a certain situation. This formula returns no value if the field is less than 1, or the value of the field if it is more than 1:
if(${field} < 1, null, ${field})
Combining AND
and OR
operators
AND
operators are evaluated before OR
operators, if you don't otherwise specify the order with parentheses. Thus, the following expression without additional parentheses:
if (
${order_items.days_to_process}>=4 OR
${order_items.shipping_time}>5 AND
${order_facts.is_first_purchase},
"review", "okay")
would be evaluated as:
if (
${order_items.days_to_process}>=4 OR
(${order_items.shipping_time}>5 AND ${order_facts.is_first_purchase}),
"review", "okay")
Positional Functions
When creating table calculations, you can use positional transformation functions to extract information about fields in different rows or pivot columns. You can also create lists and retrieve the current row or pivot column index.
Column and row totals for table calculations only
If your Explore contains totals, you can reference total values for columns and rows:
Function | Syntax | Purpose |
---|---|---|
|
| Returns the column total of the field. |
|
| Returns the row total of the field. |
Row-related functions for table calculations only
Some of these functions use the relative positions of rows, so changing the sort order of the rows affects the results of the functions.
Function | Syntax | Purpose |
---|---|---|
|
| Returns the value of the |
|
| Creates a list out of the given values. |
|
| Returns the value in |
|
| Returns the value of row |
|
| Returns a list of the |
|
| Returns the current row number. |
Pivot-related functions for table calculations only
Some of these functions use the relative positions of pivot columns, so changing the sort order of the pivoted dimension affects the results of those functions.
Function | Syntax | Purpose |
---|---|---|
|
| Returns the index of the current pivot column. |
|
| Evaluates |
|
| Returns the value of the |
|
| Returns a list of the |
|
| Returns the pivoted values of |
|
| Returns the value of |
The specific pivot functions you use determine whether the table calculation is displayed next to each pivoted column, or is displayed as a single column at the end of the table.
Filter Functions for Custom Filters and Custom Fields
Filter functions let you work with filter expressions to return values based on filtered data. Filter functions work in custom filters, filters on custom measures, and custom dimensions, but are not valid in table calculations.
Function | Syntax | Purpose |
---|---|---|
|
| Returns |