Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
jct999
Advocate II
Advocate II

[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 table with new columns. The added columns should be created if they do not already exist in the input table. Their types should be Text, and values should be null.


How can I do that ?
(My thought is that using List.Accumulate might be a good starting point... but can't see how to go further)

 

 

Thanks
Regards

1 ACCEPTED SOLUTION

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

View solution in original post

5 REPLIES 5
Syndicate_Admin
Administrator
Administrator

How many NULL values do you need in each row of the columns? Can you take the column and pivot it, to make the list into columns?

Izzii0x_0-1631614475118.png

I added a custom column which is just blank (null)

Izzii0x_3-1631614666892.png

 

Then hit "pivot column" whilst having the List highlighted:

Izzii0x_2-1631614599119.png

And then I get the following:

Izzii0x_4-1631614700631.png

 

 

It doesn't quite get you 'NULL' but gets the column names, and then adding new rows to that list will automatically add new columns.

 

 

I am confused, did you just copy my answer over or were you trying to reply to it?

Izzii0x
Frequent Visitor

How many NULL values do you need in each row of the columns? Can you take the column and pivot it, to make the list into columns?

Izzii0x_0-1631614475118.png

I added a custom column which is just blank (null)

Izzii0x_3-1631614666892.png

 

Then hit "pivot column" whilst having the List highlighted:

Izzii0x_2-1631614599119.png

And then I get the following:

Izzii0x_4-1631614700631.png

 

 

It doesn't quite get you 'NULL' but gets the column names, and then adding new rows to that list will automatically add new columns.

 

 

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

Hi, I am trying to add value to the added column based on a condition. But it's failing with the message: "Expression.Error: The name 'current_table' wasn't recognized. Make sure it's spelled correctly."  Would you please provide some insight?

 

let myFunc = ( myTable, myNewColumnList, V1) =>

let foo = List.Accumulate(

myNewColumnList,
myTable,
(current_table, new_name) =>

if List.Contains(Table.ColumnNames(current_table), new_name)

then current_table
else

let
condition = if new_name = V1 then V1 else "excluded"
in condition ,

Table.AddColumn(current_table, new_name, condition)
)

in foo

in myFunc

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.

Top Solution Authors
Top Kudoed Authors