> ## Documentation Index
> Fetch the complete documentation index at: https://docs.maia.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Lead/Lag

export const ComponentMetadata = ({warehouses, unsupportedWarehouses = [], componentType, connectionInputs, connectionOutputs}) => {
  const allWarehouses = [...warehouses.map(w => ({
    name: w,
    supported: true
  })), ...unsupportedWarehouses.map(w => ({
    name: w,
    supported: false
  }))];
  return <div style={{
    background: 'var(--colors-background-light, #f9fafb)',
    border: '1px solid var(--colors-border-default, #e5e7eb)',
    borderRadius: '12px',
    padding: '20px 28px',
    marginBottom: '28px',
    boxShadow: '0 1px 4px rgba(0,0,0,0.10)'
  }}>
      <table style={{
    width: '100%',
    borderCollapse: 'collapse'
  }}>
        <tbody>
          <tr>
            <td style={{
    fontWeight: '600',
    paddingRight: '32px',
    paddingBottom: '14px',
    whiteSpace: 'nowrap',
    verticalAlign: 'middle',
    width: '180px'
  }}>Project Availability</td>
            <td style={{
    paddingBottom: '14px',
    verticalAlign: 'middle'
  }}>
              <div style={{
    display: 'flex',
    flexWrap: 'wrap',
    gap: '8px'
  }}>
                {allWarehouses.map((w, i) => <span key={i} style={{
    background: w.supported ? '#dcfce7' : '#fee2e2',
    color: w.supported ? '#15803d' : '#b91c1c',
    border: `1px solid ${w.supported ? '#bbf7d0' : '#fca5a5'}`,
    borderRadius: '9999px',
    padding: '3px 12px',
    fontSize: '0.85rem',
    fontWeight: '500',
    whiteSpace: 'nowrap'
  }}>
                    {w.name} {w.supported ? '✅' : '❌'}
                  </span>)}
              </div>
            </td>
          </tr>
          <tr>
            <td style={{
    fontWeight: '600',
    paddingRight: '32px',
    paddingBottom: '14px',
    whiteSpace: 'nowrap',
    verticalAlign: 'middle'
  }}>Component Type</td>
            <td style={{
    paddingBottom: '14px',
    verticalAlign: 'middle'
  }}>{componentType}</td>
          </tr>
          <tr>
            <td style={{
    fontWeight: '600',
    paddingRight: '32px',
    paddingBottom: '14px',
    whiteSpace: 'nowrap',
    verticalAlign: 'middle'
  }}>Connection Inputs</td>
            <td style={{
    paddingBottom: '14px',
    verticalAlign: 'middle'
  }}>{connectionInputs}</td>
          </tr>
          <tr>
            <td style={{
    fontWeight: '600',
    paddingRight: '32px',
    whiteSpace: 'nowrap',
    verticalAlign: 'middle'
  }}>Connection Outputs</td>
            <td style={{
    verticalAlign: 'middle'
  }}>{connectionOutputs}</td>
          </tr>
        </tbody>
      </table>
    </div>;
};

<ComponentMetadata warehouses={["Snowflake", "Databricks", "Amazon Redshift", "Google BigQuery"]} componentType="Transformation" connectionInputs="One" connectionOutputs="Unlimited" />

The **Lead/Lag** transformation component retrieves values from table rows preceding or following the current row.

* The **LEAD** function returns the values for a row at a given offset after the current row in the partition.
* The **LAG** function returns the values for a row at a given offset before the current row in the partition.

In SQL terms, these are equivalent to using the `LEAD ... OVER` or `LAG ... OVER` clauses of a `SELECT` statement.

You can combine multiple **LEAD** and/or **LAG** operations in a single instance of the component, letting you obtain a range of offset data from the input dataset.

The component uses the **window** functions supported by your cloud data warehouse. Window functions operate on a partition or "window" of a dataset. More details of these functions can be found in your cloud data warehouse documentation:

* [Snowflake window functions](https://docs.snowflake.com/sql-reference/functions-window)
  * [Lead](https://docs.snowflake.com/sql-reference/functions/lead)
  * [Lag](https://docs.snowflake.com/en/sql-reference/functions/lag)
* [Databricks window functions](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-window-functions)
  * [Lead](https://docs.databricks.com/aws/en/sql/language-manual/functions/lead)
  * [Lag](https://docs.databricks.com/aws/en/sql/language-manual/functions/lag)
* [Amazon Redshift window functions](https://docs.aws.amazon.com/redshift/latest/dg/c_Window_functions.html)
  * [Lead](https://docs.aws.amazon.com/redshift/latest/dg/r_WF_LEAD.html)
  * [Lag](https://docs.aws.amazon.com/redshift/latest/dg/r_WF_LAG.html)
* [Google BigQuery analytic functions](https://cloud.google.com/bigquery/docs/reference/standard-sql/analytic-function-concepts)
  * [Lead](https://cloud.google.com/bigquery/docs/reference/standard-sql/navigation_functions#lead)
  * [Lag](https://cloud.google.com/bigquery/docs/reference/standard-sql/navigation_functions#lag)

### Use case

Because Lead/Lag allows you to look forward or backward at other rows in a dataset, it's helpful in time series and analytical processing. Some common uses for this include:

* Calculating changes over time. For example, using **LAG** to obtain the difference between the current day and the previous day when days are expressed as different rows in the dataset.
* Detecting trends and identifying outliers. For example, using **LEAD** to obtain this month's spending and compare it to next month's, and flagging instances where the trend is downward.
* Building cumulative or rolling metrics. For example, using multiple **LAG** functions to obtain data from one day ago, two days ago, and so on.

***

## Properties

<Tabs>
  <Tab title="Snowflake">
    <ResponseField name="Name" type="string" required>
      A human-readable name for the component.
    </ResponseField>

    {/* <!-- param-start:[includeInputColumns] | warehouses: [snowflake] --> */}

    <ResponseField name="Include input columns" type="boolean" required>
      When **Yes**, the component passes all input columns into the output.
    </ResponseField>

    {/* <!-- param-start:[partitionData] | warehouses: [snowflake] --> */}

    <ResponseField name="Partition data" type="dual listbox">
      Defines how the input data is partitioned to perform the rank calculation. The calculation is then performed on each partition.

      To use grid variables, toggle **Use Grid Variable** on at the top of the dialog. For more information, read [Grid variables](/docs/guides/grid-variables).
    </ResponseField>

    {/* <!-- param-start:[orderingsWithinPartitions] | warehouses: [snowflake] --> */}

    <ResponseField name="Orderings within partitions" type="column editor">
      * **Input Column:** The input column name for sorting within the partitioned data. You can drag columns to reorder them.
      * **Ordering:** The order of the sorting: Ascending, Descending, Nulls First, or Nulls Last.

      Click the **Text mode** toggle at the bottom of the dialog to open a multi-line editor that lets you add items in a single block. For more information, read [Text mode](/docs/guides/components-overview#text-mode).

      To use grid variables, toggle **Use Grid Variable** on at the bottom of the dialog. For more information, read [Grid variables](/docs/guides/grid-variables).
    </ResponseField>

    {/* <!-- param-start:[functions] | warehouses: [snowflake] --> */}

    <ResponseField name="Functions" type="column editor" required>
      * **Window Function:**
        * **Lead:** Returns the column from an **Offset** number of rows after the current row. For more information, read the [Snowflake Lead](https://docs.snowflake.com/sql-reference/functions/lead) documentation.
        * **Lag:** Returns the column from an **Offset** number of rows before the current row. For more information, read the [Snowflake Lag](https://docs.snowflake.com/en/sql-reference/functions/lag) documentation.
      * **Input Column:** The name of the input column that the Lead/Lag function will retrieve data from.
      * **Offset:** The number of rows to look forward (Lead) or backward (Lag) in the partition.
      * **Output Column:** The name of a column that will be created in the output dataset for the retrieved Lead/Lag data to populate.

      Click the **Text mode** toggle at the bottom of the dialog to open a multi-line editor that lets you add items in a single block. For more information, read [Text mode](/docs/guides/components-overview#text-mode).

      To use grid variables, toggle **Use Grid Variable** on at the bottom of the dialog. For more information, read [Grid variables](/docs/guides/grid-variables).
    </ResponseField>

    {/* <!-- param-start:[ignoreNulls] | warehouses: [snowflake] --> */}

    <ResponseField name="Ignore nulls" type="boolean" required>
      When **Yes**, disregard null values when determining which row to use. Null values don't count toward reaching the offset.
    </ResponseField>
  </Tab>

  <Tab title="Databricks">
    <ResponseField name="Name" type="string" required>
      A human-readable name for the component.
    </ResponseField>

    {/* <!-- param-start:[includeInputColumns] | warehouses: [databricks] --> */}

    <ResponseField name="Include input columns" type="boolean" required>
      When **Yes**, the component passes all input columns into the output.
    </ResponseField>

    {/* <!-- param-start:[partitionData] | warehouses: [databricks] --> */}

    <ResponseField name="Partition data" type="dual listbox">
      Defines how the input data is partitioned to perform the rank calculation. The calculation is then performed on each partition.

      To use grid variables, toggle **Use Grid Variable** on at the top of the dialog. For more information, read [Grid variables](/docs/guides/grid-variables).
    </ResponseField>

    {/* <!-- param-start:[orderingsWithinPartitions] | warehouses: [databricks] --> */}

    <ResponseField name="Orderings within partitions" type="column editor">
      * **Input Column:** The input column name for sorting within the partitioned data. You can drag columns to reorder them.
      * **Ordering:** The order of the sorting: Ascending, Descending, Nulls First, or Nulls Last.

      Click the **Text mode** toggle at the bottom of the dialog to open a multi-line editor that lets you add items in a single block. For more information, read [Text mode](/docs/guides/components-overview#text-mode).

      To use grid variables, toggle **Use Grid Variable** on at the bottom of the dialog. For more information, read [Grid variables](/docs/guides/grid-variables).
    </ResponseField>

    {/* <!-- param-start:[functions] | warehouses: [databricks] --> */}

    <ResponseField name="Functions" type="column editor" required>
      * **Window Function:**
        * **Lead:** Returns the column from an **Offset** number of rows after the current row. For more information, read the [Databricks Lead](https://docs.databricks.com/aws/en/sql/language-manual/functions/lead) documentation.
        * **Lag:** Returns the column from an **Offset** number of rows before the current row. For more information, read the [Databricks Lag](https://docs.databricks.com/aws/en/sql/language-manual/functions/lag) documentation.
      * **Input Column:** The name of the input column that the Lead/Lag function will retrieve data from.
      * **Offset:** The number of rows to look forward (Lead) or backward (Lag) in the partition.
      * **Output Column:** The name of a column that will be created in the output dataset for the retrieved Lead/Lag data to populate.

      Click the **Text mode** toggle at the bottom of the dialog to open a multi-line editor that lets you add items in a single block. For more information, read [Text mode](/docs/guides/components-overview#text-mode).

      To use grid variables, toggle **Use Grid Variable** on at the bottom of the dialog. For more information, read [Grid variables](/docs/guides/grid-variables).
    </ResponseField>

    {/* <!-- param-start:[ignoreNulls] | warehouses: [databricks] --> */}

    <ResponseField name="Ignore nulls" type="boolean" required>
      When **Yes**, disregard null values when determining which row to use. Null values don't count toward reaching the offset.
    </ResponseField>
  </Tab>

  <Tab title="Amazon Redshift">
    <ResponseField name="Name" type="string" required>
      A human-readable name for the component.
    </ResponseField>

    {/* <!-- param-start:[includeInputColumns] | warehouses: [redshift] --> */}

    <ResponseField name="Include input columns" type="boolean" required>
      When **Yes**, the component passes all input columns into the output.
    </ResponseField>

    {/* <!-- param-start:[partitionData] | warehouses: [redshift] --> */}

    <ResponseField name="Partition data" type="dual listbox">
      Defines how the input data is partitioned to perform the rank calculation. The calculation is then performed on each partition.

      To use grid variables, toggle **Use Grid Variable** on at the top of the dialog. For more information, read [Grid variables](/docs/guides/grid-variables).
    </ResponseField>

    {/* <!-- param-start:[orderingsWithinPartitions] | warehouses: [redshift] --> */}

    <ResponseField name="Orderings within partitions" type="column editor">
      * **Input Column:** The input column name for sorting within the partitioned data. You can drag columns to reorder them.
      * **Ordering:** The order of the sorting: Ascending, Descending, Nulls First, or Nulls Last.

      Click the **Text mode** toggle at the bottom of the dialog to open a multi-line editor that lets you add items in a single block. For more information, read [Text mode](/docs/guides/components-overview#text-mode).

      To use grid variables, toggle **Use Grid Variable** on at the bottom of the dialog. For more information, read [Grid variables](/docs/guides/grid-variables).
    </ResponseField>

    {/* <!-- param-start:[functions] | warehouses: [redshift] --> */}

    <ResponseField name="Functions" type="column editor" required>
      * **Window Function:**
        * **Lead:** Returns the column from an **Offset** number of rows after the current row. For more information, read the [Amazon Redshift Lead](https://docs.aws.amazon.com/redshift/latest/dg/r_WF_LEAD.html) documentation.
        * **Lag:** Returns the column from an **Offset** number of rows before the current row. For more information, read the [Amazon Redshift Lag](https://docs.aws.amazon.com/redshift/latest/dg/r_WF_LAG.html) documentation.
      * **Input Column:** The name of the input column that the Lead/Lag function will retrieve data from.
      * **Offset:** The number of rows to look forward (Lead) or backward (Lag) in the partition.
      * **Output Column:** The name of a column that will be created in the output dataset for the retrieved Lead/Lag data to populate.

      Click the **Text mode** toggle at the bottom of the dialog to open a multi-line editor that lets you add items in a single block. For more information, read [Text mode](/docs/guides/components-overview#text-mode).

      To use grid variables, toggle **Use Grid Variable** on at the bottom of the dialog. For more information, read [Grid variables](/docs/guides/grid-variables).
    </ResponseField>

    {/* <!-- param-start:[ignoreNulls] | warehouses: [redshift] --> */}

    <ResponseField name="Ignore nulls" type="boolean" required>
      When **Yes**, disregard null values when determining which row to use. Null values don't count toward reaching the offset.
    </ResponseField>
  </Tab>

  <Tab title="Google BigQuery">
    <ResponseField name="Name" type="string" required>
      A human-readable name for the component.
    </ResponseField>

    {/* <!-- param-start:[includeInputColumns] | warehouses: [bigquery] --> */}

    <ResponseField name="Include input columns" type="boolean" required>
      When **Yes**, the component passes all input columns into the output.
    </ResponseField>

    {/* <!-- param-start:[partitionData] | warehouses: [bigquery] --> */}

    <ResponseField name="Partition data" type="dual listbox">
      Defines how the input data is partitioned to perform the rank calculation. The calculation is then performed on each partition.

      To use grid variables, toggle **Use Grid Variable** on at the top of the dialog. For more information, read [Grid variables](/docs/guides/grid-variables).
    </ResponseField>

    {/* <!-- param-start:[orderingsWithinPartitions] | warehouses: [bigquery] --> */}

    <ResponseField name="Orderings within partitions" type="column editor">
      * **Input Column:** The input column name for sorting within the partitioned data. You can drag columns to reorder them.
      * **Ordering:** The order of the sorting: Ascending, Descending, Nulls First, or Nulls Last.

      Click the **Text mode** toggle at the bottom of the dialog to open a multi-line editor that lets you add items in a single block. For more information, read [Text mode](/docs/guides/components-overview#text-mode).

      To use grid variables, toggle **Use Grid Variable** on at the bottom of the dialog. For more information, read [Grid variables](/docs/guides/grid-variables).
    </ResponseField>

    {/* <!-- param-start:[functions] | warehouses: [bigquery] --> */}

    <ResponseField name="Functions" type="column editor" required>
      * **Window Function:**
        * **Lead:** Returns the column from an **Offset** number of rows after the current row. For more information, read the [Google BigQuery Lead](https://cloud.google.com/bigquery/docs/reference/standard-sql/navigation_functions#lead) documentation.
        * **Lag:** Returns the column from an **Offset** number of rows before the current row. For more information, read the [Google BigQuery Lag](https://cloud.google.com/bigquery/docs/reference/standard-sql/navigation_functions#lag) documentation.
      * **Input Column:** The name of the input column that the Lead/Lag function will retrieve data from.
      * **Offset:** The number of rows to look forward (Lead) or backward (Lag) in the partition.
      * **Output Column:** The name of a column that will be created in the output dataset for the retrieved Lead/Lag data to populate.

      Click the **Text mode** toggle at the bottom of the dialog to open a multi-line editor that lets you add items in a single block. For more information, read [Text mode](/docs/guides/components-overview#text-mode).

      To use grid variables, toggle **Use Grid Variable** on at the bottom of the dialog. For more information, read [Grid variables](/docs/guides/grid-variables).
    </ResponseField>
  </Tab>
</Tabs>
