Functions
Functions for custom fields are powerful tools that allow you to enhance the flexibility and depth of your reports by creating and manipulating specialized data fields. These functions enable you to define and calculate values that are not directly available in your raw data, providing a way to tailor your reports to meet specific analytical needs and business requirements.
Analytics supports the following functions:
AVG | IS NULL |
CASE | LEN |
CHARINDEX | LOWER |
CONCAT | MAX |
CONCAT_WS | MIN |
COUNT | MONTH |
COUNT DISTINCT | PATINDEX/POSITION |
DATEADD | REPLACE |
DATEDIFF | SUBSTRING |
DAY | SUM |
GETDATE | UPPER |
IF | YEAR |
Conditional Functions
Conditional functions are built-in functions that return different values or perform different actions based on whether certain conditions are met. They are instrumental in creating custom calculations, formatting data based on specific criteria, and making data-driven decisions within your reports.
CASE WHEN $condition THEN $true [WHEN $cond2 THEN $true2, …] [ELSE $false] END
Checks the $condition and returns the expression of $true or $false accordingly. Multiple conditions are allowed.
-
Return type: mixed (depends on $true and $false type)
-
The return value of this function can be used as a parameter for any other function, as long as the types match. For example:
AVG(CASE WHEN measure > 0 THEN measure1 ELSE measure2 END)
In this case, if measure > 0, then it’s equivalent to AVG(measure1). For this reason, the validation of the $true and $false depends on the calling function (AVG in this example)
-
The following example results in an error because AVG does not accept another AVG or a dimension as a parameter::
AVG(CASE WHEN measure > 0 THEN AVG(measure) ELSE dimension END)
-
- The return type of ALL $true and $false must match.
- If there is no ELSE parameter and none of the WHEN conditions match, the function will return NULL.
- If more than one WHEN condition matches, the function will return the result of the first matching condition.
- When the return type is a dimension (string or date), it cannot have any aggregate function on any parameter of the expression.
Parameter values:
Parameter | Accepted Types | Examples | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
WHEN $condition |
A conditional expression (please check Comparison Operators)
Does not allow a field or expression without a comparison operator |
|
||||||||||||||||
THEN $true |
Any expression that is valid as a Custom Field expression
This parameter can be included more than once to match multiple conditions |
|||||||||||||||||
ELSE $false |
Any expression that is valid as a Custom Field expression
This parameter is optional |
For example, this function:
returns:
IF($condition, $true, false)
Checks the $condition and returns the expression of $true or $false accordingly. This function is similar to CASE WHEN, but has a more simpler usage, allowing only 1 condition and always else.
-
Return type: mixed (depends on $true and $false type)
The same rule of the CASE statement applies here regarding the return value of this function. For example:
AVG(IF(measure>0, measure1, measure2)) is valid.
Check the CASE WHEN information panel for more details.
- The return type of $true and $false must match.
- When the return type is a dimension (string or date), it cannot include any aggregate functions on any parameters of the expression.
Parameter values:
Parameter | Accepted Type | Examples | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
$condition | A conditional expression (for more information, see Comparison Operators). Does not allow a field or expression without a comparison operator |
|
||||||||||||||||||||
$true | Any expression that is valid as a Custom Field expression | |||||||||||||||||||||
$false | Any expression that is valid as a Custom Field expression |
For example, this function:
returns:
Aggregation Functions
Aggregations functions can be used to summarize and condense large volumes of data, making it easier to interpret patterns, trends, and overall performance. Whether you’re analyzing sales figures, financial metrics, or operational statistics, aggregation functions provide a way to aggregate multiple data points into a single, meaningful value.
AVG($field)
-
Returns the average of a measure $field, aggregated by the dimensions.
Return type: Number
Parameter values:
Parameter | Description | Accepted Types | Examples | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
$field | The field that contains the value. |
Any measure
Does not allow other aggregate functions Does not allow static values |
|
For example, this function:
returns:
COUNT
-
Returns the number of the items found in the field.
Return type: Number
Parameter values:
Parameter | Description |
---|---|
$field | The field to count. |
For example, this function:
returns:
COUNT([DISTINCT] $field)
-
Returns the count (or distinct count) of a dimension $field, aggregated by the dimensions.
Return type: Number
Parameter values:
Parameter | Description | Accepted Types | Examples | ||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
DISTINCT | Optional flag to indicate to count unique values. | DISTINCT |
|
||||||||
$field |
The field that contains the value. |
Any dimension, measure or date field Does not allow other aggregate functions Does not allow static values |
|
For example, this function:
returns:
In this example, we have a list of last names and we want to know if they are unique users or if there are users with the same last name. The result shows two users with the last name Decker and 2 users with the last name Jones.
IS NULL($ expression, $replacement)
-
Returns the specified value IF the expression is NULL, otherwise returns the expression.
Return type: Dependent upon the selected parameters.
Parameter values:
Parameter | Description |
---|---|
$expression | The expression to test whether is NULL. |
$replacement | The value to return if expression is NULL. |
For example, this function:
returns:
MAX($field)
-
Returns the maximum value of a $field, aggregated by the dimensions on the widget. MAX will return:
For measures, the highest number on the set (MAX of 1 and 2 returns 2)
For dates, the highest date (MAX of 2022-01-01 and 2023-01-01 will return the latest)
For dimensions, the latest string on lexicographic order (MAX of ‘Earth’ and ‘Moon’ returns ‘Moon’)
Return type: The same as parameter field (number, string or date)
Parameter values:
Parameter | Description | Accepted Types | Examples | ||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
$field | The field that contains the value. |
Any field
Does not allow other aggregate functions Does not allow static values |
|
For example, this function:
returns:
MIN($field)
-
Returns the minimum value of a $field, aggregated by the dimensions on the widget. MIN returns:
For measures, the lowest number on the set (MIN of 1 and 2 returns 1)
For dates, the lowest date (MIN of 2022-01-01 and 2023-01-01 will return the earliest)
For dimensions, the first string on lexicographic order (MIN of ‘Earth’ and ‘Moon’ returns ‘Earth’)
Return type: The same as parameter field (number, string or date)
Parameter values:
Parameter | Description | Accepted Types | Examples | ||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
$field | The field that contains the value. |
Any field
Does not allow other aggregate functions Does not allow static values |
|
For example, this function:
returns:
SUM($field)
-
Returns the sum value of a measure $field, aggregated by the dimensions on the widget.
Return type: Number
Parameter values:
Parameter | Description | Accepted Types | Examples | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
$field | The field that contains the value. |
Any field
Does not allow other aggregate functions Does not allow static values |
|
String Functions
String functions are designed to handle various text processing tasks, such as concatenation, extraction, replacement, and formatting. These functions are particularly valuable when dealing with custom fields, where the data may not always be in a standard format or may require specific modifications to meet business needs.
CHARINDEX($substring, $string, [$start])
-
Searches for $substring inside $string and returns the position of the first character of that substring.
- A starting point can be specified using the optional $start parameter, which indicates the character position.
- Returns 0 (zero) if there is no substring.
- The index of first character is 1. Searching for substring “abc” on string “abcde” will return 1.
- A $start parameter less than 1 will still initiate the search from the beginning of the string.
Return type: Number
Parameter values:
Parameter | Description | Accepted Types | Examples | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
$substring | The substring to be searched. This parameter must be in single quotes. | Any dimension, date or static string |
|
||||||||||
$string | The string from which the substring is to be searched. | Any dimension, date or static string | |||||||||||
$start |
Optional: This parameter is only used when you need to specify a starting position in the string apart from the start of the string. Note: You must delete this parameter if you are not using it. |
Any measure or static number |
For example, this function:
returns:
CONCAT($field1, $field2, […])
-
Joins two or more strings, returning the result. Anything that is not a string will be casted as string.
Return type: String
Parameter values:
Parameter | Description | Accepted Types | Examples | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
$field1 | The fields to add together. | Anything |
|
||||||||||
$field2 | The fields to add together. | Anything | |||||||||||
[…] | Optional | More parameters of any type |
For example, this function:
returns:
CONCAT_WS($separator, $field1, $field2, […])
-
Returns two or more strings with a separator. Anything that is not a string will be casted as string.
Return type: String
Parameter values:
Parameter | Description | Accepted Types | Examples | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
$separator |
The separator to use.
Note: This parameter must be in single quotes. |
Any dimension, date field or static string |
|
||||||||||
$field1 | The first field to add together. | Anything | |||||||||||
$field2 | The second field to add together. | Anything | |||||||||||
[…] | Optional parameter. | More parameters of any type |
LEN($field)
-
Returns the amount of characters (the length) of a string. Anything that is not a string will be casted as string before length calculation. In scenarios like LEN(20+5), will calculate the formula first, so result is LEN(25) = 2.
Return Type: Number
Parameter values:
Parameter | Description | Accepted Types | Valid Examples |
---|---|---|---|
$field | The field that contains the value. | Anything |
LEN(20)
LEN(20+5) LEN('My text') LEN(dimension) LEN(measure) LEN(datefield) LEN(UPPER(dimension)) LEN(SUM(measure)) |
For example, this function:
returns:
LOWER($field)
-
Returns the same string passed as the parameter with all characters converted to lower case.
Return type: String
Parameter values:
Parameter | Description | Accepted Types | Examples | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
$field | The field that contains the value. | Any dimension or static string |
|
For example, this function:
returns:
PATINDEX/POSITION($pattern, $field)
-
Searches for $pattern inside $field, and returns the position of the first character that matches the pattern. The pattern can include a regular expression. The search is case-insensitive. Returns 0 (zero) if do not find the pattern. The index of first character is 1. Searching for pattern “abc” on string “abcde” will return 1.
Return type: Number
Parameter values:
Parameter | Description | Accepted Types | Examples | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
$pattern |
The pattern to find. It MUST be surrounded by single quotes and %. Other wildcards can be used in pattern, such as:
You can only select non-numeric data fields. |
Static string |
|
||||||||||||||
$field | The field to be searched. You can only select non-numeric data fields. | Any dimension or static string |
For example, these functions:
return:
REPLACE($field, $old, $new)
-
Returns the same string passed as $field, but replacing all occurrences of $old by $new.
Return type: String
Parameter values:
Parameter | Description | Accepted Types | Examples | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
$field | The field that contains the value. | Any dimension or static string |
|
||||||||||
$old | The string to be replaced. | Any dimension or static string | |||||||||||
$new | The replacement string. | Any dimension or static string |
For example, this function:
returns:
SUBSTRING($field, $start, $length)
-
Returns part of the string passed on $field, starting from character at position $start and ending after $length characters. If $start or $length is a float, the value will be ignore the decimal part.
Return type: String
Parameter values:
Parameter | Description | Accepted Types | Examples | ||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
$field | The field that contains the value. | Any dimension or static string |
|
||||||||
$start |
The start position. The first position in the string is 1. Note: Must be a positive number. |
Any measure or number | |||||||||
$length |
The number of characters to extract. Note: Must be a positive number. |
Any measure or number |
For example, this function:
returns:
UPPER($field)
-
Returns the same string passed as parameter, but with all characters converted to upper case.
Return type: String
Parameter values:
Parameter | Description | Accepted Types | Examples | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
$field | The field that contains the value. | Any dimension or static string |
|
For example, this function:
returns:
Date Functions
Date functions in custom fields allow you to transform, analyze, and present time-based data in a way that enhances clarity and relevance. Whether you’re generating sales reports, tracking project timelines, or analyzing trends over time, incorporating date functions can help you perform calculations, filter data, and create dynamic visualizations.
DATEADD($interval, $number, $field)
-
Adds or subtracts a given date (specified by the $field parameter) by a specific amount (indicated by $number) of time (defined by $interval).
Return type: Date/Time
Parameter values:
Parameter | Description | Accepted Types | Examples | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
$interval |
The type of interval to add. Can be one of the following values:
|
Any option of Date Interval data type |
|
||||||||||||
$number |
The value of the time/date interval to add. Both positive and negative values are allowed. Note: To extract, add the minus sign (-). For example, -5. |
Any number or measure | |||||||||||||
$field |
The field that contains the value. |
Any date field, alias or static date |
For example, this function:
returns:
DATEDIFF($interval, $field1, $field2)
-
Returns the difference in numbers (based on $interval) between two dates.
Return type: Number
Parameter values:
Parameter | Description | Accepted Types | Examples | ||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
$interval |
The type of interval to use. Can be one of the following values:
|
Any option of Date Interval data type |
|
||||||||
$field1 |
The field that contains the date. |
Any date field, alias or static date | |||||||||
$field2 | The field that contains the date. | Any date field, alias or static date |
For example, this function:
returns:
DAY($field)
-
Returns the day of a given date. For example, for a date ‘2021-12-31’, it will return 31.
Return type: Number
Parameter values:
Parameter | Description | Accepted Types | Examples | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
$field | The field that contains the value. |
Any date field
Does not allow date aliases |
|
For example, this function:
returns:
GETDATE()
-
Returns the current datetime based on database server configuration. Data returned is a datetime and can be used as any date field, to use in other functions like DATEADD, DATEDIFF, DAY and MAX. For example, to get the amount of days since year start, use:
DATEDIFF(DAY, CONCAT(YEAR(GETDATE()), '-01-01'), GETDATE())
Return type: Date
Parameter values:
No parameters.
For example, this function:
returns:
MONTH($field)
-
Returns the month of a given date. For example, ‘2021-12-31’, returns 12.
Return type: Number
Parameter values:
Parameter | Description | Accepted Types | Examples | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
$field | The field that contains the value. |
Any date field
Does not allow date aliases |
|
For example, this function:
returns:
YEAR($field)
-
Returns the year of a given date. For example, ‘2021-12-31’, returns 2021.
Return type: Number
Parameter values:
Parameter | Description | Accepted Types | Examples | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
$field | The field that contains the value. |
Any date field
Does not allow date aliases |
|
For example, this function:
returns: