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

Grow your Fabric skills and prepare for the DP-600 certification exam by completing the latest Microsoft Fabric challenge.

Reply
Goodkat
Helper I
Helper I

Congruent Header Rename based on List

Dear Power Query enthusiasts,

 

Today I worked on the topic of automated header renaming in power query. And now I am struggling with the last step. I have a query on which the header names are incorrect. At the same time I have a list were I know the headers are correct. Both are congruent, i.e. Header for column 1 sits in [mdHeader]row 1, column 2 = row 2 and so forth.

Which command I would need to implement into the Table.RenameColumns command to achieve this? I searched the internet intensively and watched numerous videos over the past hours, but none could give me a clue how to perform this 'transposed copy rename' operation in one commandline in [Header] query.

 

Any idea from the community will be greatly appreciated!

 

https://c.gmx.net/@324888734501700174/6aAbqAZdQkKdWVkovk3c-w

 

Best regards, Andreas

1 ACCEPTED SOLUTION
jennratten
Super User
Super User

Hello!  Here is how you can accomplish this.

If you have a table of data:

jennratten_4-1702909433105.png

And a table of column headers:

jennratten_1-1702909025612.png

You can rename them like so (for ease of understanding I have broken the actions into separate steps, but these can be combined into one step if preferred).  I have included two options - one for getting the old names from the source data and the other for getting the old names from the table which also contains the new names.

jennratten_2-1702909258273.png

After the renaming is applied:

jennratten_3-1702909404174.png

 

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WctRTcEksKc1VcM4vKsgvSizJzM9TUNAI1QvW01TSUfJzVorViVbyBcoU6Si4FyVmJOYqJOalKLhkJmen5hUDlfi6KMXGAgA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Account Name" = _t, #"State or Province" = _t]),
    FieldNames = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WckxOzi/NK1HwS8xNVdKBcZVidaKVgksSS1IV8osUAoryyzLzkkHyYDGl2FgA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [old = _t, new = _t]),

    // list of old names if they are coming from the source data.
    varOldNames = Table.ColumnNames ( Source ),

    // list of old names if they are in a table with the new names, aka a data map.
    // varOldNames = Table.Column ( FieldNames, "old" ),

    // list of new names
    varNewNames = Table.Column ( FieldNames, "new" ),

    // zip the old names and new names together into a list.
    OldNewList = List.Zip ( 
        {
            varOldNames, varNewNames
        }
    ),

    // rename the columns in the table using the zipped list.
    RenameColumns = Table.RenameColumns ( Source, OldNewList )
in
    RenameColumns

 

 

View solution in original post

2 REPLIES 2
jennratten
Super User
Super User

Hello!  Here is how you can accomplish this.

If you have a table of data:

jennratten_4-1702909433105.png

And a table of column headers:

jennratten_1-1702909025612.png

You can rename them like so (for ease of understanding I have broken the actions into separate steps, but these can be combined into one step if preferred).  I have included two options - one for getting the old names from the source data and the other for getting the old names from the table which also contains the new names.

jennratten_2-1702909258273.png

After the renaming is applied:

jennratten_3-1702909404174.png

 

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WctRTcEksKc1VcM4vKsgvSizJzM9TUNAI1QvW01TSUfJzVorViVbyBcoU6Si4FyVmJOYqJOalKLhkJmen5hUDlfi6KMXGAgA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Account Name" = _t, #"State or Province" = _t]),
    FieldNames = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WckxOzi/NK1HwS8xNVdKBcZVidaKVgksSS1IV8osUAoryyzLzkkHyYDGl2FgA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [old = _t, new = _t]),

    // list of old names if they are coming from the source data.
    varOldNames = Table.ColumnNames ( Source ),

    // list of old names if they are in a table with the new names, aka a data map.
    // varOldNames = Table.Column ( FieldNames, "old" ),

    // list of new names
    varNewNames = Table.Column ( FieldNames, "new" ),

    // zip the old names and new names together into a list.
    OldNewList = List.Zip ( 
        {
            varOldNames, varNewNames
        }
    ),

    // rename the columns in the table using the zipped list.
    RenameColumns = Table.RenameColumns ( Source, OldNewList )
in
    RenameColumns

 

 

Dear Jenn,

Thank you so much! How cool! It works! And so crisp & clear! I just ran a test in my original data with all of the commands merged into one line as you suggested. And it does the trick! I will put this solution into my little Power Query learning compilation.

Have a good day, or rest of the day, and a joyful upcoming holiday season!

 

Many greetings, Andreas

Helpful resources

Announcements
RTI Forums Carousel3

New forum boards available in Real-Time Intelligence.

Ask questions in Eventhouse and KQL, Eventstream, and Reflex.

MayPowerBICarousel

Power BI Monthly Update - May 2024

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

Top Solution Authors
Top Kudoed Authors