Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Window operators

The window operators this instance offers: what each is for, the questions it answers and when to reach for something else. Operators are sorted by name; each name links to its detail block below.

OperatorIn plain wordsAnswers questions likeLevelInstead, when…
WIN_DELTAAdds the change from a set number of rows earlier in the field’s own units, such as orders this month minus last month.By how many orders did each month go up or down on the month before?basicWIN_PCT_CHANGE when you want the change relative to the earlier value.
OVERLAY_DELTA_VS_PRIOR when you want each period’s change from the one before as a decoration on the result.
WIN_DENSE_RANKRanks rows within each partition in the chosen order; rows with equal values share a rank and the next follows on: 1, 2, 2, 3.Which price tier is each product in, counting equal prices as one tier?basicWIN_RANK when the rank should skip past equal values, as in sports standings.
WIN_ROW_NUMBER when every row needs its own number even when values are equal.
WIN_DISTRIBUTED_LAGAdds a carry-over (distributed lag, or adstock) series: each row sums current and earlier values, weighted by a decaying curve.How much of last month’s advertising is still working this week, as a column a model can use?intermediateWIN_EWMA when you want a smoothed level of the series itself, seeded from its first value.
WIN_MOVING_AVG when you want every row in a fixed window to count equally.
WIN_EWMAAdds a smoothed series: each new value counts for a fixed share w, the previous smoothed level for 1 - w, so older values fade.What is the underlying level of daily demand once day-to-day noise is damped?intermediateWIN_MOVING_AVG when you want every row in a fixed window to count equally.
OVERLAY_ZSCORE_VS_ROLLING when you want how unusual each period is against its recent run.
WIN_FORMULAAdds a number to every result row computed from that row’s columns and earlier windows, such as one share divided by another.What is each decile’s share of defaults minus its share of loans?intermediateWIN_LIFT when you want one column’s share over another’s share, the lift of a gains table.
ATTR_FORMULA when you want a value computed per record before grouping.
WIN_LAGAdds to every row the value of a field from a set number of rows earlier in the ordered series, such as last month’s sales.What were each store’s sales in the month before, on the same row as this month’s?basicWIN_DELTA when you want the difference from the earlier row, not its value.
WIN_LEAD when you want the value from a later row.
OVERLAY_INDEX_VS_PRIOR when you want each period against the period before it as an index.
WIN_LEADAdds to every row the value of a field from a set number of rows later in the ordered series, such as next month’s sales.What did each customer spend in the following month, beside this month’s spend?basicWIN_LAG when you want the value from an earlier row.
WIN_DELTA when you want how much the field changed between rows.
WIN_LIFTAdds how many times its share of one column a row holds of another, such as a decile’s share of defaults over its share of loans.How many times the average default rate do the riskiest deciles of a score capture?intermediateWIN_SHARE when you want one column’s share of its own total.
AGG_AUC when you want how well a score ranks one outcome above the other as one number.
OVERLAY_INDEX_VS_TOTAL when you want each group against the overall figure as an index.
WIN_MOVING_AVGAdds a moving average: the mean of a field over a fixed number of neighbouring rows, smoothing short-term swings in a series.What is the trailing seven-day average of daily orders?basicWIN_RUNNING_AVG when you want the average of everything up to this row.
WIN_EWMA when you want recent rows to count more than older ones.
OVERLAY_INDEX_VS_ROLLING_MEAN when you want each period against its recent average.
WIN_PCT_CHANGEAdds the change from a set number of rows earlier as a fraction of the earlier value: 0.05 is a 5% rise on last month.By what percentage did each month’s revenue grow on the month before?basicWIN_DELTA when the earlier value can be 0, near 0 or negative, or the field is itself a percentage.
OVERLAY_YOY when you want the same year-ago period as the comparison.
OVERLAY_INDEX_VS_PRIOR when you want each period as an index value against the one before.
WIN_RANKRanks rows within each partition in the chosen order; rows with equal values share a rank and the next skips: 1, 2, 2, 4.Where does each store rank on revenue within its region?basicWIN_DENSE_RANK when equal values should share a rank with no gap after them.
WIN_ROW_NUMBER when every row needs its own number even when values are equal.
ATTR_PERCENTILE when you want each record’s position as a percentage of all records.
WIN_ROW_NUMBERNumbers the rows 1, 2, 3 within each partition in the chosen order, for picking the first few rows of every group.Which are the top three products by revenue in each region?basicWIN_RANK when rows with equal values should share a position.
WIN_DENSE_RANK when you want ranks without gaps after equal values.
WIN_RUNNING_AVGAdds the average of a field over the rows so far in the ordered series, such as the average order value to date.What is the average monthly revenue for the year so far, at each month?basicWIN_MOVING_AVG when you want the average of a fixed number of recent rows.
AGG_AVERAGE when you want one average per group, not a running one.
WIN_RUNNING_SUMAdds a running total of a field down the ordered rows, such as revenue for the year to date.What is the year-to-date revenue at the end of each month?basicAGG_SUM when you want one total per group, not a running one.
WIN_RUNNING_AVG when you want the running average instead of the total.
WIN_SHAREAdds each row’s share of its partition’s total, or the running share down the ordered rows, such as the share of defaults caught so far.What share of all defaults falls in each score decile, and how much do the riskiest deciles capture together?basicWIN_RUNNING_SUM when you want the running total itself, not as a share.
WIN_LIFT when you want how much richer in the field a row is than its share of another column.
OVERLAY_SHARE_OF_TOTAL when you want each group’s share as a decoration on the result.

Operators

WIN_DELTA

Adds the change from a set number of rows earlier in the field’s own units, such as orders this month minus last month.

Level: basic

Questions it answers:

  • By how many orders did each month go up or down on the month before?
  • How many points did satisfaction move between waves?

Use cases by domain:

  • survey: Change in a brand’s awareness, in percentage points, from the last wave.
  • ops: Week-on-week change in tickets opened per queue.
  • science: Change in each subject’s reading since the previous visit.

Assumptions:

  • Runs on the result’s rows after grouping: one row per group, or one per filtered record when nothing is grouped.
  • Computed separately within each partition_by slice, in order_by order; with no partition the whole result is one series.
  • Steps are rows, not calendar periods: a period with no row is skipped, not filled, so one row back can span two periods.
  • The first rows of each partition, and any row where either value is missing, read null; an earlier value of 0 is a real change.

Use something else:

  • WIN_PCT_CHANGE when you want the change relative to the earlier value.
  • OVERLAY_DELTA_VS_PRIOR when you want each period’s change from the one before as a decoration on the result.

Glossary: percentage-point, baseline, missing-value

Skill: op-win-delta

WIN_DENSE_RANK

Ranks rows within each partition in the chosen order; rows with equal values share a rank and the next follows on: 1, 2, 2, 3.

Level: basic

Questions it answers:

  • Which price tier is each product in, counting equal prices as one tier?
  • With scores ordered desc, how many distinct scores sit above each respondent’s (the dense rank minus 1)?

Use cases by domain:

  • survey: Position of each rating level from the top, with equal ratings sharing one position.
  • ops: Tier number of each store by sales, equal sales sharing a tier.
  • science: Order of distinct dose levels within each trial arm.

Assumptions:

  • Runs on the result’s rows after grouping: one row per group, or one per filtered record when nothing is grouped.
  • Rank 1 goes to the first row in order_by order: the smallest value, or the largest when the key is desc.
  • Rows tie only when equal on every order_by key.
  • Rows with a missing order_by value sort last, in both directions.

Use something else:

  • WIN_RANK when the rank should skip past equal values, as in sports standings.
  • WIN_ROW_NUMBER when every row needs its own number even when values are equal.

Glossary: rank, ties

Skill: op-win-dense-rank

WIN_DISTRIBUTED_LAG

Adds a carry-over (distributed lag, or adstock) series: each row sums current and earlier values, weighted by a decaying curve.

Level: intermediate

Also known as: distributed lag, adstock, carry-over effect, lagged media effect

Questions it answers:

  • How much of last month’s advertising is still working this week, as a column a model can use?
  • What does each channel’s spend look like once its lingering effect is spread over later weeks?

Use cases by domain:

  • survey: Carried-over campaign exposure beside each tracking wave, for a model of brand awareness.
  • ops: Weekly media spend per channel turned into its lingering effect before a sales model.
  • science: A dose given over time turned into its fading cumulative exposure at each reading.

Assumptions:

  • Runs on the result’s rows after grouping: one row per group, or one per filtered record when nothing is grouped.
  • Computed separately within each partition_by slice, in order_by order; with no partition the whole result is one series.
  • Steps are rows, not calendar periods: a period with no row is skipped, not filled, so one row back can span two periods.
  • The curve is set by params.kernel: geometric (each lag back keeps a fixed share, params.decay), delayed (the same carry-over peaking params.peak rows later) or a Weibull curve (params.shape and params.scale), cut off after params.max_lag rows.
  • By default the weights sum to 1, so the column stays on the field’s scale; with params.normalize false the raw weights apply.
  • A missing value reads null and adds nothing to later rows; rows before the partition starts add nothing either, so the carry-over starts from zero.

Use something else:

  • WIN_EWMA when you want a smoothed level of the series itself, seeded from its first value.
  • WIN_MOVING_AVG when you want every row in a fixed window to count equally.

Glossary: missing-value

Skill: op-win-distributed-lag

WIN_EWMA

Adds a smoothed series: each new value counts for a fixed share w, the previous smoothed level for 1 - w, so older values fade.

Level: intermediate

Also known as: exponential smoothing, exponentially weighted moving average

Questions it answers:

  • What is the underlying level of daily demand once day-to-day noise is damped?
  • Is the smoothed defect rate drifting up over recent batches?

Use cases by domain:

  • survey: Smoothed tracking score that reacts to recent waves more than old ones.
  • ops: Smoothed daily demand for a stock forecast.
  • science: Smoothed sensor reading that follows slow drift and damps spikes.

Assumptions:

  • Runs on the result’s rows after grouping: one row per group, or one per filtered record when nothing is grouped.
  • Computed separately within each partition_by slice, in order_by order; with no partition the whole result is one series.
  • Steps are rows, not calendar periods: a period with no row is skipped, not filled, so one row back can span two periods.
  • The share w is set directly (params.alpha, above 0 and at most 1) or as its complement (params.decay = 1 - w, at least 0 and below 1); it seeds from the partition’s first value present.
  • A missing value reads null and the smoothing carries over it.

Use something else:

Glossary: mean, missing-value

Skill: op-win-ewma

WIN_FORMULA

Adds a number to every result row computed from that row’s columns and earlier windows, such as one share divided by another.

Level: intermediate

Questions it answers:

  • What is each decile’s share of defaults minus its share of loans?
  • What is each region’s revenue per order, next to its share of the total?

Use cases by domain:

  • survey: The gap between a segment’s share of complaints and its share of respondents.
  • ops: Revenue per order per store, from the summed revenue and the order count.
  • science: Cases per thousand at risk per site, from the case and population columns.

Assumptions:

  • Runs on the result’s rows after grouping: one row per group, or one per filtered record when nothing is grouped.
  • It reads only its own row: the group keys, the aggregation labels and the outputs of the windows listed before it, never a later window.
  • A missing value in any column it reads makes the row’s result missing; a division by zero reads missing too, never infinity.

Use something else:

  • WIN_LIFT when you want one column’s share over another’s share, the lift of a gains table.
  • ATTR_FORMULA when you want a value computed per record before grouping.

Glossary: missing-value

Skill: op-win-formula

WIN_LAG

Adds to every row the value of a field from a set number of rows earlier in the ordered series, such as last month’s sales.

Level: basic

Questions it answers:

  • What were each store’s sales in the month before, on the same row as this month’s?
  • What score did each respondent give in the previous wave?

Use cases by domain:

  • survey: Previous wave’s score beside the current one for each tracked brand.
  • ops: Last week’s order count beside this week’s, per warehouse.
  • science: The previous reading beside each measurement in a time series.

Assumptions:

  • Runs on the result’s rows after grouping: one row per group, or one per filtered record when nothing is grouped.
  • Computed separately within each partition_by slice, in order_by order; with no partition the whole result is one series.
  • Steps are rows, not calendar periods: a period with no row is skipped, not filled, so one row back can span two periods.
  • The first rows of each partition have no earlier row and read null, or the default when one is set.

Use something else:

  • WIN_DELTA when you want the difference from the earlier row, not its value.
  • WIN_LEAD when you want the value from a later row.
  • OVERLAY_INDEX_VS_PRIOR when you want each period against the period before it as an index.

Glossary: missing-value

Skill: op-win-lag

WIN_LEAD

Adds to every row the value of a field from a set number of rows later in the ordered series, such as next month’s sales.

Level: basic

Questions it answers:

  • What did each customer spend in the following month, beside this month’s spend?
  • What reading came next after each measurement?

Use cases by domain:

  • survey: Next wave’s answer beside the current one, to see who changed their mind.
  • ops: Next shipment date beside each order, to measure the wait.
  • science: The following reading beside each measurement in a series.

Assumptions:

  • Runs on the result’s rows after grouping: one row per group, or one per filtered record when nothing is grouped.
  • Computed separately within each partition_by slice, in order_by order; with no partition the whole result is one series.
  • Steps are rows, not calendar periods: a period with no row is skipped, not filled, so one row back can span two periods.
  • The last rows of each partition have no later row and read null, or the default when one is set.

Use something else:

  • WIN_LAG when you want the value from an earlier row.
  • WIN_DELTA when you want how much the field changed between rows.

Glossary: missing-value

Skill: op-win-lead

WIN_LIFT

Adds how many times its share of one column a row holds of another, such as a decile’s share of defaults over its share of loans.

Level: intermediate

Questions it answers:

  • How many times the average default rate do the riskiest deciles of a score capture?
  • Which stores sell more than their share of floor space would predict?

Use cases by domain:

  • survey: A segment’s share of complaints over its share of respondents.
  • ops: Cumulative lift of a churn score: share of churners caught over share of customers contacted.
  • science: A site’s share of cases over its share of the population at risk.

Assumptions:

  • Runs on the result’s rows after grouping: one row per group, or one per filtered record when nothing is grouped.
  • Computed separately within each partition_by slice, in order_by order; with no partition the whole result is one series.
  • It is the field’s share divided by the base’s share, each total over its own values present; with cumulative both are running shares in order_by order.
  • A missing value on either side, or a zero base share, reads null.

Use something else:

  • WIN_SHARE when you want one column’s share of its own total.
  • AGG_AUC when you want how well a score ranks one outcome above the other as one number.
  • OVERLAY_INDEX_VS_TOTAL when you want each group against the overall figure as an index.

Glossary: baseline, missing-value

Skill: op-win-lift

WIN_MOVING_AVG

Adds a moving average: the mean of a field over a fixed number of neighbouring rows, smoothing short-term swings in a series.

Level: basic

Also known as: rolling mean, simple moving average

Questions it answers:

  • What is the trailing seven-day average of daily orders?
  • Is the trend in weekly sign-ups rising once the week-to-week noise is smoothed out?

Use cases by domain:

  • survey: Three-wave average of a tracking score to steady small-sample waves.
  • ops: Seven-day average of daily orders.
  • science: Smooth a noisy sensor series before reading its trend.

Assumptions:

  • Runs on the result’s rows after grouping: one row per group, or one per filtered record when nothing is grouped.
  • Computed separately within each partition_by slice, in order_by order; with no partition the whole result is one series.
  • Steps are rows, not calendar periods: a period with no row is skipped, not filled, so one row back can span two periods.
  • The frame must be bounded on both sides; near a partition’s edges it holds fewer rows.
  • Missing values are skipped, so the figure covers the values present; a frame with no value reads null.

Use something else:

Glossary: rolling-mean, mean, missing-value

Skill: op-win-moving-avg

WIN_PCT_CHANGE

Adds the change from a set number of rows earlier as a fraction of the earlier value: 0.05 is a 5% rise on last month.

Level: basic

Questions it answers:

  • By what percentage did each month’s revenue grow on the month before?
  • Which regions grew fastest relative to their own previous quarter?

Use cases by domain:

  • survey: Relative change in sample size per wave.
  • ops: Month-on-month growth in revenue per product line.
  • science: Relative change in a measurement since the previous visit.

Assumptions:

  • Runs on the result’s rows after grouping: one row per group, or one per filtered record when nothing is grouped.
  • Computed separately within each partition_by slice, in order_by order; with no partition the whole result is one series.
  • Steps are rows, not calendar periods: a period with no row is skipped, not filled, so one row back can span two periods.
  • The first rows of each partition, any row where either value is missing, and any row whose earlier value is 0 read null.

Use something else:

  • WIN_DELTA when the earlier value can be 0, near 0 or negative, or the field is itself a percentage.
  • OVERLAY_YOY when you want the same year-ago period as the comparison.
  • OVERLAY_INDEX_VS_PRIOR when you want each period as an index value against the one before.

Glossary: baseline, percentage-point, index-value, missing-value

Skill: op-win-pct-change

WIN_RANK

Ranks rows within each partition in the chosen order; rows with equal values share a rank and the next skips: 1, 2, 2, 4.

Level: basic

Questions it answers:

  • Where does each store rank on revenue within its region?
  • Which product came first in each month by units sold?

Use cases by domain:

  • survey: Rank of each brand on preference within every market.
  • ops: Rank of each warehouse on on-time delivery within its region.
  • science: Rank of each site’s yield within each season.

Assumptions:

  • Runs on the result’s rows after grouping: one row per group, or one per filtered record when nothing is grouped.
  • Rank 1 goes to the first row in order_by order: the smallest value, or the largest when the key is desc.
  • Rows tie only when equal on every order_by key.
  • Rows with a missing order_by value sort last, in both directions.

Use something else:

  • WIN_DENSE_RANK when equal values should share a rank with no gap after them.
  • WIN_ROW_NUMBER when every row needs its own number even when values are equal.
  • ATTR_PERCENTILE when you want each record’s position as a percentage of all records.

Glossary: rank, ties

Skill: op-win-rank

WIN_ROW_NUMBER

Numbers the rows 1, 2, 3 within each partition in the chosen order, for picking the first few rows of every group.

Level: basic

Questions it answers:

  • Which are the top three products by revenue in each region?
  • What is each customer’s first order, by date?

Use cases by domain:

  • survey: Number each respondent’s answers in the order given.
  • ops: Top five stores by sales within each region.
  • harness: Number result rows to page through them in a fixed order.

Assumptions:

  • Runs on the result’s rows after grouping: one row per group, or one per filtered record when nothing is grouped.
  • Row 1 goes to the first row in order_by order: the smallest value, or the largest when the key is desc, so set desc for a top-N.
  • Rows with equal order_by values still get different numbers, in an arbitrary but repeatable order.
  • Rows with a missing order_by value sort last, in both directions.

Use something else:

  • WIN_RANK when rows with equal values should share a position.
  • WIN_DENSE_RANK when you want ranks without gaps after equal values.

Glossary: rank, ties

Skill: op-win-row-number

WIN_RUNNING_AVG

Adds the average of a field over the rows so far in the ordered series, such as the average order value to date.

Level: basic

Questions it answers:

  • What is the average monthly revenue for the year so far, at each month?
  • How has the cumulative average score settled as more waves came in?

Use cases by domain:

  • survey: Average satisfaction to date across fieldwork days.
  • ops: Average handling time to date, month by month.
  • science: Cumulative mean of repeated measurements as each one is added.

Assumptions:

  • Runs on the result’s rows after grouping: one row per group, or one per filtered record when nothing is grouped.
  • Computed separately within each partition_by slice, in order_by order; with no partition the whole result is one series.
  • The frame sets which rows count: no preceding bound with following 0 gives the average from the start to this row.
  • Missing values are skipped, so the figure covers the values present; a frame with no value reads null.

Use something else:

  • WIN_MOVING_AVG when you want the average of a fixed number of recent rows.
  • AGG_AVERAGE when you want one average per group, not a running one.

Glossary: mean, missing-value

Skill: op-win-running-avg

WIN_RUNNING_SUM

Adds a running total of a field down the ordered rows, such as revenue for the year to date.

Level: basic

Questions it answers:

  • What is the year-to-date revenue at the end of each month?
  • How many responses had come in by each day of fieldwork?

Use cases by domain:

  • survey: Cumulative completes per day against the fieldwork quota.
  • ops: Year-to-date sales per region, month by month.
  • science: Cumulative dose received by each visit.

Assumptions:

  • Runs on the result’s rows after grouping: one row per group, or one per filtered record when nothing is grouped.
  • Computed separately within each partition_by slice, in order_by order; with no partition the whole result is one series.
  • The frame sets which rows add up: no preceding bound with following 0 gives the total from the start to this row.
  • Missing values are skipped, so the figure covers the values present; a frame with no value reads null.

Use something else:

  • AGG_SUM when you want one total per group, not a running one.
  • WIN_RUNNING_AVG when you want the running average instead of the total.

Glossary: missing-value

Skill: op-win-running-sum

WIN_SHARE

Adds each row’s share of its partition’s total, or the running share down the ordered rows, such as the share of defaults caught so far.

Level: basic

Questions it answers:

  • What share of all defaults falls in each score decile, and how much do the riskiest deciles capture together?
  • What fraction of each region’s revenue does each store bring in?

Use cases by domain:

  • survey: Each answer’s share of the responses within its question.
  • ops: Cumulative share of tickets handled by the busiest agents first.
  • science: Share of the total dose each visit delivered, per subject.

Assumptions:

  • Runs on the result’s rows after grouping: one row per group, or one per filtered record when nothing is grouped.
  • Computed separately within each partition_by slice, in order_by order; with no partition the whole result is one series.
  • Without cumulative no order is needed; with it the running total runs from the partition’s first row in order_by order to this one.
  • Missing values add nothing to the total; a zero total reads null.

Use something else:

  • WIN_RUNNING_SUM when you want the running total itself, not as a share.
  • WIN_LIFT when you want how much richer in the field a row is than its share of another column.
  • OVERLAY_SHARE_OF_TOTAL when you want each group’s share as a decoration on the result.

Glossary: missing-value

Skill: op-win-share