Power Query: Name or Number
21 August 2019
Welcome to our Power Query blog. Today, I look at an example where renaming a column is performed by column position.
I have some expense data that Mary, my reliable fictional salesperson has supplied:
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2019/power-query/142/image1.png/e774d10cbbb9450fc45efbe51abdf434.jpg)
I want to rename expense to Expense Type. I can double click the column or right-click and select ‘Rename’:
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2019/power-query/142/image2.png/f32e5a15e2cf9c3e4d2d058458ce054d.jpg)
I have created a new step with the following M code
= Table.RenameColumns(Source,{{"expense ", "Expense Type"}})
I make a few other changes and save my query.
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2019/power-query/142/image3.png/f1140ff857fc3b6f5f97a6a24f4a6fc7.jpg)
I have similar data from Paul:
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2019/power-query/142/image4.png/72aa864d2854c6fefb1083fba0ab5792.jpg)
I use the Expenses query for this data too.
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2019/power-query/142/image5.png/36776d1da4d05b45bb5a5d09375f407c.jpg)
However, the results are not ideal:
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2019/power-query/142/image6.png/23912d3b1671861e02bebcd5183f1607.jpg)
Paul has called the column holding the expense type data Type, so column name expense is not recognised. I need a way of renaming my column without using the original name. I will use the formula Table.ColumnNames() instead of the original name of the column.
Table.ColumnNames(table as table) as {Text}
This function returns the names of columns from a table.
Table.ColumnNames will return all the column names from my table in a list, so Table.ColumnNames(Source) will give me all the column names in my source. In order to select a particular column name, I need to specify where my column appears in the list, starting at 0 for the first column. The expense type data is in the third column, so its index will be two (2). Therefore, instead of my original step,
=Table.RenameColumns(Source,{{"expense ", "Expense Type"}})
I will use
=Table.RenameColumns(Source,{{Table.ColumnNames(Source){2}, "Expense Type"}})
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2019/power-query/142/image7.png/6f49c288a0d88a66b427eaf4ece923d6.jpg)
My column has been renamed without referencing the original column name. This is great so I save my query…
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2019/power-query/142/image8.png/b9ee28d90e6b5bc92ea4aeafdad51628.jpg)
Another problem! This time, Paul has not used a capital letter on the Amount column. I can use a similar approach for the Expense Type column. Instead of the step
=Table.TransformColumnTypes(#"Filled Down",{{"Amount", Currency.Type}})
I will use
=Table.TransformColumnTypes(#"Filled Down",{{Table.ColumnNames(Source){3}, Currency.Type}})
This time, I need to direct my change at the fourth column, identified by index 3:
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2019/power-query/142/image9.png/0485ccbc83bdeec1d741bad442a1ea5f.jpg)
I have now created a more robust query that can cope with my salespeople’s column names.
Come back next time for more ways to use Power Query!