cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
vancromy Frequent Visitor
Frequent Visitor

PQ - Rename Column Names based of column names from another table

Hi.

 

I'm sure this is really simple but I can't seem to find the answer online. Many queries are about translating rows to Column Names but I'm specifically after how I can make the column names of my table equal to the ones of another table. I know this is dangerous because it requires the columns to be in the right order and no new columns to appear or old ones to dissapear but I will gladly take that risk on. 

 

Table 1: 

Company          Date            Sales

Bob Co       27/04/2018          20

 

Table 2: 

A                        B                    C

Bob Co       27/04/2016          30

 

I would like Table 2 to inherit Table 1's column names. I can't just hardcode it because the columnames in Table 1 can change.

 

I've tried the following but got this error: Expression.Error: We expected a RenameOperations value.

#"Rename Columns" = Table.RenameColumns(Table2, Table.ColumnNames(Table1))

 

Also tried

#"Renamed Columns" = Table.RenameColumns(Table2, Table.ToList(#table({"OldNames","NewNames"},{{Table.ColumnNames(Table2)},{Table.ColumnNames(Table1)}})))

 

but got this error: Expression.Error: 2 keys were specified, but 1 values were provided.

 

Any help would be much appreciated

Y

 

1 ACCEPTED SOLUTION

Accepted Solutions
Super User
Super User

Re: PQ - Rename Column Names based of column names from another table

You were pretty close already:

 

#"Rename Columns" = Table.RenameColumns(Table2, List.Zip( { Table.ColumnNames(Table2), Table.ColumnNames(Table1) } ) )

 

Did I answer your question? Mark my post as a solution!

Proud to be a Datanaut!

Imke Feldmann

How to integrate M-code into your solution -- Check out more PBI- learning resources here -- Performance Tipps for M-queries




5 REPLIES 5
Super User
Super User

Re: PQ - Rename Column Names based of column names from another table

Dealing with multiple tables in a query is tricky outside of a Merge or Append but @ImkeF might have a trick for it.


I have book! Learn Power BI from Packt


Did I answer your question? Mark my post as a solution!

Proud to be a Datanaut!

Super User
Super User

Re: PQ - Rename Column Names based of column names from another table

You were pretty close already:

 

#"Rename Columns" = Table.RenameColumns(Table2, List.Zip( { Table.ColumnNames(Table2), Table.ColumnNames(Table1) } ) )

 

Did I answer your question? Mark my post as a solution!

Proud to be a Datanaut!

Imke Feldmann

How to integrate M-code into your solution -- Check out more PBI- learning resources here -- Performance Tipps for M-queries




vancromy Frequent Visitor
Frequent Visitor

Re: PQ - Rename Column Names based of column names from another table

Thanks both! That's done the trick and I've learnt about a new PQ function in the process Smiley Happy

Luthiendsa Occasional Visitor
Occasional Visitor

Re: PQ - Rename Column Names based of column names from another table

Hi @ImkeF,

 

Where do I put the " #"Rename Columns" = Table.RenameColumns(Table2, List.Zip( { Table.ColumnNames(Table2), Table.ColumnNames(Table1) } ) ) "?

I am a beginner in Power BI and I really could use the solution you gave.

 

Super User
Super User

Re: PQ - Rename Column Names based of column names from another table

Hi , @Luthiendsa ,

did you check the link from my signature already?: http://community.powerbi.com/t5/Webinars-and-Video-Gallery/Power-BI-Forum-Help-How-to-integrate-M-co...

Please let me if that makes sense or you need further advise.

Did I answer your question? Mark my post as a solution!

Proud to be a Datanaut!

Imke Feldmann

How to integrate M-code into your solution -- Check out more PBI- learning resources here -- Performance Tipps for M-queries




Helpful resources

Announcements
Virtual Launch Event

Microsoft Business Applications October Virtual Launch Event

Join us for an in-depth look at the new innovations across Dynamics 365 and the Microsoft Power Platform.

Power BI Helps Homeless and Trouble Youth

Power BI Helps Homeless and Trouble Youth

We spoke with Power BI Super User, Greg Deckler, about his charity work

MBAS Gallery

Watch Sessions On Demand!

Continue your learning in our online communities.

Users Online
Currently online: 235 members 2,622 guests
Please welcome our newest community members: