Calculations in visuals and low-code cells
Leverage the full power of SQL without the boilerplate
Last updated
Leverage the full power of SQL without the boilerplate
Last updated
Calculations allow you to perform more complex analyses than possible with the built-in functions available in the visual/low-code UI, without having to write a full SQL statement.
Calculations in Count are SQL expressions, written in a syntax that is heavily influenced by DuckDB (itself similar to PostgreSQL). They can be used with all databases supported by Count and — depending on the database — are parsed and transliterated to the appropriate dialect as required.
You can apply calculations to selects, filters and join constraints in visuals and low-code cells. In order to add or edit a calculation, either:
Double click on a drop target or select to get an inline-editor
Click on a select, filter or join constraint and select Edit calculation... to bring up the calculation modal
Once you're happy with your calculation, submit it using Shift + Enter.
Calculations in Count support the following expression types:
Literal (0
, 0.123
, 'abc'
, NULL
, TRUE
, FALSE
)
Identifier (Sales
, Orders.Sales
)
Function (sum(Sales)
)
Between (Sales BETWEEN 10 and 20
)
In (Region IN ('East', 'West')
)
Case (CASE WHEN Region = 'East' THEN 0 ELSE 1 END
)
Cast (CAST(Sales AS VARCHAR)
)
Binary (2 + 2
, A OR B
)
Unary (-2
, NOT TRUE
)
There's no explicit limit to the length of calculations.
The precedence of operators (the order in which expressions are evaluated) follows that of PostgreSQL. Parentheses can be used to override this.
Precedence | Symbols | Name | Example |
---|---|---|---|
1 |
| Unary minus |
|
2 |
| Exponentiation |
|
3 |
| Multiplication, Divison, Modulo |
|
4 |
| Addition, Subtraction |
|
5 |
| Comparison |
|
6 |
| Is |
|
7 |
| Logical negation |
|
8 |
| Logical AND |
|
9 |
| Logical OR |
|
The following data types can be used in CAST
expressions:
VARCHAR ('abc'
)
DOUBLE (1.23
)
INTEGER (123
)
DATE (DATE '2024-01-01'
)
TIMESTAMP (TIMESTAMP '2024-01-01T00:00:00'
)
TIMESTAMPTZ (TIMESTAMPTZ '2024-01-01T00:00:00Z'
)
BOOLEAN (TRUE
)
Count supports the following functions (note that some functions may be unavailable on some databases):
Function | Description | Example | Result |
---|---|---|---|
| Concatenate any number of strings together. |
|
|
| Returns true if string contains substring. |
|
|
| Returns true if string ends with substring. |
|
|
| Extracts the left-most characters from the string. |
|
|
| Number of characters in the string. |
|
|
| Converts string to lower case. |
|
|
| Pads the string with the character from the left until it has count characters. |
|
|
| Removes leading spaces from the string. |
|
|
| Returns the original string repeated count times. |
|
|
| Replaces any occurrences of the source with target in string. |
|
|
| Reverses the string. |
|
|
| Extracts the right-most count characters from the string. |
|
|
| Returns the original string with trailing spaces added up to length characters. |
|
|
| Removes trailing spaces from the string. |
|
|
| Split the string along the delimiter and return the data at the (1-based) index of the list. If the index is outside the bounds of the list, returns an empty string. |
|
|
| Returns true string begins with substring. |
|
|
| Returns the position of the first occurrence of substring. Returns 0 if the substring is not found. |
|
|
| Extract a substring of length characters from character position. |
|
|
| Removes whitespace from both sides of the string. |
|
|
| Converts the string to upper case. |
|
|
Function | Description | Example | Result |
---|---|---|---|
| Computes the absolute value of x. |
|
|
| Computes the arc cosine of x. |
|
|
| Computes the arc sine of x. |
|
|
| Computes the arc tangent of x. |
|
|
| Computes the arc tangent of x and y. |
|
|
| Computes the smallest integer value that is greater than or equal to x. |
|
|
| Computes the cosine of x. |
|
|
| Computes the degrees of x. |
|
|
| Returns the result of integer division of x by y. |
|
|
| Computes the exponential of x. |
|
|
| Computes the largest integer value that is less than or equal to x. |
|
|
| Computes the natural logarithm of x. |
|
|
| Computes the logarithm of x to base y. |
|
|
| Computes the logarithm base 10 of x. |
|
|
| Computes the modulo of x and y. |
|
|
| Returns the value of π. |
|
|
| Computes the value of x raised to the power of y. |
|
|
| Computes the radians of x. |
|
|
| Returns a random real number between 0 and 1. |
|
|
| Rounds x to precision. |
|
|
| Computes the sign of x. |
|
|
| Computes the sine of x. |
|
|
| Computes the square-root of x. |
|
|
| Computes the tangent of x. |
|
|
| Truncates x to precision. |
|
|
Function | Description | Example | Result |
---|---|---|---|
| Adds an interval to a date. |
|
|
| Returns the number of complete partitions between start_date and end_date. |
|
|
| Truncates a date to specified precision. |
|
|
| Returns the current timestamp. |
| Various |
| Returns the current date. |
| Various |
Intervals take the form of:
INTERVAL x <date_part>
INTERVAL 'x <date_part> y <date_part> z <date_part> ...'
and the following are valid date parts:
YEAR
, YR
, Y
, YEAR
, YRS
QUARTER
, QUARTERS
MONTH
, MON
, MONTHS
, MONS
WEEK
DAY
, DAYS
, D
HOUR
, HR
, HOURS
, HRS
, H
MINUTES
, MIN
, MINUTES
, MINS
, M
SECOND
, SEC
, SECONDS
, SECS
, S
MILLISECONDS
, MILLISECOND
, MS
, MSEC
, MSECS
, MSECOND
, MSECONDS
MICROSECONDS
, MICROSECONDS
, US
, USEC
, USECOND
,
Function | Description | Example | Result |
---|---|---|---|
| Returns the integer day of the month. |
|
|
| Returns the integer day of the week (Sunday = 0, Saturday = 6). |
|
|
| Returns the integer day of the year. |
|
|
| Returns the integer month of year (January = 1). |
|
|
| Returns the integer quarter of year (January-March = 1). |
|
|
| Returns the week number of year (starting with 1). |
|
|
| Returns the year. |
|
|
Function | Description | Example |
---|---|---|
| Computes the average (mean) of the values within x. NULL values are ignored. |
|
| Calculates the Pearson correlation coefficient of non-null pairs in the specified columns. |
|
| Counts the number of non-null values in the specified column. If no column is specified, counts the number of rows in the table. |
|
| Calculates the population covariance of non-null pairs in the specified columns. |
|
| Calculates the sample covariance of non-null pairs in the specified columns. |
|
| Returns the maximum value of non-null expressions. |
|
| Computes the median of the values within x. NULL values are ignored. |
|
| Returns the minimum value of non-null expressions. |
|
| Returns the most frequent value of non-null expressions. |
|
| Calculates the value of a continuous variable x at a specified percentile y, where y is between 0 and 1. The result is interpolated between adjacent values of x. |
|
| Calculates the value of a discrete variable x at a specified percentile y, where y is between 0 and 1. The result is the value of x at the yth percentile. |
|
| Computes the standard deviation of the values within x. NULL values are ignored. |
|
| Computes the standard deviation of the values within x. NULL values are ignored. |
|
| Computes the sum of the values within x. NULL values are ignored. |
|
| Computes the variance of the values within x. NULL values are ignored. |
|
| Computes the variance of the values within x. NULL values are ignored. |
|
Function | Description | Example |
---|---|---|
| Evaluates the cumualative distribution: the number of partition rows preceding the current row / total partition rows. |
|
| Calculates the rank of the current row without gaps. |
|
| Evaluates expr for the first row in the window frame. |
|
| Returns the value of expr evaluated at the row that is offset rows before the current row within the window frame. If there is no such row, instead return default. If offset is omitted, it defaults to 1. If default is omitted, it defaults to NULL. |
|
| Evaluates expr for the last row in the window frame. |
|
| Returns the value of expr evaluated at the row that is offset rows after the current row within the window frame. If there is no such row, instead return default. If offset is omitted, it defaults to 1. If default is omitted, it defaults to NULL. |
|
| Evaluates expr at the nth row of the window frame. |
|
| Distributes the rows in an ordered partition into a specified number of groups, ranging from 1 to num_buckets. |
|
| Evaluates the relative rank of the current row: (rank - 1) / (total partition rows - 1). |
|
| Calculates the rank of the current row with gaps. Rows with equal values receive the same rank value. |
|
| Returns the number of the current row within the partition, 1-indexed. |
|
Function | Description | Example | Result |
---|---|---|---|
| Returns the value of the first non-null expression. All expressions must be of the same type. |
|
|
| Returns the greatest value of the list of expressions. All expressions must be of the same type. |
|
|
| If x is true, returns y, Otherwise, returns z. |
|
|
| If x is null, returns y. Otherwise, returns x. |
|
|
| Returns the least value of the list of expressions. All expressions must be of the same type. |
|
|
| Returns null if x = y, otherwise returns x. |
|
|