We know that different database functions have some variations in syntax, commonly referred to as SQL dialects. The unified calculation functions provided by the BI platform act as a middle-layer translation, shielding underlying database dialect differences.
When creating Calculated Fields or Summary Expressions, you can use the system's built-in functions provided by the BI platform or use native database functions.
System Built-in Functions: This article will detail the definition and usage of each function with comprehensive examples. Please refer to the system built-in functions section.
Native Database Functions: Native functions provided by the underlying database. Different databases have different native functions. If needed, you can consult the respective official documentation for function usage.
Unified calculation functions were introduced in BI version 1.16. They support all data sources (including Text and HTTP types) except ElasticSearch and MongoDB. For relational databases, mainstream database types are supported, listed below:
MySQL
Doris
H2
Clickhouse
Oracle
Dameng
PostgreSQL
SQLServer
Presto
Note
If you encounter incorrect function translations for the database types listed above, please contact us.
Why use logical calculations?
Logical calculations allow you to determine whether a specific condition is true or false (Boolean logic). For example, you might want to classify values based on certain conditions.
CASE WHEN condition1 THEN result1 WHEN condition2 THEN result2 [ELSE default]END
Definition
Outputs a value based on WHEN condition evaluation. If none of the WHEN conditions are met, returns the (optional) default value. Returns Null if no default value exists.
Output
Depends on the data type of the THEN values. Note that most databases require consistent output data types across different branches; otherwise, runtime syntax errors may occur.
Example
CASE WHEN `order_rn` = 1 THEN `order_profit` ELSE nullEND
CASE WHEN `gender` = 'F' THEN 1 ELSE 0END
Caution
Note that most databases require consistent output data types across different branches; otherwise, runtime syntax errors may occur.
Rounds [number] to the specified number of decimal places. The optional precise parameter specifies the precision (number of decimal digits) to include in the final result. If decimals is omitted, number is rounded to the nearest integer.
As the name suggests, Floor in English means floor. Rounds [number] down to the nearest integer. Corresponding functions are CEIL (round up) and ROUND.
As the name suggests, Ceil in English means ceiling. Rounds [number] up to the nearest integer. Corresponding functions are FLOOR (round down) and ROUND.
Division A/B, compatible with divisor being 0, solving the issue where direct A/B in databases throws an error when the divisor is 0. Returns [nullValue] when the divisor is 0. If no nullValue is provided, returns Null.
Calculates the change rate from [from] to [to]: (to - from) / from
Compatible when from is 0. Returns [nullValue] when from is 0. If no nullValue is provided, returns Null.
Finds the position of substring substring within original string string. Returns 0 if the substring is not found. The position of the first character in the string is 1. Returns null if the first argument is null.
Returns a substring from the string, using the delimiter character to split the string into a sequence of tokens.
Example
SPLIT ('a-b-c-d', '-', 2) = "b"
Note
Some data sources have limitations when splitting strings. When the token number exceeds the number of split substrings, some databases return null, while others return the last string.
Adds the specified number [interval] of date units [dateUnit] to the [date] parameter. DateUnit supports: 'year', 'quarter', 'month', 'day', 'week', 'hour', 'minute', 'second'
Example
DATEADD(`birth_day`, 1, 'year') Adds 1 year to the birth date
Adds the specified number [interval] of years to the [date] parameter. If interval is negative, subtracts. Equivalent to DATEADD(birth_day, 1, 'year').
Example
ADDYEAR(`dt`, -1) Gets the date corresponding to 1 year before `dt`
Adds the specified number [interval] of months to the [date] parameter. If interval is negative, subtracts. Equivalent to DATEADD(birth_day, 1, 'month').
Example
ADDMONTH(`dt`, -1) Gets the date corresponding to 1 month before `dt`
Adds the specified number [interval] of weeks to the [date] parameter. If interval is negative, subtracts. Equivalent to DATEADD(birth_day, 1, 'week').
Example
ADDWEEK(`dt`, -1) Gets the date corresponding to 1 week before `dt`
Adds the specified number [interval] of hours to the [date] parameter. If interval is negative, subtracts. Equivalent to DATEADD(birth_day, 1, 'hour').
Example
ADDHOUR(`dt`, -1) Gets the date time corresponding to 1 hour before `dt`
Returns the difference between [startDate] and [endDate] expressed in units of [dateUnit]. DateUnit supports: 'year', 'quarter', 'month', 'day', 'week', 'hour', 'minute', 'second'
Example
DATEDIFF('year', `BIRTH_DATE`, CURRENT_DATE()) -- Output: age
Gets the name of the date part DATEPART(datePart, date). DatePart supports: 'year', 'month', 'dayofyear', 'dayofmonth', 'dayofweek', 'isodayofweek', 'weekofyear'
Example
DATEPART('year', `BIRTH_DATE`) -- Output: 1985
Note
It is more recommended to use specific date part functions like dayOfMonth(date), YEAR(date) to get date parts.
Returns the day of the week of the given [date] as an integer.
Example
DAYOFWEEK(`BIRTH_DATE`) -- Output: 1-7
Note
This function does not guarantee consistency due to database differences; the week count returned may start with Monday or Sunday as the first day. The related function ISODAYOFWEEK returns the week starting with Monday as the first day.
Date part List
yyyy: Year(2022)
yy: Short Year(22)
MM: Month(01-12)
dd: Day of Month(01-31)
HH: Hour (00..23)
ww: week of year(01..53), Monday is first day of week
Converts a date to a date string in the specified format.
Date part List
yyyy: Year(2022)
yy: Short Year(22)
MM: Month(01-12)
dd: Day of Month(01-31)
HH: Hour (00..23)
ww: week of year(01..53), Monday is first day of week