Shape · Columns

Multi-row Formula

Works out a column from the current row and its neighbours, including the column's own value on the previous row. Use it for fill-down, running totals or carrying the last value forward.

Also known as: Previous row, fill down

How it works

Choose Update column to overwrite an existing column (it keeps its type) or New column with a name and a Type (Double by default). Group by starts the sequence again per group. The Formula is Add Column's dialect plus row tokens: [Row-1:col] reads col one row back, [Row+2:col] two rows ahead, and [Row-1:target], where target is the column being computed, is the value this formula produced on the previous row. The target may only be read one row back, never further or ahead. Rows are taken in the order they arrive, so sort first if the order matters. Rows past the edge read as decides whether a token beyond the first or last row gives NULL or an empty value (0 for numbers, an empty string for text).

Inputs and outputs

Inputs

  • data — Table

Outputs

  • data — Table

Settings

The Attributes panel of Multi-row Formula, as in the app: the controls work, but nothing runs — there is no data behind it.

The same settings, as the Assistant writes them
{
  mode?: "update"|"new"
  target: string
  outputType?: "string"|"integer"|"double"|"boolean"|"date"|"timestamp"
  expression: string
  groupBy?: string[]
  otherRows?: "null"|"empty"
}

expression = raw DuckDB SQL (Add Column's dialect) with row tokens [Row-N:col] / [Row+N:col]; [Row-1:<target>] is the value computed for the previous row (fill-down: "CASE WHEN x IS NULL THEN [Row-1:x] ELSE x END"); the target may not be read further back or ahead. mode "update" overwrites target (keeping its type), "new" appends it as outputType. Rows restart per groupBy; otherRows is what a row past the edge reads as (empty = 0 for numbers, '' for text)

Example

A spreadsheet export lists the region only on the first row of each block. Update region with CASE WHEN region IS NULL THEN [Row-1:region] ELSE region END. For a running total instead, add a new running column with [Row-1:running] + amount, group by region and let edge rows read as empty.

regionamountrunning
North1010
NULL → North515
South77