Skip to main content

How to handle changing column names in Power Query

When working with spreadsheets and tables within spreadsheets it can be common for the column names to change. This can be deliberate or accidental….

Fortunately there is a transformation in the Power Query interface that can deal with column header issues easily.

Note - if the order of the columns change this wont work!

In this example I have a table with 3 columns as shown here

The associated query includes a transformation to create a new column with 10% of the price, called Fee.

As you can see the column titled Price is included in the formula to create the new 'Fee' column. If for any reason this column changes then this formula will fail and present an error. To demonstrate this I'll change the column title to 'Price ($)' and refresh the query.

As expected, the Fee column returns an error as the Price column has changed. The error details the following issue

The first line of the error clearly states the Price of the record wasn’t found.

How to fix this issue

Fortunately this issue can be easily fixed by applying the following process

1. Use Headers as First Row

2. Delete top row

3. Rename the columns to the desired column names

To set the headers as the first row, we'll use the 'Use Headers as First Row' transformation in the ribbon.

This will push the headers down in to the rows in the table and give the columns consisent names, Column1, Column2, Column3.

Then we'll remove the top row using the 'Remove Top Rows…' Transformation in the quick table menu

Enter 1 in the options for this transformation, to remove the single top most row and click 'OK'

This will leave the following table

Then rename the columns as required by double clicking on the header row and typing in the preferred column name

Once this is done the multiplication that we had previously used will return the desired result because the column names are aligned to the original names.

The final query will look something like this…

With the M code containing something similar to the following:


let

    Source = Excel.Workbook(File.Contents("C:\Users\...\Documents\Data\SampleData\SampleTable.xlsx"), null, true),

    Table1_Table = Source{[Item="Table1",Kind="Table"]}[Data],

    #"Demoted Headers" = Table.DemoteHeaders(Table1_Table),

    #"Removed Top Rows" = Table.Skip(#"Demoted Headers",1),

    #"Renamed Columns" = Table.RenameColumns(#"Removed Top Rows",{{"Column1", "Type"}, {"Column2", "Value"}, {"Column3", "Price"}}),

    #"Inserted Multiplication" = Table.AddColumn(#"Renamed Columns", "Fee", each [Price] * 0.1, type number)

in

    #"Inserted Multiplication"


Comments

Popular posts from this blog

How to combine multiple files with Power Query (with no VBA and just 10 mouse clicks!)

The need to combine information from multiple files is one that most users of Excel will have come across at some point in the use of Excel. I've personally spent far too many hours aggregating data from multiple files, that are identical in structure, so that I can analyse larger datasets and provide insights into products and processes. For anyone who has also done this and not yet discovered Power Query you'll probably be amazed how simple the process has become. I realise there might be some who will say "just use VBA, its easy once you learn how to code..." and they would be right. The method using Power Query provides a zero code solution that is an evolution of the Excel interface that many will already be familiar with. In this example, I've created a sample file and created a number of duplicates of the file which I've saved in a folder. The folder contains only these files and i'd recommend you do the same if you're looking to try out this pr

Extracting data from Word (.docx) files into Power Query

Word and Excel don’t usually get along too well so it's no surprise that Power Query isn't directly compatible with its estranged cousin Word either. If you are presented with the need to import data from Word into Power Query you'll be please to hear it is possible however it requires a couple of manual steps to make it work.  The manual steps could fairly easily be completed by a batch file which would automate the process further. Here is the Excel data pasted 'as values' in a Word file which i'll use for the first example Here is the Excel data pasted with 'keep source formatting' which i'll reference a couple of times in the article. Although the steps I've covered below aren't complex, this whole process has some unknowns around it so you may find the result in your instance varies from mine. The Word file I've used contains the contents of a range of excel cells that I deliberately pasted as values into Word to create a test file f

How to automate the import of all files in a Google Drive folder to PowerBI, now updated, please read the first paragraphs!! (PowerQuery)

Update!!! This method no longer works although there is a new Google Sheets connector for PowerQuery that is currently in beta. If you're using PowerBI, you'll need to enable the preview features to enable it. https://docs.microsoft.com/en-us/power-query/connectors/googlesheets In the interest if demonstrating how it 'did' work, the original post is provided below. If you attempt to replicate this, you'll quickly realise the website doesn't behave as it used to. (here is the original article)  Using PowerQuery to access multiple files within the same folder on a local or network drive is a game-changing feature that will almost certainly save many people hours of effort. This functionality is great if your data exists in a place that is easily accessible but what do you do if your data is somewhere less accessible like Sharepoint, OneDrive or even Google Docs? I have previously connected to data on SharePoint and found it fairly straight forward which raises the