> ## Documentation Index
> Fetch the complete documentation index at: https://lightdash-docs-data-app-visualizations.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Percent change from previous

> The percent change gives you the percent change in a value compared to the value in the row above it.

[Just gimme the code! <Icon icon="bug" iconType="solid" />](#here’s-the-sql-you-can-copy-paste-to-calculate-the-percent-change-from-the-previous)

Here's an example of a percent change calculation:

<Frame>
  <img src="https://mintcdn.com/lightdash-docs-data-app-visualizations/KTu47iVvTnYI6n68/images/guides/table-calculations/table-calculation-sql-templates/percent-change-16ad7591cac52477dd6cd3b210ee4e66.jpg?fit=max&auto=format&n=KTu47iVvTnYI6n68&q=85&s=c11d94598f58b5863644a1be5efbe83c" width="2820" height="1418" data-path="images/guides/table-calculations/table-calculation-sql-templates/percent-change-16ad7591cac52477dd6cd3b210ee4e66.jpg" />
</Frame>

And here's the SQL that was used to generate it:

```sql theme={null}
(
  ${orders.total_order_amount} /
    LAG(${orders.total_order_amount}) OVER (
      ORDER BY
        ${orders.order_date_week}
    )
) - 1
```

In general, the SQL used for calculating the percent change from the previous value has two bits (with an optional third bit):

* `column_I_want_to_compare` - this is the column with the values you want to compare
* `column_I_want_to_order_by` - this is the column you want to use to order the values you want to compare
* `optional_other_column_I_want_to_order_by` - this column is optional and you can add as many more `order by` columns as you need. Normally, you'll need to add every dimension in your results table to the `ORDER BY` bit in your SQL. And, the order of these will need to be the same as the ordering you've added to the columns in your results table.

### Here's the SQL you can copy-paste to calculate the percent change from the previous

```sql theme={null}
(
  ${table.column_i_want_to_compare} /
    LAG(${table.column_i_want_to_compare}) OVER (
      ORDER BY
        ${table.column_I_want_to_order_by},
        ${table.optional_other_column_I_want_to_order_by}
    )
) - 1
```

### Make sure to add percent formatting to your calculation

In the `format` tab, make sure to update the format to `percent` so that your table calculation is shown as a percentage value (instead of a number).

<Frame>
  <img src="https://mintcdn.com/lightdash-docs-data-app-visualizations/KTu47iVvTnYI6n68/images/guides/table-calculations/table-calculation-sql-templates/format-percent-6d905aa48e29631b298dd27a38a8b183.jpg?fit=max&auto=format&n=KTu47iVvTnYI6n68&q=85&s=a30d4b8ab8f1c7ca6eb9af4b84f8d767" width="2644" height="1512" data-path="images/guides/table-calculations/table-calculation-sql-templates/format-percent-6d905aa48e29631b298dd27a38a8b183.jpg" />
</Frame>
