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

# Percent of group/pivot total

> Calculate each value as a percentage of its group or pivot total

<Note>
  A [formula table calculation](/explore/table-calculations/formulas) computes this without hand-written SQL — combine the [percent-of-total example](/explore/table-calculations/formulas#examples) with a [`PARTITION BY` clause](/explore/table-calculations/formulas#window-clauses-partition-by-and-order-by) to divide each value by its group's total.
</Note>

[Just gimme the code! <Icon icon="bug" />](#here’s-the-sql-you-can-copy-paste-to-calculate-the-percent-of-the-group/pivot-total)

Here's an example of a percent of the total group/pivot:

<Frame>
  <img src="https://mintcdn.com/lightdash/PptB0out6mCmbYM1/images/explore/table-calculations/sql-templates/percent-of-group-pivot-total/percent-of-total-group-e5c69d1ef40dca9abac547acaa94f597.png?fit=max&auto=format&n=PptB0out6mCmbYM1&q=85&s=c45e0552afea65d4572b1b6a8138edea" alt="A pivoted results table with a table calculation column giving each cell's percent of its group total" width="1824" height="824" data-path="images/explore/table-calculations/sql-templates/percent-of-group-pivot-total/percent-of-total-group-e5c69d1ef40dca9abac547acaa94f597.png" />
</Frame>

And here's the SQL used in the table calculation:

```sql theme={null}
${orders.total_order_amount} /
  SUM(${orders.total_order_amount}) OVER (
    PARTITION BY
      ${orders.order_date_month}
  )
```

In general, the SQL used for calculating the percent of the total has two important columns:

* `column_i_want_to_see_the_percent_of_total` - this is the column that you want to see the percent total of. In our example above, that was the `sum of profit`.
* `column_i_want_to_group_by` - this is the column that you want the total to be grouped by. In our example above, that was the `order date - month`.

### Here's the SQL you can copy-paste to calculate the percent of the group/pivot total

```sql theme={null}
${table.column_i_want_to_see_the_percent_of_total} /
  SUM(${table.column_i_want_to_see_the_percent_of_total}) OVER (
    PARTITION BY
      ${table.column_i_want_to_group_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).

<Frame>
  <img src="https://mintcdn.com/lightdash/PptB0out6mCmbYM1/images/explore/table-calculations/sql-templates/format-percent-6d905aa48e29631b298dd27a38a8b183.jpg?fit=max&auto=format&n=PptB0out6mCmbYM1&q=85&s=0cdf734f62493aa4ea12be2f80c71643" alt="The Format tab of the table calculation editor with the format set to percent" width="2644" height="1512" data-path="images/explore/table-calculations/sql-templates/format-percent-6d905aa48e29631b298dd27a38a8b183.jpg" />
</Frame>
