# Take a value from another row

Shape / table

A variant of [Calculate across rows](https://meridian.online/operations/calculate-across-rows).

Bring a value from a row a fixed offset before or after the current one, or from the first, last or nth row of the window, often to compute a difference or percentage change.

Page: <https://meridian.online/operations/calculate-across-rows/value-from-another-row>

## Status

Runs as a SQL step.

arcform carries it as a `sql:` step today.

## The DuckDB step

take a value from another row: the previous, the next, the first, the last and the nth

It reads the `orders` table the proofs share. Create it first:

```sql
CREATE TABLE orders AS SELECT * FROM (VALUES
  (1,  'Alice Ng',      'north', DATE '2026-01-03', 120.00, 'widget', 'a|b',   '{"channel":"web","items":[1,2]}'),
  (2,  'alice ng',      'north', DATE '2026-01-05',  80.50, 'gadget', 'b',     '{"channel":"shop","items":[3]}'),
  (3,  'Bob  Okafor',   'south', DATE '2026-01-05', 300.00, 'widget', 'a|c',   '{"channel":"web","items":[4,5,6]}'),
  (4,  'Carla Díaz',    'east',  DATE '2026-01-09',  NULL,  'gizmo',  NULL,    '{"channel":"web","items":[]}'),
  (5,  'Bob Okafor',    'south', DATE '2026-01-12',  45.25, 'gadget', 'c',     '{"channel":"phone","items":[7]}'),
  (6,  'Dev Patel',     'west',  DATE '2026-01-12', 510.00, 'widget', 'a',     '{"channel":"web","items":[8,9]}'),
  (7,  'Carla Diaz',    'east',  DATE '2026-01-20',  99.99, 'gizmo',  'b|c',   '{"channel":"shop","items":[10]}'),
  (8,  'Dev Patel',     'west',  DATE '2026-02-02', 510.00, 'widget', 'a',     '{"channel":"web","items":[8,9]}'),
  (9,  'Eve Larsen',    NULL,    DATE '2026-02-03',  15.00, 'gadget', NULL,    '{"channel":"web","items":[11]}'),
  (10, 'Alice Ng',      'north', DATE '2026-02-10', 220.00, 'gizmo',  'a|b|c', '{"channel":"phone","items":[12,13]}')
) t(order_id, customer, region, order_date, amount, product, tags, meta);
```

Then run the statement:

```sql
CREATE TEMP TABLE result AS
SELECT product, order_id, amount,
       lag(amount) OVER w AS previous_amount,
       lead(amount) OVER w AS next_amount,
       round(amount / lag(amount) OVER w - 1, 4) AS change,
       first_value(amount) OVER whole AS first_amount,
       last_value(amount) OVER whole AS last_amount,
       nth_value(amount, 2) OVER whole AS second_amount
FROM orders
WHERE amount IS NOT NULL
WINDOW w AS (PARTITION BY product ORDER BY order_date, order_id),
       whole AS (PARTITION BY product ORDER BY order_date, order_id ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING);
SELECT * FROM result ORDER BY product, order_id;
SELECT CASE WHEN ((SELECT list(previous_amount ORDER BY order_id) FROM result WHERE product = 'widget') IS NOT DISTINCT FROM [NULL, 120.00, 300.00, 510.00] AND (SELECT list(next_amount ORDER BY order_id) FROM result WHERE product = 'widget') IS NOT DISTINCT FROM [300.00, 510.00, 510.00, NULL] AND (SELECT list(change ORDER BY order_id) FROM result WHERE product = 'widget') IS NOT DISTINCT FROM [NULL, 1.5, 0.7, 0.0] AND (SELECT list(change ORDER BY order_id) FROM result WHERE product = 'gadget') IS NOT DISTINCT FROM [NULL, -0.4379, -0.6685] AND (SELECT list(change ORDER BY order_id) FROM result WHERE product = 'gizmo') IS NOT DISTINCT FROM [NULL, 1.2002] AND (SELECT (first_amount, last_amount, second_amount) FROM result WHERE order_id = 6) = (120.00, 510.00, 300.00) AND (SELECT (first_amount, last_amount, second_amount) FROM result WHERE order_id = 5) = (80.50, 15.00, 45.25)) THEN 'as expected' ELSE error('the previous, next, first, last and second amounts or the change from the previous order were not the expected values per product') END AS verdict;
```

It needs nothing installed beyond DuckDB itself.

## What other tools call it

Meridian's classification; an independent blind rating agreed on 95.7% of 324 rows (variant, version 3).

- Alteryx Designer: [`Multi-Row Formula`](https://help.alteryx.com/current/en/designer/tools/preparation/multi-row-formula-tool.html) (read 2026-09-27)
- Bruin: [`lag`](https://getbruin.com/docs/bruin/core-concepts/semantic-layer.html) (read 2026-09-27)
- Bruin: [`lead`](https://getbruin.com/docs/bruin/core-concepts/semantic-layer.html) (read 2026-09-27)
- Apache DataFusion: [`first_value`](https://datafusion.apache.org/user-guide/sql/window_functions.html) (read 2026-09-27)
- Apache DataFusion: [`lag`](https://datafusion.apache.org/user-guide/sql/window_functions.html) (read 2026-09-27)
- Apache DataFusion: [`last_value`](https://datafusion.apache.org/user-guide/sql/window_functions.html) (read 2026-09-27)
- Apache DataFusion: [`lead`](https://datafusion.apache.org/user-guide/sql/window_functions.html) (read 2026-09-27)
- Apache DataFusion: [`nth_value`](https://datafusion.apache.org/user-guide/sql/window_functions.html) (read 2026-09-27)
- DuckDB: [`first_value`](https://duckdb.org/docs/current/sql/functions/window_functions) (read 2026-09-27)
- DuckDB: [`lag`](https://duckdb.org/docs/current/sql/functions/window_functions) (read 2026-09-27)
- DuckDB: [`last_value`](https://duckdb.org/docs/current/sql/functions/window_functions) (read 2026-09-27)
- DuckDB: [`lead`](https://duckdb.org/docs/current/sql/functions/window_functions) (read 2026-09-27)
- DuckDB: [`nth_value`](https://duckdb.org/docs/current/sql/functions/window_functions) (read 2026-09-27)
- Duckle: [`First Value`](https://github.com/slothflowlabs/duckle/blob/f85f8960edfb6efbf4cb89d58a7b32e16df0e867/website/docs/components.html) (read 2026-09-25)
- Duckle: [`Lag`](https://github.com/slothflowlabs/duckle/blob/f85f8960edfb6efbf4cb89d58a7b32e16df0e867/website/docs/components.html) (read 2026-09-25)
- Duckle: [`Last Value`](https://github.com/slothflowlabs/duckle/blob/f85f8960edfb6efbf4cb89d58a7b32e16df0e867/website/docs/components.html) (read 2026-09-25)
- Duckle: [`Lead`](https://github.com/slothflowlabs/duckle/blob/f85f8960edfb6efbf4cb89d58a7b32e16df0e867/website/docs/components.html) (read 2026-09-25)
- EasyMorph: [`Shift column up/down`](https://help.easymorph.com/doku.php?id=transformations:shiftcolumn) (read 2026-09-27)
- Apache Hop: [`Analytic Query`](https://hop.apache.org/manual/latest/pipeline/transforms/analyticquery.html) (read 2026-09-27)
- Ibis: [`Lag`](https://github.com/ibis-project/ibis/blob/05d2b293344e86cb41c1a426071734f7e270335a/ibis/expr/operations/analytic.py) (read 2026-09-27)
- Ibis: [`Lead`](https://github.com/ibis-project/ibis/blob/05d2b293344e86cb41c1a426071734f7e270335a/ibis/expr/operations/analytic.py) (read 2026-09-27)
- Ibis: [`NthValue`](https://github.com/ibis-project/ibis/blob/05d2b293344e86cb41c1a426071734f7e270335a/ibis/expr/operations/analytic.py) (read 2026-09-27)
- KNIME Analytics Platform: [`Lag Column`](https://api.hub.knime.com/nodes/*RurwVi1VODgLDTQP?details=full) (read 2026-09-27)
- Pentaho Data Integration: [`Analytic query`](https://docs.pentaho.com/pdia-data-integration/9.3-data-integration/pdi-transformation-steps-reference-overview) (read 2026-09-27)
- Polars: [`DataFrame.shift`](https://docs.pola.rs/api/python/stable/reference/dataframe/modify_select.html) (read 2026-09-27)
- Polars: [`Expr.diff`](https://docs.pola.rs/api/python/stable/reference/expressions/computation.html) (read 2026-09-27)
- Polars: [`Expr.pct_change`](https://docs.pola.rs/api/python/stable/reference/expressions/computation.html) (read 2026-09-27)
- Polars: [`Expr.shift`](https://docs.pola.rs/api/python/stable/reference/expressions/modify_select.html) (read 2026-09-27)
- RapidMiner Studio: [`Lag`](https://docs.rapidminer.com/latest/studio/operators/modeling/time_series/transformation/lag_series_with_default_lag.html) (read 2026-09-27)
- RapidMiner Studio: [`Windowing`](https://docs.rapidminer.com/latest/studio/operators/modeling/time_series/windowing/windowing.html) (read 2026-09-27)
- Tableau Prep: [`Difference From`](https://help.tableau.com/current/prep/en-us/prep_multirow_calculations.htm) (read 2026-09-27)
- Tableau Prep: [`LOOKUP`](https://help.tableau.com/current/prep/en-us/prep_function_reference.htm) (read 2026-09-27)
- Tableau Prep: [`Percent Difference From`](https://help.tableau.com/current/prep/en-us/prep_multirow_calculations.htm) (read 2026-09-27)
- Talend: [`tMemorizeRows`](https://help.qlik.com/talend/en-US/components/8.0/tmemorizerows/tmemorizerows-component) (read 2026-09-27)
- Trifacta: [`NEXT Function`](https://help.alteryx.com/aac/en/trifacta-classic/wrangle-language/window-functions.html) (read 2026-09-27)
- Trifacta: [`PREV Function`](https://help.alteryx.com/aac/en/trifacta-classic/wrangle-language/window-functions.html) (read 2026-09-27)
- VisiData: [`addcol-delta`](https://github.com/saulpw/visidata/blob/7609212ec2febafa9caba7183cbebfaf49627909/visidata/experimental/rownum.py#L73) (read 2026-09-23)
