Power Query Add New Columns According Qty of Phone Numbers

Jose Fonyat

New member
Joined
Aug 30, 2017
Messages
13
Reaction score
0
Points
0
Location
Brazil
Excel Version(s)
Microsoft 365 Apps Versao 2007
Hello Guys!

Let me know if you could help me with this table below:


IdNamePhone NumberIndex
7-100052John8523-11221
7-100052John9523-22332
4-150194Richard7532-11111
4-150194Richard8537-10202
4-150194Richard9321-66773

My goal is to add new columns named Phone Number 1, Phone Number 2 or Phone Number N as many phones as unique Ids we have and show their respective phone numbers.

IdNamePhone NumberIndexPhone 1Phone 2Phone 3
7-100052John8523-112218523-11229523-2233null
7-100052John9523-223328523-11229523-2233null
4-150194Richard7532-111117532-11118537-10209321-6677
4-150194Richard8537-102027532-11118537-10209321-6677
4-150194Richard9321-667737532-11118537-10209321-6677

After that I would just remove the duplicated rows.

Any help it would be very appreciated

Best Regards
 
Code:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Id", type text}, {"Name", type text}, {"Phone Number", type text}, {"Index", Int64.Type}}),
    #"Pivoted Column" = Table.Pivot(Table.TransformColumnTypes(#"Changed Type", {{"Index", type text}}, "en-US"), List.Distinct(Table.TransformColumnTypes(#"Changed Type", {{"Index", type text}}, "en-US")[Index]), "Index", "Phone Number")
in
    #"Pivoted Column"

Excel 2016 (Windows) 32 bit
A
B
C
D
E
1
IdName123
2
4-150194Richard7532-11118537-10209321-6677
3
7-100052John8523-11229523-2233
Sheet: Sheet2
 
Very easy!

M Code:

Code:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Id", type text}, {"Name", type text}, {"Phone Number", type text}, {"Index", Int64.Type}}),
    #"Added Prefix" = Table.TransformColumns(#"Changed Type", {{"Index", each "Phone " & Text.From(_, "en-GB"), type text}}),
    #"Reordered Columns" = Table.ReorderColumns(#"Added Prefix",{"Id", "Name", "Index", "Phone Number"}),
    #"Pivoted Column" = Table.Pivot(#"Reordered Columns", List.Distinct(#"Reordered Columns"[Index]), "Index", "Phone Number")
in
    #"Pivoted Column"

Excel 2016 (Windows) 32 bit
F
G
H
I
J
1
IdNamePhone 1Phone 2Phone 3
2
4-150194Richard7532-11118537-10209321-6677
3
7-100052John8523-11229523-2233
Sheet: Sheet1

Let me know if you need greater explanation.
 

Attachments

  • PQ Phone List Pivot AliGW.xlsx
    17.4 KB · Views: 10
Back
Top