Kater SQL Functions Calculation context
SHARE_OF_TOTAL
Divides a measure on each row by its total at a broader grouping.
Family: Calculation context
Name the grouping with BY or ACROSS.
Syntax
SHARE_OF_TOTAL(measure, BY(dimension, ...) | ACROSS(dimension, ...) [, SET(...)] [, IGNORE_FILTERS(...)])Arguments
| Argument | Description |
|---|---|
measure | The measure to divide. |
BY(...) or ACROSS(...) | The grouping of the total. BY() divides by the grand total. |
SET(...), IGNORE_FILTERS(...) | Optional. More changes to the total’s context. They change only the total, not the row’s own value. |
Returns
A fraction, such as 0.25 for 25 percent. It is null when the total is zero.
Usage notes
- The total is worked out again, as
TOTALworks it out. - Format it as a percent with a
/* @format percent(1) */comment after the column. KATER__SHARE_OF_TOTALis a window. It divides by the total of the rows in its partition, not by a measure worked out again.
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", SHARE_OF_TOTAL(orders.sum__quantity, BY()) AS "Share of Units" /* @format percent(1) */FROM kater_dataset.orders AS ordersGROUP BY orders.channelORDER BY 1| Channel | Units | Share of Units |
|---|---|---|
| 2,383 | 26.0% | |
| paid | 2,292 | 25.0% |
| store | 2,330 | 25.5% |
| web | 2,149 | 23.5% |
See also
- Change a measure’s context
- Other functions in Calculation context:
TOTAL,VALUE_FOR,CALCULATE_ACROSS,CALCULATE_BY,CALCULATE_PER,RATIO,RUNNING_TOTAL,MOVING_AVERAGE,CUMULATIVE_SHARE,BY,ACROSS,IGNORE_FILTERS