Change a Column Name in the Power BI Query Editor without hardcoding

Home Power BI Query Editor Change a Column Name in the Power BI Query Editor without hardcoding

Sometimes, when bringing in data in the query editor, you need to change the name of a column that isn't always the same in the source. This is one of these occasions where the UI isn't going to help much: by double clicking on the column header and changing it into whatever you want it to will hardcode the name of the column as it is coming in. When in a new data refresh the column name is different, the query will break. Therefore, you're going to have to write some M yourself. Or steal the code below from me šŸ˜‰

Here it is:

= Table.RenameColumns (

The table you want to refer to, usually the previous step in your query,

{{ Table.ColumnNames (Ā The same table againĀ ) {Ā The number of the columnĀ },

"The new column name" }}

)

And if you want to change the order of your columns dynamically, don't just move them around using the UI either. That will hard code all of the column names in your table. This is the code you want to use instead:

Table.ReorderColumns (

The table you want to refer to which usually is the previous step in your query,

List.InsertRange (

List.Difference (

Table.ColumnNames (Ā The same table againĀ ),

{"Columnname you want to move1" , "Columnname you want to moveN"}

),

The columnnumber you want to move the columns to,

{"Columnname you want to move1" , "Columnname you want to moveN"}

)

)