Forum Discussion

jct999's avatar
jct999
Advocate II
4 years ago
Solved

[PowerQuery] Function to add columns based on a list of column names

Hello everyone, I would like to create a function that do the following : Given 2 parameters : - A table - A list of name for new columns. Ex: {"COUNTRY", "CUSTOMER_NAME"} Return : The input...
  • jct999's avatar
    jct999
    4 years ago

    Here is the solution I found. (May be helpfull for others)

     

    let myFunc = ( myTable, myNewColumnList) =>

    let foo = List.Accumulate(

    myNewColumnList,
    myTable,
    (current_table, new_name) =>

    if List.Contains(Table.ColumnNames( current_table), new_name)
    then current_table
    else Table.AddColumn(current_table, new_name, each null)

    )

    in foo

    in myFunc