Forum Discussion

Nickda0123's avatar
Nickda0123
New Member
3 years ago
Solved

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...
  • ppm1's avatar
    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