Data connectivity & integrationPipeline Builder ExpressionsLag

Lag

Supported in: Batch, Faster

Returns the value of the input expression evaluated at an earlier row in the window, specifically the row that is a given number of rows before the current row. For example, with a lag of 1, this returns the value from the row immediately before the current row; with a lag of 2, it returns the value from two rows before, and so on. This lets you compare a value in the current row to a value in an earlier row without a self-join.

This requires an ordered window. The window's ordering defines which row comes before the current row, so you must specify an order for the results to be deterministic.

If the window does not contain a row that far back, such as when the current row is within the first few rows of the window, the function returns the default value, or null if no default is specified.

Expression categories: Aggregate

Declared arguments

  • Expression: Expression to lag.
    Expression<T>
  • optional Default value: Default value if there are fewer than 'lag' rows before the current row.
    Literal<T>
  • optional Lag: Number of rows to lag.
    Literal<Integer>

Type variable bounds: T accepts AnyType

Output type: T

Examples

Example 1: Base case

Argument values:

  • Expression: price
  • Default value: null
  • Lag: 1
priceOutput
100null
105100
98105
10298

Example 2: Null case

Argument values:

  • Expression: price
  • Default value: 0
  • Lag: 1
priceOutput
1000
105100
98105
10298

Example 3: Edge case

Argument values:

  • Expression: price
  • Default value: null
  • Lag: 2
priceOutput
100null
105null
98100
102105