Skip to main content
On this page

Kater SQL Functions Field declarations

EXCEL

Writes a field's whole value as an Excel formula over the columns of the query's result.

Family: Field declarations

The compiler turns the formula into your warehouse’s SQL each time the query compiles.

Syntax

CUSTOM_CALCULATION('name', EXCEL('=formula')) AS "Label"

Arguments

ArgumentDescription
'=formula'The formula, in quotes, starting with =. It names columns by their field names, such as [@sum__quantity]. A quote inside the formula is written twice, and text inside it goes in double quotes.

Returns

The formula’s value for each row of the result.

Usage notes

  • Write it in a query or in the Sandbox, inside CUSTOM_CALCULATION. It is the field’s whole value, never part of a larger expression. Shared models have no formulas.
  • A formula reads only the columns of its own query. Add a field to the query before a formula reads it.
  • [@revenue] reads this row, and [@revenue:-1] the row above. [revenue] reads every row, and [#total revenue] the measure worked out over every row at once.
  • The row above follows the query’s ORDER BY. A formula that reads another row needs the query sorted.
  • The syntax is US Excel’s, with a comma between arguments. A value that Excel shows as an error, such as a division by zero, is empty.
  • Lookups such as VLOOKUP and XLOOKUP are not available. Bring the value into the query as a field.

Example

This Sandbox query reads the sample shop’s orders. The table shows the rows that Kater gave.

SELECT
orders.channel AS "Channel",
EVAL(orders.sum__quantity) AS "Units",
EVAL(orders.count_distinct__order_id) AS "Orders",
CUSTOM_CALCULATION('units_per_order', EXCEL('=ROUND([@sum__quantity]/[@count_distinct__order_id], 2)')) AS "Units per Order"
FROM kater_dataset.orders AS orders
GROUP BY
orders.channel
ORDER BY
1
ChannelUnitsOrdersUnits per Order
email2,3837783.06
paid2,2927623.01
store2,3307543.09
web2,1497063.04

See also