Forum Discussion
Nickda0123
3 years agoNew Member
Transform columns whose data type is "List"
I have a table with 100 columns In which there are 40 columns (which data type is "list") need to be Transform by this code: List.Transform(list_column_names_need_transform, (columnName) => T...
- 3 years ago
Here's one way to do it in the query editor. To see how it works, just create a blank query, open the Advanced Editor and replace the text there with the M code below.
let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WclTSUXJWitWJVnICslyUYmMB", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t, Column2 = _t]), #"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", type text}, {"Column2", type text}}), #"Added Custom" = Table.AddColumn(#"Changed Type", "Column3", each {"E", "F", "G"}), #"Added Custom1" = Table.AddColumn(#"Added Custom", "Column4", each {"H", "I", "J"}), #"Added Custom2" = Table.AddColumn(#"Added Custom1", "RecordColumn", each _), #"Removed Other Columns" = Table.SelectColumns(#"Added Custom2",{"RecordColumn"}), #"Added Custom3" = Table.AddColumn(#"Removed Other Columns", "Custom", each let fieldnames = Record.FieldNames([RecordColumn]), updatedlist = List.Transform(Record.ToList([RecordColumn]), each try Text.Combine(_, ",") otherwise _), newrecord = Record.FromList(updatedlist, fieldnames) in newrecord), #"Removed Other Columns1" = Table.SelectColumns(#"Added Custom3",{"Custom"}), #"Expanded Custom" = Table.ExpandRecordColumn(#"Removed Other Columns1", "Custom", {"Column1", "Column2", "Column3", "Column4"}, {"Column1", "Column2", "Column3", "Column4"}) in #"Expanded Custom"Pat
ppm1
3 years agoSolution Sage
Here's one way to do it in the query editor. To see how it works, just create a blank query, open the Advanced Editor and replace the text there with the M code below.
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WclTSUXJWitWJVnICslyUYmMB", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t, Column2 = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", type text}, {"Column2", type text}}),
#"Added Custom" = Table.AddColumn(#"Changed Type", "Column3", each {"E", "F", "G"}),
#"Added Custom1" = Table.AddColumn(#"Added Custom", "Column4", each {"H", "I", "J"}),
#"Added Custom2" = Table.AddColumn(#"Added Custom1", "RecordColumn", each _),
#"Removed Other Columns" = Table.SelectColumns(#"Added Custom2",{"RecordColumn"}),
#"Added Custom3" = Table.AddColumn(#"Removed Other Columns", "Custom", each let
fieldnames = Record.FieldNames([RecordColumn]),
updatedlist = List.Transform(Record.ToList([RecordColumn]), each try Text.Combine(_, ",") otherwise _),
newrecord = Record.FromList(updatedlist, fieldnames)
in
newrecord),
#"Removed Other Columns1" = Table.SelectColumns(#"Added Custom3",{"Custom"}),
#"Expanded Custom" = Table.ExpandRecordColumn(#"Removed Other Columns1", "Custom", {"Column1", "Column2", "Column3", "Column4"}, {"Column1", "Column2", "Column3", "Column4"})
in
#"Expanded Custom"
Pat
Nickda0123
3 years agoNew Member
Thank you very much, this is what I want to find!