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

# pct_change

> Calculate percentage change between consecutive numbers in a numeric column. 

For a pair of consecutive numbers x1, x2 in the input column, calculates the difference between x2 and x1
expressed as a percentage of x1, i.e. `100 * (x2 - x1) / x1`.

## Usage

The following examples show how the step can be used in a recipe.

<Accordion title="Examples" icon="code" defaultOpen="true">
  <Tabs>
    <Tab title="Example 1">
      Calculate daily percentage change of stock prices sorted by date

      ```stan theme={null}
      pct_change(ds.price, ds.date) => (ds.price_pct_change)
      ```
    </Tab>

    <Tab title="Example 2">
      Calculate proportional change in revenue without sorting

      ```stan theme={null}
      pct_change(ds.revenue, {"as_proportion": true}) => (ds.revenue_change)
      ```
    </Tab>

    <Tab title="Signature">
      General syntax for using the step in a recipe. Shows the inputs and outputs the step is expected to receive and will produce respectively. For futher details see sections below.

      ```stan theme={null}
      pct_change(data: number, *sort: column, {
          "param": value,
          ...
      }) -> (output: number)
      ```
    </Tab>
  </Tabs>
</Accordion>

## Inputs & Outputs

The following are the inputs expected by the step and the outputs it produces. These are generally
columns (`ds.first_name`), datasets (`ds` or `ds[["first_name", "last_name"]]`) or models (referenced
by name e.g. `"churn-clf"`).

<Accordion title="Inputs" icon="right-to-bracket">
  <ParamField path="data" type="column[number]" required>
    A numeric column to calculate percentage change for.
  </ParamField>

  <ParamField path="*sort" type="column">
    An optional column used to sort the rows before calculating the percent change.
  </ParamField>
</Accordion>

<Accordion title="Outputs" icon="right-from-bracket">
  <ParamField path="output" type="column[number]" required>
    A numeric column with the calculated percentages.
  </ParamField>
</Accordion>

## Configuration

The following parameters can be used to configure the behaviour of the step by including them in
a json object as the last "input" to the step, i.e. `step(..., {"param": "value", ...}) -> (output)`.

<Accordion title="Parameters" defaultOpen="true" icon="sliders">
  <ParamField path="fill_nan" type="string" default="forward">
    Method to fill missing values (NaNs).
    When `forward`, uses last valid observation to fill a gap. When `backwards`, uses next valid observation instead.

    Values must be one of the following:

    * `forward`
    * `backward`
  </ParamField>

  <ParamField path="fill_nan_max" type="[integer, null]">
    Maximum number of consecutive NaNs to fill before stopping.

    Values must be in the following range:

    ```javascript theme={null}
    1 ≤ fill_nan_max < inf
    ```
  </ParamField>

  <ParamField path="as_proportion" type="boolean" default="false">
    Whether to express result as proportion instead of percentage.
  </ParamField>

  <ParamField path="ascending" type="boolean" default="true">
    Whether to sort ascending instead of descending using the `sort` column before calculating the change.
  </ParamField>
</Accordion>
