How to Rename Dynamic Excel Columns by Week Number in Power Query
Question details
The user needs to rename dynamically changing column headers (such as specific week numbers) in Excel reports without relying on fixed column positions.

- Product
- Microsoft Excel
- Device & OS
- not provided
- Scenario
- Processing weekly reports where column orders fluctuate, making positional references break the query when new data is imported.
- Observed behavior
- Positional and hardcoded references fail when the column order changes, requiring a text-based identification method to reliably rename or extract headers containing week numbers.
Ensure your weekly report data is formatted as an Excel Table and successfully loaded into the Power Query Editor before applying transformation steps.
Use Power Query M Code to Dynamically Rename Columns
Instead of relying on column position, use Power Query's advanced editor to identify and rename columns based on their header text.
When dealing with weekly reports where column names change (e.g., Week 7 to Week 8) or their order shifts, relying on standard UI renaming steps will hardcode the exact column name or position. To make your query robust, you can use M code to search for specific text patterns like 'Week' and rename them dynamically.
Open your query in the Power Query Editor. Go to the Home tab and click 'Use First Row as Headers' to ensure your week numbers are recognized as the active column names.
Navigate to the Home tab and select 'Advanced Editor' to view the underlying M code of your current query steps.
Modify the Table.RenameColumns step to dynamically find the target column. Use a combination of Table.ColumnNames and List.Select to identify the header containing the word 'Week', then map it to your desired static column name.

Try WPS Office for Your Data Processing Needs
While Power Query is a specific Microsoft Excel feature, WPS Spreadsheet offers a robust, free, and lightweight alternative for standard data analysis, formatting, and daily reporting tasks with full compatibility with Excel files.
- 1. Download and install: Get WPS Office for free from the official website and install it on your device.
- 2. Open your report files: Launch WPS Spreadsheet and open your existing .xlsx weekly reports with zero formatting loss.
- 3. Analyze your data: Utilize the built-in sorting, filtering, and formula tools to easily manage your dynamically changing weekly data.

Frequently Asked Questions
Why does my Power Query break when columns change order?
Power Query often hardcodes column names or positions during the 'Changed Type' or 'Renamed Columns' steps. If the source data changes the column order or introduces a new name (like a new week number), the hardcoded reference can no longer be found, causing an error.
Can I rename columns dynamically using just the Power Query interface?
While you can double-click a header to rename it manually, this action hardcodes the original name into the step. For truly dynamic renaming based on changing text (like varying week numbers), modifying the M code in the Advanced Editor is usually necessary.
How do I remove hardcoded column references in Power Query?
Check the 'Applied Steps' pane on the right side of the Power Query Editor. Look for auto-generated steps like 'Changed Type'. You can either delete these steps or edit the formula bar to remove the hardcoded arrays of specific column names.




