• Skip to main content
  • Skip to header right navigation
  • Skip to site footer

My Online Training Hub

Learn Dashboards, Excel, Power BI, Power Query, Power Pivot

  • Courses
  • Pricing
    • Free Courses
    • Power BI Course
    • Excel Power Query Course
    • Power Pivot and DAX Course
    • Excel Dashboard Course
    • Excel PivotTable Course – Quick Start
    • Advanced Excel Formulas Course
    • Excel Expert Advanced Excel Training
    • Excel Tables Course
    • Excel, Word, Outlook
    • Financial Modelling Course
    • Excel PivotTable Course
    • Excel for Customer Service Professionals
    • Excel for Operations Management Course
    • Excel for Decision Making Under Uncertainty Course
    • Excel for Finance Course
    • Excel Analysis ToolPak Course
    • Multi-User Pricing
  • Resources
    • Free Downloads
    • Excel Functions Explained
    • Excel Formulas
    • Excel Add-ins
    • IF Function
      • Excel IF Statement Explained
      • Excel IF AND OR Functions
      • IF Formula Builder
    • Time & Dates in Excel
      • Excel Date & Time
      • Calculating Time in Excel
      • Excel Time Calculation Tricks
      • Excel Date and Time Formatting
    • Excel Keyboard Shortcuts
    • Excel Custom Number Format Guide
    • Pivot Tables Guide
    • VLOOKUP Guide
    • ALT Codes
    • Excel VBA & Macros
    • Excel User Forms
    • VBA String Functions
  • Members
    • Login
  • Blog
  • Excel Webinars
  • Excel Forum
    • Register as Forum Member
  • Login

Convert 2 column table with duplicate values in column 1 to table with multiple columns and unique values in column 1 |Power Query|Excel Forum|My Online Training Hub

You are here: Home / Convert 2 column table with duplicate values in column 1 to table with multiple columns and unique values in column 1 |Power Query|Excel Forum|My Online Training Hub

office scripts course

Avatar
sp_LogInOut Log In sp_Registration Register
sp_Search Search
Advanced Search|Last Search Results
Search
Forum Scope




Match



Forum Options



Minimum search word length is 3 characters - maximum search word length is 84 characters
sp_Search Search
sp_RankInfo
Lost password?
sp_CrumbsHome HomeExcel ForumPower QueryConvert 2 column table with duplica…
sp_PrintTopic sp_TopicIcon
Convert 2 column table with duplicate values in column 1 to table with multiple columns and unique values in column 1
Avatar
Robert Green
Member
Members
Level 0
Forum Posts: 25
Member Since:
November 9, 2020
sp_UserOfflineSmall Offline
1
May 26, 2023 - 2:43 am
sp_Permalink sp_Print

I have a table with 2 columns. The first column contains duplicate values of a code. The second column contains a name associated with the code. I would like create a table with unique values in column 1 and with multiple columns each containing a name associated with the code. Can I use Power Query? Thanks
Code Name
01404360 BOOTH, Adrian
01404360 LEWIS, David Alexander
01404360 NICHOLSON, Leslie
11772984 BELLIS, Tom Arthur
07277291 OSBORNE, Leigh Ross
07277291 OSBORNE, Maxine
07277291 TYLER, Gregory Stuart
05052644 PERKINS, Antony James
05052644 PERKINS, Nicola Jane
01461813 CANNON, Hugo James
01461813 CANNON, Robert James
01461813 DAWSON, Peter Graham
01461813 SHROSBREE, Richard James
04299559 DEFFLEY, Anthony Francis
04299559 DEFFLEY, Steven Francis
00549113 BROWNE, Niall
00549113 HANCOCK, Gary
00549113 HANCOCK, Jayne
00549113 WALSH, Patrick Martin
03161873 HAYTON, Antony
03161873 HAYTON, Jane
03161873 TRAVES, Alec
03161873 TRAVES, Joseph
03161873 TRAVES, June
03161873 TRAVES, Karen

Code Name 1 Name 2 Name 3 Name 4 Name 5 Name 6
01404360 BOOTH, Adrian LEWIS, David Alexander NICHOLSON, Leslie
11772984 BELLIS, Tom Arthur
07277291 OSBORNE, Leigh Ross OSBORNE, Maxine TYLER, Gregory Stuart
05052644 PERKINS, Antony James PERKINS, Nicola Jane
01461813 CANNON, Hugo James CANNON, Robert James DAWSON, Peter Graham SHROSBREE, Richard James
04299559 DEFFLEY, Anthony Francis DEFFLEY, Steven Francis
00549113 BROWNE, Niall HANCOCK, Gary HANCOCK, Jayne WALSH, Patrick Martin
03161873 HAYTON, Antony HAYTON, Jane TRAVES, Alec TRAVES, Joseph TRAVES, June TRAVES, Karen

sp_AnswersTopicSeeAnswer See Answer
Avatar
Velouria
London or thereabouts
Moderator
Members


Trusted Members

Moderators
Level 4
Forum Posts: 687
Member Since:
November 1, 2018
sp_UserOfflineSmall Offline
2
May 26, 2023 - 3:15 am
sp_Permalink sp_Print sp_EditHistory

Yes, you can use something like this:

let
Source = Excel.CurrentWorkbook(){[Name="Table2"]}[Content],
#"Grouped Rows" = Table.Group(Source, {"Code"}, {{"Data", each Table.AddIndexColumn(_,"Index",1,1), type table [Code=number, Name=text]}}),
#"Removed Columns" = Table.RemoveColumns(#"Grouped Rows",{"Code"}),
#"Expanded Data" = Table.ExpandTableColumn(#"Removed Columns", "Data", {"Code", "Name","Index"}, {"Code", "Name", "Index"}),
#"Pivoted Column" = Table.Pivot(Table.TransformColumnTypes(#"Expanded Data", {{"Index", type text}}, "en-GB"), List.Distinct(Table.TransformColumnTypes(#"Expanded Data", {{"Index", type text}}, "en-GB")[Index]), "Index", "Name")
in
#"Pivoted Column"

It basically groups on the code, adding an index column to the table created for each code, then expands that table and pivots using the added index column.

Avatar
Alan Sidman
Steamboat Springs, CO
Member
Members


Trusted Members
Level 0
Forum Posts: 174
Member Since:
October 18, 2018
sp_UserOfflineSmall Offline
3
May 26, 2023 - 6:36 am
sp_Permalink sp_Print

Rory,
When I tried your Mcode, it errored looking for the Index Column that you created in the Group By.

I separated those steps and this worked for me.

let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Grouped Rows" = Table.Group(Source, {"Code"}, {{"Data", each _, type table [Code=number, Name=text]}}),
#"Added Custom" = Table.AddColumn(#"Grouped Rows", "Custom", each Table.AddIndexColumn([Data], "Index",1,1)),
#"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Data"}),
#"Expanded Custom" = Table.ExpandTableColumn(#"Removed Columns", "Custom", {"Name", "Index"}, {"Name", "Index"}),
#"Pivoted Column" = Table.Pivot(Table.TransformColumnTypes(#"Expanded Custom", {{"Index", type text}}, "en-US"), List.Distinct(Table.TransformColumnTypes(#"Expanded Custom", {{"Index", type text}}, "en-US")[Index]), "Index", "Name")
in
#"Pivoted Column"

sp_AnswersTopicAnswer
Answers Post
Avatar
Velouria
London or thereabouts
Moderator
Members


Trusted Members

Moderators
Level 4
Forum Posts: 687
Member Since:
November 1, 2018
sp_UserOfflineSmall Offline
4
May 26, 2023 - 4:51 pm
sp_Permalink sp_Print

HI Alan,

That's odd - I just tried it again and it worked as posted (64bit 365). Would you mind testing this slight alteration?

let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Grouped Rows" = Table.Group(Source, {"Code"}, {{"Data", each Table.AddIndexColumn(_,"Index",1,1), type table [Code=number, Name=text, Index=number]}}),
#"Removed Columns" = Table.RemoveColumns(#"Grouped Rows",{"Code"}),
#"Expanded Data" = Table.ExpandTableColumn(#"Removed Columns", "Data", {"Code", "Name","Index"}, {"Code", "Name", "Index"}),
#"Pivoted Column" = Table.Pivot(Table.TransformColumnTypes(#"Expanded Data", {{"Index", type text}}, "en-GB"), List.Distinct(Table.TransformColumnTypes(#"Expanded Data", {{"Index", type text}}, "en-GB")[Index]), "Index", "Name")
in
#"Pivoted Column"

Avatar
Robert Green
Member
Members
Level 0
Forum Posts: 25
Member Since:
November 9, 2020
sp_UserOfflineSmall Offline
5
May 27, 2023 - 1:24 am
sp_Permalink sp_Print

Thank you, both.
I did find that Rory's solutions worked but omitted the code in column 1, but Alan's solution kept the code in column 1.

I now need to figure out how it works !!

Thank you both, again.

Avatar
Alan Sidman
Steamboat Springs, CO
Member
Members


Trusted Members
Level 0
Forum Posts: 174
Member Since:
October 18, 2018
sp_UserOfflineSmall Offline
6
May 27, 2023 - 5:08 pm
sp_Permalink sp_Print

Rory
Modified Mcode worked perfectly
Alan

sp_Feed
Go to top
Forum Timezone: Australia/Brisbane
Most Users Ever Online: 245
Currently Online: Marcel Bila, Shawn Ward, Nicholas Meyer, Andrew Mansour, Sheryl Gill, Rick Scott, John DiMatteo, Rob Starren, Leigh Thomas
Guest(s) 11
Currently Browsing this Page:
1 Guest(s)
Top Posters:
SunnyKow: 1432
Anders Sehlstedt: 880
Purfleet: 414
Frans Visser: 346
David_Ng: 306
lea cohen: 237
Jessica Stewart: 219
A.Maurizio: 213
Aye Mu: 201
jaryszek: 183
Newest Members:
Sheilah Taylor
Clare Webber
David Jenssen
Dominic Brosnahan
Young You
Jennifer Owens
Mohamed Touahria
Sheila McCall
Nicholas Montano
John Babcock
Forum Stats:
Groups: 3
Forums: 24
Topics: 6524
Posts: 28551

 

Member Stats:
Guest Posters: 49
Members: 32804
Moderators: 2
Admins: 4
Administrators: Mynda Treacy, Philip Treacy, Catalin Bombea, FT
Moderators: Velouria, Riny van Eekelen
© Simple:Press —sp_Information

Sidebar

Blog Categories

  • Excel
  • Excel Charts
  • Excel Dashboard
  • Excel Formulas
  • Excel Office Scripts
  • Excel PivotTables
  • Excel Shortcuts
  • Excel VBA
  • General Tips
  • Online Training
  • Outlook
  • Power Apps
  • Power Automate
  • Power BI
  • Power Pivot
  • Power Query
microsoft mvp logo
trustpilot excellent rating
Secured by Sucuri Badge
MyOnlineTrainingHub on YouTube Mynda Treacy on Linked In Mynda Treacy on Instagram Mynda Treacy on Twitter Mynda Treacy on Pinterest MyOnlineTrainingHub on Facebook

Sign up to our newsletter and join over 400,000
others who learn Excel and Power BI with us.

 

Company

  • About My Online Training Hub
  • Disclosure Statement
  • Frequently Asked Questions
  • Guarantee
  • Privacy Policy
  • Terms & Conditions
  • Testimonials
  • Become an Affiliate
  • Sponsor Our Newsletter

Support

  • Contact
  • Forum
  • Helpdesk - For Technical Issues

Copyright © 2023 · My Online Training Hub · All Rights Reserved. Microsoft and the Microsoft Office logo are trademarks or registered trademarks of Microsoft Corporation in the United States and/or other countries. Product names, logos, brands, and other trademarks featured or referred to within this website are the property of their respective trademark holders.