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
| Argument | Description |
|---|---|
measure | The measure to work out. |
dimension | The dimensions to add to the grouping, such as orders.customer_id. Give one or more. |
summarize | How 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_PERwritesCALCULATE_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 ordersWHERE KATER__IS_NOT_NULL(orders.region)GROUP BY orders.regionORDER BY 1| Region | Units per Customer, This Region | Units per Customer, All Regions |
|---|---|---|
| AMER | 9.39 | 37.62 |
| APAC | 9.16 | 37.85 |
| EMEA | 10.48 | 37.70 |
| LATAM | 9.91 | 37.87 |
See also
- Change a measure’s context
- Other functions in Calculation context:
TOTAL,SHARE_OF_TOTAL,VALUE_FOR,CALCULATE_ACROSS,CALCULATE_BY,RATIO,RUNNING_TOTAL,MOVING_AVERAGE,CUMULATIVE_SHARE,BY,ACROSS,IGNORE_FILTERS