Forum

How to combine two ...
 
Notifications
Clear all

How to combine two columns (List) into one single column (list1 + List2)

8 Posts
3 Users
0 Reactions
551 Views
(@bluesky63)
Posts: 162
Estimable Member
Topic starter
 

Hi PQ Query,

I tried to apply List.Combine () but the 2nd list append to the first list,  and I wanted the column 1 and column 2 to be side by side in a field and when I expand,  it should be correspond to each other (correct me if I am wrong)

let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
Custom1 = Table.TransformColumns( Source,
{{"Name", each Text.Split(_, ",")},
{"Department", each Text.Split(_, ",")}
}
),
#"Added Custom" = Table.AddColumn(Custom1, "Custom", each List.Combine({[Name],[Department]}))
in
#"Added Custom"

Thank you very much !

 
Posted : 10/06/2022 4:04 am
Riny van Eekelen
(@riny)
Posts: 1188
Member Moderator
 

After splitting, transpose the table and then Table.FromColumns.

See attached in the Table1 query.

 
Posted : 10/06/2022 4:54 am
(@bluesky63)
Posts: 162
Estimable Member
Topic starter
 

Hi Riny,

thanks for your quick response,  my sample is only one record (original source got few hundred),   and how to handle multiple records using your Mcode

Note: added a second record in the attachment

thank you Sir

 
Posted : 10/06/2022 5:21 am
Riny van Eekelen
(@riny)
Posts: 1188
Member Moderator
 

Then I would opt for creating two separate tables. One for Names and one for Departments and merge them on the basis of an index column. Perhaps not the most sophisticated solution, but it works. See the Merge1 query attached.

 
Posted : 10/06/2022 5:51 am
Riny van Eekelen
(@riny)
Posts: 1188
Member Moderator
 

Added another possible solution. See Custom2 query in the attached file.

 
Posted : 10/06/2022 6:07 am
(@debaser)
Posts: 836
Member Moderator
 

You could also use List.Zip and then Table.FromRows:

 

let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
Custom1 = Table.TransformColumns( Source, {{"Name", each Text.Split(_, ",")}, {"Department", each Text.Split(_, ",")}}),
#"Added Custom" = Table.AddColumn(Custom1, "Custom", each List.Zip({[Name], [Department]})),
#"Added Custom1" = Table.AddColumn(#"Added Custom", "Custom.1", each Table.FromRows([Custom])),
#"Expanded Custom.1" = Table.ExpandTableColumn(#"Added Custom1", "Custom.1", {"Column1", "Column2"}, {"Custom.1.Column1", "Custom.1.Column2"}),
#"Removed Columns" = Table.RemoveColumns(#"Expanded Custom.1",{"Name", "Department", "Custom"}),
#"Trimmed Text" = Table.TransformColumns(#"Removed Columns",{{"Custom.1.Column1", Text.Trim, type text}, {"Custom.1.Column2", Text.Trim, type text}})
in
#"Trimmed Text"

 
Posted : 10/06/2022 7:05 am
(@bluesky63)
Posts: 162
Estimable Member
Topic starter
 

Hi Riny,

Thank you for your time,

how to distribute the ID correctly to each Name+Department ?

Sorry I forgotten about the unique identifier

 

Thank you !

 
Posted : 10/06/2022 7:38 am
(@bluesky63)
Posts: 162
Estimable Member
Topic starter
 

HI Velouria

List.Zip is cool and it works even with ID.

Thanks

 

Hi Riny : -   Thank you for your solution !

 
Posted : 10/06/2022 8:00 am
Share: