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

Get certified in Microsoft Fabric—for free! For a limited time, get a free DP-600 exam voucher to use by the end of 2024. Register now

Reply
Anonymous
Not applicable

PowerQuery in Excel - adding columns based on a List.

Hi all,

I have a question.
So Let say I have Table with any data,
then I have a list with Columns I wish to add:

{"NewCol1", "NewCol2", "NewCol3"}
the list is manualy verified (  what ccan you do? eh... )

so Now I need to add thoose columnsto MyTable.

any help would be appriciead

5 REPLIES 5
Anonymous
Not applicable

ok, let me provide a simple example.
Let say I have a Table:
amount in local Currency

exchange rate 2
exchange rate 3
some other columns.

and now my Column list:
NewColumnExRate1
NewColumnExRate2
NewColumnExRate2

Calculations:
if columns do not exists in original table:
NewColumnExRate1 = amount in local Currency * exchange rate 1

NewColumnExRate2 = amount in local Currency * exchange rate 2
NewColumnExRate3 = amount in local Currency * exchange rate 3

this is either / or logic,
either all of them will exists or none of them, so there will be no need to do conditional addding of columns.

I tried using @AlienSx solution but Igot defeated and could not implement addding columns with calculations, just empty ones.

@Anonymous this checks if one of column names from your list is already there. If not then all 3 columns are added

    rates = 
        [NewColumnExRate1 = each [amount in local Currency] * [exchange rate 1],
        NewColumnExRate2 = each [amount in local Currency] * [exchange rate 2],
        NewColumnExRate3 = each [amount in local Currency] * [exchange rate 3]],
    add_columns = 
        if List.Contains(Table.ColumnNames(MyTable), "NewColumnExRate3") 
        then MyTable 
        else List.Accumulate(Record.FieldNames(rates), MyTable, (s, c) => Table.AddColumn(s, c, Record.Field(rates, c)))
wdx223_Daniel
Super User
Super User

this code will add three null columns

=Table.SelectColumns(MyTable,Table.ColumnNames(MyTable)&{"NewCol1", "NewCol2", "NewCol3"},2)

AlienSx
Super User
Super User

Hi, @Anonymous 

List.Accumulate(your_list, MyTable, (s, c) => Table.AddColumn(s, c, each null))
JoeBarry
Solution Sage
Solution Sage

Hi @Anonymous 

 

You will need to provide more information,

 

What data is in the original table?

What data is in the new columns and how are they related to the original table?

 

Thanks

Joe

Helpful resources

Announcements
November Carousel

Fabric Community Update - November 2024

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

Live Sessions with Fabric DB

Be one of the first to start using Fabric Databases

Starting December 3, join live sessions with database experts and the Fabric product team to learn just how easy it is to get started.

Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Early Bird pricing ends December 9th.

Nov PBI Update Carousel

Power BI Monthly Update - November 2024

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