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

# Percent of previous value

> Calculate a value as a percentage of the value in the row above it

> [!NOTE]
>
> The [`LAG` window function](https://docs.meetsilicon.com/explore/table-calculations/formulas#window) in a [formula table calculation](https://docs.meetsilicon.com/explore/table-calculations/formulas.md) computes this without hand-written SQL -- for example, `=revenue / LAG(revenue)`.

[Just gimme the code! ](#heres-the-sql-you-can-copy-paste-to-calculate-the-percent-of-the-previous-value)

Here's an example of a percent of previous value calculation:

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},
      ${orders.status}
  )
```

In general, the SQL used for calculating the percent of 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 of the previous value

```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}
  )
```

### 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).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.