Power query or M to convert table headers

Hi,

I’m looking for direction on how to convert headers of a data table automatically. These headers from the import file will always be consistent. Rather than manually change the headers each time when I import the data, I’m looking to create a header converter script using a parameter.

My sample file contains five fields and I want to convert them to the below layout.

image

Sample.pbix (27.1 KB)

Thank you,

Tony

Hi @rizzotony,

Implement this into your quey OR turn it into a function that you can invoke on your table.
Note. To implement in your query you’ll have to change the myTable into a step name

Table.RenameColumns( myTable, 
  List.Zip(
    {
      Table.ColumnNames( myTable ), 
      {"Branch", "Business or Retail Flag", "Household Key", "Individual Key", "Account Status"}
    }
  )
)

Here’s your sample.
Sample.pbix (33.1 KB)

I hope this is helpful

2 Likes

Thanks Melissa,

I’m trying to understand how the code knows to change the original column name to the new column name. For example, how does it know to change BIZRETL to Business or Retail.

I follow your code but am missing that logic. I’m sure it’s clear, but I’m new to using M.

I’ll use your example and study more.

Thank you so much for your assistance.

Tony

Well it actually doesn’t ‘know’ or even tests wether the column name is a match. Since you’ve stated that: these headers from the import file will always be consistent. All it is doing now - is replacing a column name based on its position.

However if that is relevant you can incorporate the depicted ‘translation’ table into the model and leverage that (note that you didn’t supply it in your sample PBIX).