Power Query add multiple empty colums?

Viewed 626

I need to add 12 empty columns in Power Query with custom names. Now I am adding one column at the time and change the name.

Is it possible to do this faster/better?

I tried the first option but get an error. When I add a column with code

= Table.FromColumns(
Table.ToColumns(#"Prev Step") & {{null}, {null}, {null}}, 
Table.ColumnNames(#"Prev Step") & {"Empty1", "Empty2", "Empty3"}

)

I get a lot of rows in the three columns. What am I doing wrong? Faulty output

Desired output

2 Answers

How about this for adding three empty columns? Extend to more as needed.

= Table.FromColumns(
    Table.ToColumns(#"Prev Step") & {{null}, {null}, {null}}, 
    Table.ColumnNames(#"Prev Step") & {"Empty1", "Empty2", "Empty3"}
  )

By generating lists of columns and associated names, you can specify just the number of columns to add, and let the list of nulls and names be generated automatically. eg:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WSkksSVQwVIrVgTKNlGJjAQ==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", type text}}),

//add Multiple Blank Columns
    numCols = 12,
    colNames = List.Generate(
        ()=>[colName = "Blank", idx=0],
            each [idx] < numCols,
            each [colName = "Blank" & Number.ToText([idx]+1), idx = [idx]+1],
            each [colName]
        ),

    addedCols = Table.FromColumns(
                    Table.ToColumns(#"Changed Type") & List.Repeat({{null}},numCols),
                    Table.ColumnNames(#"Changed Type") & colNames)
in
    addedCols
Related