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.

1. Calculate the grand total
Section titled “1. Calculate the grand total”- 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.

- 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 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.
2. Divide each row by the grand total
Section titled “2. Divide each row by the grand total”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.

- 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](/_astro/percentage-shares-5.4E-pqwJ2_F3zEb.webp)
The new column holds each product’s share of the total as a decimal fraction.

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

3. The M code
Section titled “3. The M code”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).