Skip to main content
On this page

Kater SQL Functions Calculation context

CALCULATE_PER

Adds the dimensions you name to the query's grouping, works the measure out there, then combines those values for each row.

Family: Calculation context

Use it for an average per customer in each region.

Syntax

CALCULATE_PER(measure, dimension, ..., summarize => 'avg' | 'sum' | 'min' | 'max')

Arguments

ArgumentDescription
measureThe measure to work out.
dimensionThe dimensions to add to the grouping, such as orders.customer_id. Give one or more.
summarizeHow to combine the values for each row: 'avg', 'sum', 'min' or 'max'.

Returns

The combined value on each row of the result.

Usage notes

  • The query’s own grouping applies, and the dimensions are added to it. Compare CALCULATE_BY, which uses only the dimensions it names.
  • The call reads one source, by simple field names. Bring a joined or computed field into a source query first.
  • KATER__CALCULATE_PER writes CALCULATE_PER. A field written with one is worked out at the grouping of the query that holds it, so another query cannot read it.

Example

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

SELECT
orders.region AS "Region",
MEASURE('units_per_customer_in_region', CALCULATE_PER(orders.sum__quantity, orders.customer_id, summarize => 'avg')) AS "Units per Customer, This Region",
MEASURE('units_per_customer_all_regions', CALCULATE_BY(orders.sum__quantity, orders.customer_id, summarize => 'avg')) AS "Units per Customer, All Regions"
FROM kater_dataset.orders AS orders
WHERE
KATER__IS_NOT_NULL(orders.region)
GROUP BY
orders.region
ORDER BY
1
RegionUnits per Customer, This RegionUnits per Customer, All Regions
AMER9.3937.62
APAC9.1637.85
EMEA10.4837.70
LATAM9.9137.87

See also