Skip to content

Calculating percentage shares

This page adds a column that shows each product’s sales as a percentage of total sales. The technique is to calculate the grand total in one step, turn it into a single value, and then go back to the original table and divide each row by that value.

The example starts from this Excel table of sales by product.

An Excel table named Fruits with Product and Sales columns, listing apples, oranges, pears and grapes

  • Click inside the table, and on Excel’s Data tab click From Table/Range. The Power Query Editor opens with a new query.
  • On the Home tab, click Group By, and choose Advanced.
  • Remove every grouping column, so that the whole table forms one group.
  • Type GrandTotal as the new column name, choose Sum as the operation and Sales as the column.

The Group By dialog in Advanced mode with no grouping columns and a single aggregation, GrandTotal, as the sum of Sales

  • Click OK.

The query now returns a table with one column, GrandTotal, and one row, which holds the total of the Sales column.

  • Right-click the value, and choose Drill Down.

The one-cell GrandTotal table, showing 26, with the right-click menu open at Drill Down

The new step returns the grand total as a single number rather than as a cell in a table. Power Query names the step after the column, GrandTotal, and later steps can use that name to refer to the number.

The next step goes back to the original table, so that each sales figure can be divided by the total.

  • Click the fx button beside the formula bar to add a new step.
  • Replace everything after the equals sign with the name of the query’s first step, which should be Source. The step now returns the original table, as loaded from Excel.

The formula bar reading = Source, with the original table of four products and their sales below it

  • On the Add Column tab, click Custom Column.
  • Name the new column Share. Double-click Sales in the list of available columns to add it to the formula, and divide it by GrandTotal, the name of the drill-down step. The formula reads:
= [Sales]/GrandTotal

The Custom Column dialog with the new column named Share and the formula = [Sales]/GrandTotal

The new column holds each product’s share of the total as a decimal fraction.

The query with a Share column holding values between 0 and 1, such as 0.230769231 for apples

  • Click Close & Load.

In the worksheet, format the Share column as a percentage in the usual way, by selecting the column, right-clicking and choosing Format Cells.

The loaded Excel table with Product, Sales and Share columns, where Share is shown as a percentage, such as 23% for apples

The code for the whole query is below.

let
//Load data from the Excel worksheet
Source = Excel.CurrentWorkbook(){[Name="Fruits"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Product", type text}, {"Sales", Int64.Type}}),
//Return a table containing the grand total of sales
#"Grouped Rows" = Table.Group(#"Changed Type", {}, {{"GrandTotal", each List.Sum([Sales]), type nullable number}}),
//Returns the grand total as a value on its own
GrandTotal = #"Grouped Rows"{0}[GrandTotal],
//Fetch the original input table
Custom1 = Source,
//Insert a custom column to calculate the percentage share
#"Added Custom" = Table.AddColumn(Custom1, "Share", each [Sales]/GrandTotal),
#"Changed Type1" = Table.TransformColumnTypes(#"Added Custom",{{"Share", Percentage.Type}})
in
#"Changed Type1"

This example follows one in Chris Webb’s Power Query for Power BI and Excel (Apress).