Support Portal

Welcome
Login

Analytics Functions and Operators

Table calculations and custom filters use Analytics Expressions. A major part of these expressions are the functions and operators that you can use in them. This page includes information about all of these functions and operators.

Basic Categories

The functions and operators can be divided into a few basic categories:

Some Functions Are Only Available for Table Calculations

Analytics expressions for custom filters can use most functions and operators while table calculations as well as custom fields can use any function or operator. This page is organized to make it clear which functions and operators are available, depending on where you are using your Analytics expression.

The functions that are available only for table calculations are typically functions that convert datatypes, aggregate data from multiple rows, or refer to other rows or pivot columns.

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 Analytics Expression

FunctionSyntaxPurpose
absabs(value)Returns the absolute value of value
ceilingceiling(value)Returns the smallest integer greater than or equal to value
expexp(value)Returns e to the power of value
floorfloor(value)Returns the largest integer less than or equal to value
lnln(value)Returns the natural logarithm of value
loglog(value)Returns the base 10 logarithm of value
modmod(value, divisor)Returns the remainder of dividing value by divisor
powerpower(base, exponent)Returns base raised to the power of exponent
randrand()Returns a random number between 0 and 1
roundround(value, num_decimals)Returns value rounded to num_decimals decimal places
sqrtsqrt(value)Returns the square root of value

Functions for Table Calculations Only

Many of these functions operate over many rows and will only consider the rows returned by your query.

FunctionSyntaxPurpose
acosacos(value)Returns the inverse cosine of value
asinasin(value)Returns the inverse sine of value
atanatan(value)Returns the inverse tangent of value
beta_distbeta_dist(value, alpha,
beta, cumulative)
Returns the position of value on the beta distribution with parameters alphaand beta. If cumulative = yes, returns the cumulative probability
beta_invbeta_inv(probability,
alpha, beta)
Returns the position of probability on the inverse cumulative beta distribution with parameters alpha and beta
binom_distbinom_dist(num_successes,
num_tests,
probability, cumulative)
Returns the probability of getting num_successes successes in num_teststests with the given probability of success. If cumulative = yes, returns the cumulative probability
binom_invbinom_inv(num_tests,
test_probability,
target_probability)
Returns the smallest number k such that binom(k, num_tests,
test_probability, yes)
>= target_probability
chisq_distchisq_dist(value, dof,
cumulative)
Returns the position of value on the gamma distribution with dof degrees of freedom. If cumulative = yes, returns the cumulative probability
chisq_invchisq_inv(probability, dof)Returns the position of probability on the inverse cumulative gamma distribution with dof degrees of freedom
chisq_testchisq_test(actual,
expected)
Returns the probability for the chi-squared test for independence between actual and expected data. actual can be a column or a column of lists, and expected must be the same type.
combincombin(set_size,
selection_size)
Returns the number of ways of choosing selection_size elements from a set of size set_size
confidence_normconfidence_norm(alpha,
stdev, n)
Returns half the width of the normal confidence interval at significance level alpha, standard deviation stdev, and sample size n
confidence_tconfidence_t(alpha,
stdev, n)
Returns half the width of the Student’s t-distribution confidence interval at significance level alpha, standard deviation stdev, and sample size n
correlcorrel(column_1, column_2)Returns the correlation coefficient of column_1 and column_2
coscos(value)Returns the cosine of value
countcount(expression)Returns the count of non-null values in the column defined by expression, unless expression defines a column of Lists, in which case returns the count in each List
count_distinctcount_distinct(expression)Returns the count of distinct non-null values in the column defined by expression, unless expression defines a column of Lists, in which case returns the count in each List
covar_popcovar_pop(column_1,
column_2)
Returns the population covariance of column_1 and column_2
covar_sampcovar_samp(column_1,
column_2)
Returns the sample covariance of column_1 and column_2
degreesdegrees(value)Converts value from radians to degrees
expon_distexpon_dist(value, lambda,
cumulative)
Returns the position of value on the exponential distribution with parameter lambda. If cumulative = yes, returns the cumulative probability
f_distf_dist(value, dof_1,
dof_2, cumulative)
Returns the position of value on the F distribution with parameters dof_1and dof_2. If cumulative = yes, returns the cumulative probability
f_invf_inv(probability, dof_1,
dof_2)
Returns the position of probability on the inverse cumulative F distribution with parameters dof_1 and dof_2
factfact(value)Returns the factorial of value
gamma_distgamma_dist(value, alpha,
beta, cumulative)
Returns the position of value on the gamma distribution with parameters alpha and beta. If cumulative = yes, returns the cumulative probability
gamma_invgamma_inv(probability,
alpha, beta)
Returns the position of probability on the inverse cumulative gamma distribution with parameters alpha and beta
geomeangeomean(expression)Returns the geometric mean of the column created by expression unless expression defines a column of Lists, in which case returns the geometric mean of each List
hypgeom_disthypgeom_dist
(sample_successes,
sample_size,
population_successes,
population_size,
cumulative)
Returns the probability of getting sample_successes from the given sample_size, number of population_successes, and population_size. If cumulative = yes, returns the cumulative probability
interceptintercept(y_column,
x_column)
Returns the intercept of the linear regression line through the points determined by y_column and x_column
kurtosiskurtosis(expression)Returns the sample excess kurtosis of the column created by expressionunless expression defines a column of Lists, in which case returns the sample excess kurtosis of each List
largelarge(expression, k)Returns the kth largest value of the column created by expression unless expression defines a column of Lists, in which case returnsthe kth largest value of each List
matchmatch(value, expression)Returns the row number of the first occurence of value in the column created by expression unless expression defines a column of Lists, in which case returns the position of value in each List
maxmax(expression)Returns the max of the column created by expression unless expressiondefines a column of Lists, in which case returns the max of each List
meanmean(expression)Returns the mean of the column created by expression unless expressiondefines a column of Lists, in which case returns the mean of each List
medianmedian(expression)Returns the median of the column created by expression unless expressiondefines a column of Lists, in which case returns the median of each List
minmin(expression)Returns the min of the column created by expression unless expressiondefines a column of Lists, in which case returns the min of each List
modemode(expression)Returns the mode of the column created by expression unless expressiondefines a column of Lists, in which case returns the mode of each List
multinomialmultinomial(value_1,
value_2, ...)
Returns the factorial of the sum of the arguments divided by the product of each of their factorials
negbinom_distnegbinom_dist(num_failures,
num_successes,
probability,
cumulative)
Returns the probability of getting num_failures failures before getting num_successes successes, with the given probability of success. If cumulative = yes, returns the cumulative probability
norm_distnorm_dist(value, mean,
stdev, cumulative)
Returns the position of value on the normal distribution with the given meanand stdev. If cumulative = yes, then returns the cumulative probability
norm_invnorm_inv(probability, mean,
stdev)
Returns the position of probability on the inverse normal cumulative distribution
norm_s_distnorm_s_dist(value,
cumulative)
Returns the position of value on the standard normal distribution. If cumulative = yes, returns the cumulative probability
norm_s_invnorm_s_inv(probability)Returns the position of probability on the inverse standard normal cumulative distribution
percent_rankpercent_rank(column, value)Returns the rank of value in column as a percentage from 0 to 1 inclusive
percentilepercentile(value_column,
percentile_value)
Returns the value from the column created by expression corresponding to the given percentile_value, unless expression defines a column of Lists, in which case returns the percentile value for each List. Note: percentile_valuemust be between 0 and 1, else this returns null
pipi()Returns the value of pi
poisson_distpoisson_dist(value, lambda,
cumulative)
Returns the position of value on the poisson distribution with parameter lambda. If cumulative = yes, returns the cumulative probability
productproduct(expression)Returns the product of the column created by expression unless expressiondefines a column of Lists, in which case returns the product of each List
radiansradians(value)Converts value from degrees to radians
rankrank(value, expression)Returns the rank of value in the column created by expression unless expression defines a column of Lists, in which case returns the rank of valuein each List
rank_avgrank_avg(value, expression)Returns the average rank of value in the column created by expressionunless expression defines a column of Lists, in which case returns the average rank of value in each List
running_productrunning_product
(value_column)
Returns a running product of the values in value_column
running_totalrunning_total(value_column)Returns a running total of the values in value_column
sinsin(value)Returns the sine of value
skewskew(expression)Returns the sample skewness of the column created by expression unless expression defines a column of Lists, in which case returns the sample skewness of each List
slopeslope(y_column, x_column)Returns the slope of the linear regression line through points determined by y_column and x_column
smallsmall(expression, k)Returns the kth smallest value of the column created by expression unless expression defines a column of Lists, in which case returnsthe kth smallest value of each List
stddev_popstddev_pop(expression)Returns the standard deviation (population) of the column created by expression unless expression defines a column of Lists, in which case returns the standard deviation (population) of each List
stddev_sampstddev_pop(expression)Returns the standard deviation (sample) of the column created by expression unless expression defines a column of Lists, in which case returns the standard deviation (sample) of each List
sumsum(expression)Returns the sum of the column created by expression unless expressiondefines a column of Lists, in which case returns the sum of each List
t_distt_dist(value, dof,
cumulative)
Returns the position of value on the Student’s t-distribution with dof degrees of freedeom. If cumulative = yes, returns the cumulative probability
t_invt_inv(probability, dof)Returns the position of probability on the inverse normal cumulative distribution with dof degrees of freedom
t_testt_test(column_1, column_2,
tails, type)
Returns the result of a Student’s t-test on the data from column_1 and column_2, using 1 or 2 tails. type: 1 = paired, 2 = homoscedastic, 3 = heteroscedastic
tantan(value)Returns the tangent of value
var_popvar_pop(expression)Returns the variance (population) of the column created by expressionunless expression defines a column of Lists, in which case returns the variance (population) of each List
var_sampvar_pop(expression)Returns the variance (sample) of the column created by expression unless expression defines a column of Lists, in which case returns the variance (sample) of each List
weibull_distweibull_dist(value, shape,
scale, cumulative)
Returns the position of value on the Weibull distribution with parameters shape and scale. If cumulative = yes, returns the cumulative probability
z_testz_test(data, value, stdev)Returns the one-tailed p-value of the z-test using the existing data and stdevon the hypothesized mean value.

Operators for Any Analytics Expression

You can use the following standard mathematical operators:

OperatorsSyntaxPurpose
+value_1 + value_2Adds value_1 and value_2
-value_1 - value_2Subtracts value_2 from value_1
*value_1 * value_2Multiplies value_1 and value_2
/value_1 / value_2Divides value_1 by value_2

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. They can also be used to format the data returned in the table.

Functions For Any Analytics Expression

FunctionSyntaxPurpose
concatconcat(value_1, value_2, ...)Returns value_1, value_2, ..., value_n joined as one string
containscontains(string, search_string)Returns Yes if string contains search_string, and No otherwise
lengthlength(string)Returns the number of characters in string
lowerlower(string)Returns string with all characters converted to lower case
positionposition(string, search_string)Returns the start index of search_string in string if it exists, and 0 otherwise
replacereplace(string, old_string, new_string)Returns string with all occurrences of old_string replaced with new_string
substringsubstring(string, start_position, length)Returns the substring of string beginning at start_position consisting of lengthcharacters
upperupper(string)Returns string with all characters converted to upper case

Functions for Table Calculations Only

FunctionSyntaxPurpose
to_numberto_number(string)Returns the number represented by string, or null if the string cannot be converted
to_stringto_string(value)ADDED5.16 Returns the string representation of value, or an empty string if value is null

Date Functions

Date functions enable you to work with dates and times.

Functions For Any Analytics Expression

FunctionSyntaxPurpose
add_daysadd_days(number, date)Adds number days to date
add_hoursadd_hours(number, date)Adds number hours to date
add_minutesadd_minutes(number, date)Adds number minutes to date
add_monthsadd_months(number, date)Adds number months to date
add_secondsadd_seconds(number, date)Adds number seconds to date
add_yearsadd_years(number, date)Adds number years to date
datedate(year, month, day)Returns “year-month-day” date or null if the date would be invalid
date_timedate_time(year, month, day,
hours, minutes, seconds)
Returns
year-month-day hours:minutes:seconds” date or null if the date would be invalid
diff_daysdiff_days(start_date, end_date)Returns the number of days between start_date and end_date
diff_hoursdiff_hours(start_date, end_date)Returns the number of hours between start_date and end_date
diff_minutesdiff_minutes(start_date, end_date)Returns the number of minutes between start_date and end_date
diff_monthsdiff_months(start_date, end_date)Returns the number of months between start_date and end_date
diff_secondsdiff_seconds(start_date, end_date)Returns the number of seconds between start_date and end_date
diff_yearsdiff_years(start_date, end_date)Returns the number of years between start_date and end_date
extract_daysextract_days(date)Extracts the days from date
extract_hoursextract_hours(date)Extracts the hours from date
extract_minutesextract_minutes(date)Extracts the minutes from date
extract_monthsextract_months(date)Extracts the months from date
extract_secondsextract_seconds(date)Extracts the seconds from date
extract_yearsextract_years(date)Extracts the years from date
nownow()Returns the current date and time
trunc_daystrunc_days(date)Truncates date to days
trunc_hourstrunc_hours(date)Truncates date to hours
trunc_minutestrunc_minutes(date)Truncates date to minutes
trunc_monthstrunc_months(date)Truncates date to months
trunc_yearstrunc_years(date)Truncates date to years

Functions for Table Calculations Only

FunctionSyntaxPurpose
to_dateto_date(string)Returns the date and time corresponding to string (YYYY, YYYY-MM, YYYY-MM-DD, YYYY-MM-DD hh, YYYY-MM-DD hh:mm, or YYYY-MM-DD hh:mm:ss)

Logical Functions, Operators, and Constants

Logical functions and operators deal with whether or not something is true or false. This type of function takes the value of something, evaluates it against some criteria, returns true if the criteria is met, and false if the criteria is not met. There are also various logical operators for comparing values and combining logical expressions.

Functions For Any Analytics Expression

FunctionSyntaxPurpose
coalescecoalesce(value_1, value_2, ...)Returns the first non-null value in value_1, value_2, ..., value_n if found and null otherwise
ifif(yesno_expression,
value_if_yes,
value_if_no)
If yesno_expression evaluates to Yes, returns the value_if_yes value. Otherwise, returns the value_if_no value
is_nullis_null(value)Returns Yes if value is null, and No otherwise

Operators For Any Analytics Expression

The following comparison operators can be used with any datatype:

OperatorSyntaxPurpose
=value_1 = value_2Returns Yes if value_1 is equal to value_2, and No otherwise
!=value_1 != value_2Returns Yes if value_1 is not equal to value_2, and No otherwise

The following comparison operators only can be used with numbers and dates:

OperatorSyntaxPurpose
>value_1 > value_2Returns Yes if value_1 is greater than value_2, and No otherwise
<value_1 < value_2Returns Yes if value_1 is less than value_2, and No otherwise
>=value_1 >= value_2Returns Yes if value_1 is greater than or equal to value_2, and No otherwise
<=value_1 <= value_2Returns Yes if value_1 is less than or equal to value_2, and No otherwise

You also can combine Analytics Expressions with these logical operators:

OperatorSyntaxPurpose
ANDvalue_1 AND value_2Returns Yes if both value_1 and value_2 are Yes, and No otherwise
ORvalue_1 OR value_2Returns Yes if either value_1 or value_2 is Yes, and No otherwise
NOTNOT valueReturns Yes if value is No, and Yes otherwise

Logical Constants

You can use logical constants in Analytics Expressions. These constants are always written in lowercase and have the following meanings:

ConstantMeaning
yesTrue
noFalse
nullThere is no value

Note that the constants yes and no, are the special symbols that mean true or false in Analytics 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.

Column and Row Totals for Table Calculations Only

If your Explore contains totals, you can reference total values for columns and rows:

FunctionSyntaxPurpose
:total${field:total}Returns the column total of the field
:row_total${field:row_total}Returns the row total of the field


Row-related Functions for Table Calculations Only

FunctionSyntaxPurpose
indexindex(expression, n)Returns the value of the nth element of the column created by expression, unless expression defines a column of Lists, in which case returns the nth element of each list
listlist(value_1, value_2, ...)Creates a List out of the given values
lookuplookup(value, lookup_column,
result_column)
Returns the value in result_column that is in the same row as value is in lookup_column
offsetoffset(column, row_offset)Returns the value of row (n + row_offset) in column, where n is the current row number
offset_listoffset_list(column, row_offset,
num_values)
Returns a List of the num_values values starting at row (n + row_offset) in column, where n is the current row number
rowrow()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.

FunctionSyntaxPurpose
pivot_columnpivot_column()Returns the index of the current pivot column
pivot_indexpivot_index(expression, pivot_index)Evaluates expression in the context of the pivot column at position pivot_index (1 for first pivot, 2 second pivot, etc.). Returns null for unpivoted results
pivot_offsetpivot_offset(pivot_expression, col_offset)Returns the value of the pivot_expression in position (n + col_offset), where n is the current pivot column position. Returns null for unpivoted results
pivot_offset_listpivot_offset_list(pivot_expression,
col_offset, num_values)
Returns a List of the the num_values values in pivot_expressionstarting at position (n + col_offset), where n is the current pivot index. Returns null for unpivoted results
pivot_rowpivot_row(expression)Returns the pivoted values of expression as a List. Returns null for unpivoted results.
pivot_where
pivot_where(select_expression, expression)Returns the value of expression for the pivot column which uniquely satisfies select_expression or null if such a unique column does not exist.

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.

FunctionSyntaxPurpose
matches_filtermatches_filter(field, `filter_expression`)ADDED5.16 Returns Yes if the value of the field matches the filter expression, No if not.


Matches_filter

Syntax

matches_filter(field, `filter expression`)

The matches_filter function applies the filter expression to the field and returns Yes if the value in the field matches the filter expression or No if it does not.

Examples

This example returns Yes in a custom field if the invoice date is less than 30 days old:

matches_filter(${billing.invoice_date}, `30 days`)

Use the if function with matches_filter to return different values. The next example shows syntax of a custom field that returns “Late” if the invoice date is over 30 days old:

if(matches_filter(${billing.invoice_date}, `30 days`), "Current", "Late")

Things to Know

The string that defines the filter expression must be enclosed in backtick (`) characters.