Skip to content

Grouping Tables (to show rankings)

This page shows how to group a table and rank the records inside each group, keeping only the top few.

The data lists some of the world’s largest companies with their main financial figures: sales, profits, assets and market value, in billions of dollars. The example uses the companies in this table:

An Excel table with columns Company, Country, Continent, Sales, Profits, Assets and Market Value, listing companies such as ICBC, JPMorgan Chase, Apple and Toyota Motor

The aim is a summary that shows the top three companies by sales on each continent, in descending order. Power Query’s Group By does the grouping, and a few table functions do the ranking.

The figures arrive as text, such as “$142.9”, because each one carries its dollar sign. Text sorts character by character rather than by value, so “$99.9” would come before “$267.7”, since 9 comes after 2. Before anything is sorted, a step removes the dollar signs and commas from the four figure columns and turns them into numbers:

= Table.TransformColumns(Source, List.Transform({"Sales", "Profits", "Assets", "Market Value"}, (column) => {column, each Number.From(Text.Remove(_, {"$", ","})), type number}))
  • On the Transform tab, click Group By.
  • Group by Continent, and add one output called Details with the All Rows operation.

The Group By dialog in Advanced mode, grouping by Continent, with one output called Details using All Rows

The query now has one row per continent, with a Table object in each Details cell:

Continent Details
Asia Table
North America Table
Middle East Table

Clicking beside a Table shows every company on that continent. North America’s holds these, in the order they appear in the source:

Company Country Continent Sales Profits Assets Market Value
JPMorgan Chase United States North America 142.9 30.0 3,139.4 291.7
Berkshire Hathaway United States North America 254.6 81.4 817.7 455.4
Bank of America United States North America 112.1 24.1 2,620.0 208.6
Apple United States North America 267.7 57.2 320.4 1,285.5
AT&T United States North America 179.2 14.4 545.4 218.6
Alphabet United States North America 166.3 34.5 273.4 919.3
ExxonMobil United States North America 256.0 14.3 362.6 196.6

Add three custom columns in turn, each building on the one before.

First, a column called Sorted, which sorts each continent’s table by Sales, largest first:

The Custom Column dialog creating Sorted with the formula Table.Buffer(Table.Sort([Details], {{“Sales”, Order.Descending}}))

Table.Buffer holds the sorted table in memory, so that the sort order is fixed before the next step reads from it.

Second, a column called Top 3, which keeps only the first three rows of each sorted table:

The Custom Column dialog creating Top 3 with the formula Table.FirstN([Sorted], 3)

Third, a column called Index, which adds a Rank column numbered from 1 to each of the Top 3 tables:

The Custom Column dialog creating Index with the formula Table.AddIndexColumn([Top 3], “Rank”, 1, 1)

Delete the Details, Sorted and Top 3 columns, because the Index column now holds everything needed. That leaves Continent and Index:

Continent Index
Asia Table
North America Table
Middle East Table

Click the expand icon on the Index column, and select every field except Continent, because the query already has it:

The expand menu on the Index column, with Company, Country, Sales, Profits, Assets, Market Value and Rank ticked and Continent unticked

Move the Rank column to the far left, and load the query to Excel with Close & Load. The result lists up to three companies per continent, each with its rank by sales. The Middle East has only one company in the data, so it has only one row:

Rank Continent Company Country Sales Profits Assets Market Value
1 Asia Toyota Motor Japan 280.5 22.7 495.1 173.3
2 Asia ICBC China 177.2 45.3 4,322.5 242.3
3 Asia China Construction Bank China 162.1 38.9 3,822.0 203.8
1 North America Apple United States 267.7 57.2 320.4 1,285.5
2 North America ExxonMobil United States 256.0 14.3 362.6 196.6
3 North America Berkshire Hathaway United States 254.6 81.4 817.7 455.4
1 Middle East Saudi Arabian Oil Company (Saudi Aramco) Saudi Arabia 329.8 88.2 398.3 1,684.8

Toyota Motor tops Asia even though it comes near the end of the source table, which is the sort doing its job: with the figures still as text, “$84.8” would have outranked “$280.5”.