How do I dynamically rename multiple columns in Power Query based on a mapping table

0 votes

How do I dynamically rename multiple columns in Power Query based on a mapping table?
I am working on a Power BI project where I need to dynamically rename multiple columns in Power Query based on a mapping table. The mapping table contains the original column names and their corresponding new names. What is the best approach to automate this renaming process using M code?

1 day ago in Power BI by Evanjalin
• 24,110 points
38 views

1 answer to this question.

0 votes

To dynamically rename multiple columns in Power Query based on a mapping table, you can use the following approach with M code. The mapping table will contain the original column names and their new names, and you will loop through the columns to rename them based on the mapping.

Here’s a step-by-step method:

Steps:

  1. Load the Mapping Table
    Ensure that your mapping table contains two columns: OriginalName and NewName. This table will map the old column names to the new ones.

  2. Transform the Data Table
    Use the Table.RenameColumns function along with a List.Transform to apply the renaming dynamically.

M Code Example:

let
    // Step 1: Load your data and the mapping table
    Source = YourDataSource,  // Replace with your actual data source
    MappingTable = YourMappingTable,  // Replace with your mapping table

    // Step 2: Create a list of column renaming rules based on the mapping table
    RenameRules = List.Transform(
        Table.ToRecords(MappingTable), 
        each { [OriginalName = [OriginalName], NewName = [NewName]] }
    ),

    // Step 3: Rename columns based on the mapping table
    RenamedColumns = Table.RenameColumns(Source, RenameRules)
in
    RenamedColumns

Explanation:

  • Source: This is the table you want to apply the column renaming to (your data table).

  • MappingTable: This is the table with the original column names (OriginalName) and the corresponding new names (NewName).

  • RenameRules: This step generates a list of rules for renaming by converting the mapping table into records and applying them to the Table.RenameColumns function.

  • RenamedColumns: This step applies the renaming process to the data table using the dynamically created rules.

answered 1 day ago by anonymous
• 24,110 points

Related Questions In Power BI

0 votes
1 answer

How can I create a dynamic grouping in Power Query where the group sizes vary based on another column’s values?

You can use these innovative techniques in ...READ MORE

answered Mar 17 in Power BI by anonymous
• 24,110 points
84 views
0 votes
0 answers
0 votes
2 answers

How do I create a Power BI visual that dynamically adjusts based on user-selected filters and slicers?

The application of slicers and filters in ...READ MORE

answered Jan 23 in Power BI by anonymous
• 21,850 points
162 views
0 votes
1 answer

How can I unpivot multiple columns dynamically while keeping other columns unchanged in Power Query?

To dynamically unpivot multiple columns in Power ...READ MORE

answered Mar 11 in Power BI by anonymous
• 24,110 points
90 views
0 votes
1 answer

Install Power BI Desktop

It’s a pretty simple process. All you ...READ MORE

answered Oct 9, 2018 in Power BI by Kalgi
• 52,350 points
1,221 views
0 votes
1 answer

Few tips before I start creating Power BI dashboard

It’s always advisable to begin with the data ...READ MORE

answered Oct 9, 2018 in Power BI by Kalgi
• 52,350 points
1,039 views
0 votes
1 answer

How do I format the KPI in Power BI

format the KPI by selecting the paint ...READ MORE

answered Oct 9, 2018 in Power BI by Kalgi
• 52,350 points
1,388 views
+1 vote
1 answer
0 votes
1 answer

How do I dynamically switch measures in Power BI based on a slicer selection while maintaining performance?

To dynamically switch between different measures in ...READ MORE

answered Mar 10 in Power BI by anonymous
• 24,110 points
64 views
0 votes
1 answer

How do I create a Power Query function that dynamically adjusts based on input parameters?

If you want to build a Power ...READ MORE

answered Mar 13 in Power BI by anonymous
• 24,110 points
68 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP