Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Get certified in Microsoft Fabric—for free! For a limited time, get a free DP-600 exam voucher to use by the end of 2024. Register now

Reply
Shawkins566
Frequent Visitor

Rename a Column based on the Column Position

I have pulled multiple files into PowerBI from a folder, the files are all .txt files so getting them into a usable format is a pain, but I've got it working, the only thing is, my first column says "Source.Name" which gives me the file name for each of the file rows (which I need because that contains the date), then the rest of the columns say Column 1, Column 2, Column 3 and so on.  The first row contains the headers so I promote the first row, but that means that the first row file name replaces "Source.Name" as the header.  I renamed this as Date but when I refresh the data, it breaks because if that file doesn't run first or is missing from the folder it's trying to change "21.03.24 Group Summary Data" to "Date", but saying "21.03.24 Group Summary Data" is missing.

 

I'm looking to find out if I can replace the column header based on the table position so that no matter what is in the first column header it'll change to "Date"

 

Anyone any ideas?

 

Many thanks

 

Sarah

1 ACCEPTED SOLUTION
Zang_Mi
Resolver II
Resolver II

Hello,

 

There is an idea to solve your problem:

  1. Make the query capturing automatically the name of the first column
  2. Save it as variable
  3. Use this variable for the renaming

Let's say that we want to rename the first column of the following table by replacing the text '202400.xlsx' by 'Date'.

Zang_Mi_0-1711631369258.png

By doing it manually, Power BI will add the following step to the query, where the original column name is a fixed text. 

Zang_Mi_1-1711631709159.png

 

Zang_Mi_2-1711631874467.png

We can replace this text by a variable that captures the first column name, which may change.

Zang_Mi_5-1711632265615.png

In this new query, the yellow line shows how can we get all the column names as a list with the function Table.ColumnNames, and the expression {0} to get the first item from the list (in that case, the first column name).

 

Then, in the last step, we can just replace the fixed text by this variable to make the query works in the same way, but able to update the name automatically even though the input files change.

 

Regards

View solution in original post

3 REPLIES 3
Zang_Mi
Resolver II
Resolver II

Hello,

 

There is an idea to solve your problem:

  1. Make the query capturing automatically the name of the first column
  2. Save it as variable
  3. Use this variable for the renaming

Let's say that we want to rename the first column of the following table by replacing the text '202400.xlsx' by 'Date'.

Zang_Mi_0-1711631369258.png

By doing it manually, Power BI will add the following step to the query, where the original column name is a fixed text. 

Zang_Mi_1-1711631709159.png

 

Zang_Mi_2-1711631874467.png

We can replace this text by a variable that captures the first column name, which may change.

Zang_Mi_5-1711632265615.png

In this new query, the yellow line shows how can we get all the column names as a list with the function Table.ColumnNames, and the expression {0} to get the first item from the list (in that case, the first column name).

 

Then, in the last step, we can just replace the fixed text by this variable to make the query works in the same way, but able to update the name automatically even though the input files change.

 

Regards

Hi, I'm not quite understanding how to enter this into the PowerBI

Hello, you can edit the query using Power Query Advanced Editor in Power BI.

Zang_Mi_0-1711638166193.png

Helpful resources

Announcements
November Carousel

Fabric Community Update - November 2024

Find out what's new and trending in the Fabric Community.

Live Sessions with Fabric DB

Be one of the first to start using Fabric Databases

Starting December 3, join live sessions with database experts and the Fabric product team to learn just how easy it is to get started.

Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Early Bird pricing ends December 9th.

Nov PBI Update Carousel

Power BI Monthly Update - November 2024

Check out the November 2024 Power BI update to learn about new features.