Renaming a Column if it Contains String

programsam

Board Regular
Joined
Feb 10, 2016
Messages
123
If the subject isn't enough, hopefully the image will better explain. I have a folder where Power Query appends all of hte files. Each file has a different date and the date is always in the "Status/On order" column. I am trying to rename the column IF it has the word "Status" in it to remove all of the extra noise and just call it "STATUS".

1690230813567.png


Excel Formula:
= Table.TransformColumnNames(#"Promoted Headers", each if Text.Contains(Text.Upper(_),"Status") then "STATUS" else (_))
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
If the subject isn't enough, hopefully the image will better explain. I have a folder where Power Query appends all of hte files. Each file has a different date and the date is always in the "Status/On order" column. I am trying to rename the column IF it has the word "Status" in it to remove all of the extra noise and just call it "STATUS".

View attachment 95841

Excel Formula:
= Table.TransformColumnNames(#"Promoted Headers", each if Text.Contains(Text.Upper(_),"Status") then "STATUS" else (_))
You are using Text.Upper() function, which is the correct approach. However, you need to compare it with the uppercase word as well.

Power Query:
= Table.TransformColumnNames(#"Promoted Headers", each if Text.Contains(Text.Upper(_),"STATUS") then "STATUS" else (_))
 
Upvote 0
Note: If you have multiple columns with the Status keyword in them, then the other columns will be also renamed except for including an incremental number, such as STATUS1, STATUS2, etc. In this case, if you know the column position, then you can check that column only and update it if necessary.

Power Query:
ColumnName = Table.ColumnNames(#"Promoted Headers"){0},
    Result = Table.RenameColumns(#"Promoted Headers", if Text.Contains(Text.Upper(ColumnName), "STATUS") then {ColumnName, "STATUS"} else {})
 
Upvote 0
Solution
Note: If you have multiple columns with the Status keyword in them, then the other columns will be also renamed except for including an incremental number, such as STATUS1, STATUS2, etc. In this case, if you know the column position, then you can check that column only and update it if necessary.

Power Query:
ColumnName = Table.ColumnNames(#"Promoted Headers"){0},
    Result = Table.RenameColumns(#"Promoted Headers", if Text.Contains(Text.Upper(ColumnName), "STATUS") then {ColumnName, "STATUS"} else {})
Thank you for this, your first response was enougth to point me in the right direction but this is certainly useful!
 
Upvote 0
FWIW, you could also use:

Power Query:
Text.Contains(_, "Status", Comparer.OrdinalIgnoreCase)

for a case-insensitive comparison
 
Upvote 0

Forum statistics

Threads
1,223,947
Messages
6,175,563
Members
452,652
Latest member
eduedu

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top