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
Post a Comment