Convert Multiple Account Rows to Columns with Excel Power Query
Question details
Convert multiple country rows per account into one row per account, with each country in a separate column.

- 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.
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.
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.
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.
Select a cell in the table and choose Data > From Table/Range to open the Power Query Editor.
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
Check the preview, then choose Home > Close & Load. After changing the source table, use Data > Refresh All in Excel to update the output.

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.

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.




