Power Query Problems

Convert Multiple Account Rows to Columns with Excel Power Query

John WilsonJohn Wilson Nov 18, 2026 871 views

Question details

Convert multiple country rows per account into one row per account, with each country in a separate column.

Convert Multiple Account Rows to Columns with Excel Power Query
Product
Microsoft Excel Power Query
Device & OS
not provided
Scenario
A large table contains account numbers and one or more associated countries, and the user wants a refreshable transformation without manually transposing each account.
Observed behavior
The desired result is one row per account with countries in columns such as Country 1 and Country 2.
Before you start

Make sure the source data has headers named Account and Country. If account numbers contain leading zeros, store them as text before loading the data into Power Query.

Solution 1Recommended

Group accounts and pivot indexed countries in Power Query

Use a refreshable query to create a separate country column for each row associated with an account.

The query groups rows by account, numbers the countries within each group, and pivots those numbers into column headers. The maximum number of countries for any account determines how many country columns are created.

1
Create a source table

Select the data in Excel, choose Insert > Table, and confirm that the table has headers. On the Table Design tab, set the table name to AccountCountries.

2
Open Power Query

Select a cell in the table and choose Data > From Table/Range to open the Power Query Editor.

3
Enter the transformation

Choose Home > Advanced Editor, replace the existing code with the following, and select Done: let Source = Excel.CurrentWorkbook(){[Name="AccountCountries"]}[Content], Grouped = Table.Group(Source, {"Account"}, {{"Rows", each Table.AddIndexColumn(Table.SelectColumns(_, {"Country"}), "CountryColumn", 1, 1, Int64.Type)}}), Expanded = Table.ExpandTableColumn(Grouped, "Rows", {"Country", "CountryColumn"}, {"Country", "CountryColumn"}), Named = Table.TransformColumns(Expanded, {{"CountryColumn", each "Country " & Text.From(_), type text}}), Pivoted = Table.Pivot(Named, List.Distinct(Named[CountryColumn]), "CountryColumn", "Country") in Pivoted

4
Load and refresh the result

Check the preview, then choose Home > Close & Load. After changing the source table, use Data > Refresh All in Excel to update the output.

Group accounts and pivot indexed countries in Power Query
Country order and extra columns: The query numbers country rows within each account without defining a specific sort order. It keeps the Account and Country fields only; other source columns are not included in the result.
Free Microsoft Office alternative

Work with account and country tables in WPS Spreadsheets

WPS Office is a free, lightweight Office alternative with a familiar spreadsheet interface. Open compatible Excel workbooks and continue working with account and country tables as you move between applications. WPS does not fix Excel Power Query or guarantee that its queries will run.

Open and edit compatible Microsoft Excel XLSX filesReview and organize account and country data in a familiar spreadsheet interfaceUse a free, lightweight Office suite for everyday spreadsheet workMove existing Office documents into WPS without starting from scratch
microsoft office alternative - wps office

Frequently Asked Questions

How do I update the output when I add more accounts?

Add the new rows to the AccountCountries source table, then choose Data > Refresh All in Excel. The query will regroup and pivot the updated data.

What happens if one account has more countries than the others?

The query creates enough Country columns for the account with the most country rows. Accounts with fewer countries have blank cells in the remaining columns.

Will duplicate country entries be removed?

No. Each source row receives its own index, so repeated country values appear in separate columns. To remove duplicates, select the Account and Country columns in Power Query and choose Home > Remove Rows > Remove Duplicates before grouping.

Can I use different table or column names?

Yes. In Home > Advanced Editor, replace AccountCountries with the source table name and update every Account and Country reference to match the source headers.