Shape · Time series
Shift / Lag
Copies a value from N rows earlier or later onto each row, so you can compare a row with the one before it. For example, last month's sales next to this month's.
Also known as: Lag, lead, previous row
How it works
Group by restarts the shift per group. Order by is required: it is the row order the shift walks along. Each shift row names a column, a number of periods (1 by default), an optional fill and an output column. A positive number copies the value from that many rows earlier, a negative one from rows later. Rows with no earlier (or later) row get the fill value, or NULL when the fill is blank. The output is appended as column_lag_N or column_lead_N unless you name it.
Inputs and outputs
Inputs
data— Table
Outputs
data— Table
Settings
The Attributes panel of Shift / Lag, as in the app: the controls work, but nothing runs — there is no data behind it.
The same settings, as the Assistant writes them
{
shifts: [{ column: string, periods: number, fillValue?: string, outputColumn?: string }]
orderBy: [{ column, direction }]
groupBy?: string[]
}
Example
You have monthly sales per store and want last month next to this month. Order by month, group by store, and add a shift of sales by 1 period with output prev_sales.
| store | month | sales | prev_sales |
|---|---|---|---|
| A | 2024-01 | 120 | NULL |
| A | 2024-02 | 135 | 120 |
| A | 2024-03 | 128 | 135 |