Top.Mail.Ru
Formula Functions
CTRL+K

Functions for Formulas

In this article
  • Functions for Formulas
  • Selecting Functions for Custom Indicators
  • Supported Syntax
  • ClickHouse Functions
  • Sequence Functions
  • Date and Time Functions
  • Aggregate Functions
  • Mathematical Functions
  • String Functions
  • Array Functions
  • Other Functions
  • Operavix Custom Functions
  • Sequence Functions
  • Aggregate Functions

The system supports two types of functions for creating formulas and performing calculations:

Key formula functions and usage examples are listed below. For the complete list of ClickHouse functions, refer to the official documentation.

Selecting Functions for Custom Indicators

The following functions are available for creating custom indicators:

  • Measures, event indicators, transition indicators, and process indicators support sequence functions, date and time functions, array functions, as well as aggregate, mathematical, string, and other functions
  • Dimensions support date and time functions, array functions, mathematical, string, and other functions

The distinct operator enables calculations based only on unique values in a table column:

count(distinct "case_table"."case_id")

The conditional expression if supports building complex indicators with conditions: if(cond, then, else).

Arguments of the if expression:

  • cond – a condition that evaluates to 0 or non-zero
  • then – the result returned if the condition cond is true
  • else – the result returned if the condition cond is false

Example:

if("activity"."element_ctrl" = 10, 'Button', 'Text field')

Supported Syntax

The system supports the following capabilities for writing formulas:

  • String concatenation — combining multiple strings into one using ||. Example: "activity"."program" || ' > ' || "activity"."file_path"
  • Ternary operator — a three-argument operator that evaluates a condition and assigns one of two values based on whether the condition is true: cond ? then : else. The first argument is the condition. If true, the operator returns the second argument; if false, it returns the third. Example: "activity"."element_ctrl" = 10 ? 'Button' : 'Text field'
Note

These capabilities are supported, but syntax highlighting or other visual cues may not be available.

ClickHouse Functions

Sequence Functions

FunctionDescriptionExample
retention(cond1, cond2, ..., cond32)Shows how well a set of conditions is maintained, for example, user retention over time. Accepts 1 to 32 logical conditions and applies them to a datasetretention("activity"."date" = '2024-05-02', "activity"."date" = '2024-05-03',"activity"."date" = '2024-05-04')
sequenceCount(pattern)(time, cond1, cond2, ...)Counts non-overlapping event chains matching a pattern. Begins searching for the next chain only after the current one fully matchessequenceCount('(?1).*(?2)')("activity"."time","activity"."type" = 10, "activity"."type" = 14)
sequenceMatch(pattern)(timestamp, cond1, cond2, ...)Checks whether an event sequence contains a chain matching the specified patternsequenceMatch('(?1).*(?2)')("activity"."time","activity"."type" = 10, "activity"."type" = 14)

Date and Time Functions

FunctionDescriptionExample
date_add(time_unit, value, x)Adds a time interval to a timestampdate_add(day, 10, toDate('2022-10-31'))
dateDiff(time_unit, date1, date2)Subtracts one timestamp from anotherdateDiff('day', toDateTime('2022-10-21 01:00:00'), toDateTime('2022-10-31 19:00:00'))
date_sub(time_unit, value, x)Subtracts a time interval from a timestampdate_sub(day, 10, toDate('2022-10-31'))
date_trunc(time_unit, x)Truncates a timestamp to the specified unitdate_trunc('hour', now())
formatDateTime(x, format)Formats a timestamp according to a custom formatformatDateTime(toDate('2022-10-31'), ' %D')
now()Returns the current date and timenow()
now(timezone)Returns the current date and time in the specified timezone. timezone — the target timezonenow('Europe/Moscow')
parseDateTimeBestEffortOrZeroParses timestamps from uploaded tables in arbitrary formatsparseDateTimeBestEffortOrZero('2022-10-31 18:00:00-5:00')
parseDateTime64BestEffortOrZeroParses high-precision timestamps from uploaded tables in arbitrary formatsparseDateTime64BestEffortOrZero('2022-10-31 18:00:00-5:00')
today()Takes no arguments and returns the current date at query execution timetoday()
toDate(x)Converts a datetime to a datetoDate('2022-10-31 17:00:00')
toDayOfMonth(x)Returns the day of the month (1–31) as a UInt8toDayOfMonth(toDate('2022-10-31'))
toDayOfWeek(x)Returns the day of the week (Monday = 1, Sunday = 7) as a UInt8toDayOfWeek(toDate('2022-10-31 17:00:00'))
toHour(x)Returns the hour of the day (0–23) as a UInt8toHour(now())
toInterval(Year — Quarter — Month — Week — Day — Hour — Minute — Second)Creates a time interval of the specified type for time arithmetictoDate('2022-10-31') + toIntervalWeek(1)
toMonday(x)Rounds a date down to the nearest MondaytoMonday(toDate('2022-10-30 17:10:00'))
toMonth(x)Returns the month number (1–12) as a UInt8toMonth(toDate('2022-10-31 17:10:00'))
toStartOfDay(x)Rounds a datetime down to the start of the daytoStartOfDay(toDate('2022-10-30 17:10:00'))
toUnixTimestamp
toUnixTimestamp64Milli
Converts to seconds or milliseconds since Unix epochtoUnixTimestamp('2022-10-31 08:07:47')
toYear(x)Returns the year (AD) as a UInt16toYear(toDate('2022-10-31 17:10:00'))

Aggregate Functions

FunctionDescriptionExample
any(x)Returns any encountered value of xany("case_table"."duration")
argMax(arg, val)Returns arg corresponding to the maximum valargMax("case_table"."duration", "case_table"."duration_type")
argMaxIf(arg, val, condition)Returns arg corresponding to the maximum val with a conditionargMaxIf("case_table"."duration", "case_table"."duration_type", "case_table"."case_crm_type_lvl2"='Telephone service unavailable')
argMin(arg, val)Returns arg corresponding to the minimum valargMin("case_table"."duration", "case_table"."duration_type")
argMinIf(arg, val, condition)Returns arg corresponding to the minimum val with a conditionargMinIf("case_table"."duration", "case_table"."duration_type", "case_table"."case_crm_type_lvl2"='Telephone service unavailable')
avg(x)Computes the average. Works only for numeric types. Result is always Float64avg("case_table"."duration")
avgIf(x, condition)Computes the average with a condition. Works only for numeric types. Result is always Float64avgIf("case_table"."duration", "case_table"."case_crm_type_lvl2"='Broadband connection failure')
count(x)Returns the number of rowscount("case_table"."case_crm_type_lvl3")
countIf(x, condition)Returns the number of rows matching a conditioncountIf("case_table"."dir_tech_def", "case_table"."dir_tech_def"='PON')
max(x)Computes the maximum valuemax("case_table"."duration")
maxIf(x, condition)Computes the maximum value with a conditionmaxIf("case_table"."duration", "case_table"."rf_name"='TOMSK BRANCH')
median(x)Computes the medianmedian("case_table"."duration")
medianIf(x, condition)Computes the median with a conditionmedianIf("case_table"."duration", "case_table"."rf_name"='IRKUTSK BRANCH')
min(x)Computes the minimum valuemin("case_table"."duration")
minIf(x, condition)Computes the minimum value with a conditionminIf("case_table"."duration", "case_table"."rf_name"='TOMSK BRANCH')
quantile(level)(x)Computes the quantile at the specified levelquantile(0.5)("case_table"."duration")
quantileIf(level)(x, condition)Computes the quantile at the specified level with a conditionquantileIf(0.5)("case_table"."duration", "case_table"."rf_name"='TOMSK BRANCH')
stddevPop(x)Calculates the population standard deviationstddevPop("case_table"."duration")
sum(x)Computes the sum. Works only for numeric typessum(100)
sumIf(x, condition)Computes the sum with a condition. Works only for numeric typessumIf("case_table"."duration", "case_table"."duration_type"='1 - 5 days')
topK(N)(column)Returns an array of the N most frequent values in the columntopK(3)("case_table"."case_crm_type_lvl3")
uniq(x)Returns the approximate number of unique valuesuniq("case_table"."case_crm_type_lvl3")

Mathematical Functions

FunctionDescriptionExample
abs(x)Returns the absolute value of xabs(325 / 76)
ceil(x [, n])Rounds x up to the nearest integer or to n decimal placesceil(1278.544, 1)
floor(x [, n])Rounds x down to the nearest integer or to n decimal placesfloor(1278.446, 1)
intDiv(x, y)Performs integer division of x by yintDiv(34320, 156)
modulo(x, y)Returns the remainder of dividing x by ymodulo(1789, 2)
pow(x, y)Raises x to the power of ypow(14, 7)
round(x[, n])Rounds x to the nearest integer or to n decimal placesround(1278.546, 1)
sqrt(x)Returns the square root of xsqrt(1089)

String Functions

FunctionDescriptionExample
arrayStringConcat(arr, [separator])Joins array elements into a single string
concat(s1,s2..)Concatenates strings without separatorsconcat("case_table"."mrf_name", "case_table"."rf_name")
domainWithoutWWW(url)Extracts the domain from a URLdomainWithoutWWW('https://operavix.com/business-intelligence/')
extract(s, pattern)Extracts a substring matching a regular expressionextract("case_table"."mrf_name", '\\+')
extractAll(s, pattern)Extracts all substrings matching a regular expression as an arrayextractAll("case_table"."mrf_name", '\\+')
ilike(s, pattern)Performs case-insensitive pattern matchingilike("case_table"."rf_name", 'ФИЛИАЛ')
leftPad(s, length, pad_s)Pads the string on the left to the specified lengthleftPad("case_table"."rf_name", 7, '*')
like(haystack, pattern)Matches a string against a simple pattern with % and _ wildcardslike("case_table"."mrf_name", '_')
lowerUTF8(s)Converts a string to lowercaselowerUTF8("case_table"."dir_services_def")
match(s, pattern)Checks if a string matches a regular expressionmatch("case_table"."rf_name", '\\+')
rightPad(s, length, pad_s)Pads the string on the right to the specified lengthrightPad("case_table"."rf_name", 7, '*')
replaceRegexpOne(s, pattern, replacement)Replaces the first match of a patternreplaceRegexpOne("case_table"."start", '\\.', '-')
replaceRegexpAll(s, pattern, replacement)Replaces all matches of a patternreplaceRegexpAll("case_table"."start", '\\.', '-')
substringUTF8(s, offset, length)Extracts a substringsubstringUTF8("case_table"."mrf_name", 1, 100000)
trim(s)Removes leading and trailing whitespacetrim("case_table"."rf_name")
upperUTF8(s)Converts a string to uppercaseupperUTF8("case_table"."dir_services_def")

Array Functions

FunctionDescriptionExample
arrayAvg(arr)Returns the average of array elementsarrayAvg("case_table"."str_arr")
arrayDistinct(arr)Returns an array with unique elementsarrayDistinct("case_table"."str_arr")
arrayEnumerate(arr)Returns [1, 2, 3, ..., length(arr)]arrayEnumerate("case_table"."str_arr")
arrayEnumerateUniq(arr)Returns an array indicating the occurrence count of each elementarrayEnumerateUniq("case_table"."str_arr")
arrayFill(func, arr)Fills nulls forward using the last non-null valuearrayFill(x -> not isNull(x), "case_table"."str_arr")
arrayFilter(func, arr)Returns an array filtered by a lambda functionarrayFilter(x -> x >= 3, "case_table"."num_arr")
arrayFlatten(arr)Flattens an array of arraysarrayFlatten("case_table"."flt_arr")
arrayMap(func, arr)Applies a function to each array elementarrayMap(x -> x + 1, "case_table"."num_arr")
arrayMax(arr)Returns the maximum array elementarrayMax("case_table"."str_arr")
arrayMin(arr)Returns the minimum array elementarrayMin("case_table"."str_arr")
arrayPopBack(arr)Removes the last elementarrayPopBack("case_table"."str_arr")
arrayPopFront(arr)Removes the first elementarrayPopFront("case_table"."str_arr")
arrayPushBack(arr, x)Appends an element to the endarrayPushBack("case_table"."str_arr", 'x')
arrayPushFront(arr, x)Prepends an element to the beginningarrayPushFront("case_table"."str_arr", 'x')
arrayReverse(arr)Reverses the arrayarrayReverse("case_table"."str_arr")
arrayReverseSort(arr)Sorts the array in descending orderarrayReverseSort("case_table"."str_arr")
arrayReduce(agg_func, arr)Applies an aggregate function to array elementsarrayReduce('sum', "case_table"."num_arr")
arraySlice(arr, offset, length)Returns a slice of the arrayarraySlice("case_table"."str_arr", 2, 2)
arraySort(arr)Sorts the array in ascending orderarraySort("case_table"."str_arr")
arraySum(arr)Returns the sum of array elementsarraySum("case_table"."str_arr")
arrayUniq(arr)Counts unique elements in the arrayarrayUniq("case_table"."str_arr")
groupArray(x)Aggregates values into an arraygroupArray("activity"."type")
groupUniqArray(x)Aggregates unique values into an arraygroupUniqArray("activity"."type")
has(arr, x)Checks if an element exists in the arrayhas("case_table"."str_arr", 'a')
hasAll(arr, sub_arr)Checks if one array is a subset of anotherhasAll("case_table"."str_arr", ['a','b'])
indexOf(arr, x)Returns the 1-based index of the first occurrence of xindexOf("case_table"."str_arr", 'a')

Other Functions

FunctionDescriptionExample
cityHash64(x)Generates a 64-bit CityHash valuecityHash64("case_table"."num_arr")
coalesce(x, ...)Returns the first non-NULL valuecoalesce("case_table"."mrf_name", "case_table"."rf_name")
CRC32(x)Used for data anonymization together with hexCRC32("case_table"."mrf_name")
empty(x)Checks if a string or array is emptyempty("case_table"."num_arr")
hex(x)Used for data anonymization together with CRC32hex("case_table"."mrf_name")
if(cond, then, else)Conditional expression
isNan(x)Checks if a Float32/Float64 value is NaNisNan("ext_activity"."balance")
isNull(x)Checks if a value is NULLisNull("case_table"."rf_name")
JSONExtract(s, type)Parses JSON and extracts a value of the specified typeJSONExtract("monitoring_activity_json"."location_name", 'Array(String)')
toJSONString(value)Serializes a value to JSONtoJSONString(['abc'])
length(x)Returns string length in bytes or array sizelength("case_table"."mrf_name")
multiIf(cond_1, then_1, cond_2, then_2, ..., else)Compact form of a multi-condition CASE statement
toInt32(expr)Converts a value to Int32toInt32("activity"."name")
toInt64(expr)Converts a value to Int64toInt64("activity"."name")
toString(x)Converts numbers, dates, or datetimes to stringstoString(now())

Operavix Custom Functions

Sequence Functions

FunctionDescriptionExample
begin(column/variable)The start of a transition; accepts a column name as input (used only in transition indicators)avg(end("case_table"."finish") - begin("case_table"."start"))
end(column/variable)The end of a transition (used only in transition indicators)avg(end("case_table"."finish") - begin("case_table"."start"))
variant(event_name, event_time)Returns the process execution scenario as a stringvariant("case_table"."case_id", "case_table"."duration")

Aggregate Functions

FunctionDescriptionExample
process(aggr_func, column)Computes an aggregate expression over a dimension columnprocess(sum("case_table"."duration"), "case_table"."duration_type")

For a detailed description of the process function, its parameters, and usage examples, see Process Function.

Was the article helpful?

Yes
No
Previous
Formula Editor
We use cookies to improve our website for you.