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

Compete to become Power BI Data Viz World Champion! First round ends August 18th. Get started.

Reply
vyacheslavg
Helper II
Helper II

M/Power Query - rename all columns, based on text and delimiters

Dear community, 

 

Could you please help with hint on M code to do the following.

 

I have several columns in table, with headers similar to this:

 

abc[some good text]xyz.

 

I need to rename all column headers  from these 

abc[some good text]xyz

to 

some good text 

 

In other words, leave only the text between [ ] and remove everything else. 

 

I figured out, that I will most probably  need Table.Renamecolumns and Text.Betweendelimeters functions -  but I can't combine it into the working M code, exceeds my current capabilities... Smiley Embarassed

 

 

1 ACCEPTED SOLUTION

Actually, Table.TransformColumnNames is the better choice.

 

Example (assuming if "[" is present, the "]" will also be there):

 

let
    Source = Table1,
    RenamedColumns = Table.TransformColumnNames(Source, each if Text.Contains(_,"[") then Text.BetweenDelimiters(_,"[","]") else _)
in
    RenamedColumns
Specializing in Power Query Formula Language (M)

View solution in original post

3 REPLIES 3
sdjensen
Solution Sage
Solution Sage

@vyacheslavg - you could perhaps try with the transpose feature that will switch your columns to rows (and rows to columns) and then make one (or more) search/replace on the first column (that used to be your column headers) and after you make your search/replace you transpose the table again to put columns back as columns.

/sdjensen

Actually, Table.TransformColumnNames is the better choice.

 

Example (assuming if "[" is present, the "]" will also be there):

 

let
    Source = Table1,
    RenamedColumns = Table.TransformColumnNames(Source, each if Text.Contains(_,"[") then Text.BetweenDelimiters(_,"[","]") else _)
in
    RenamedColumns
Specializing in Power Query Formula Language (M)

Marcel, thanks a lot. 

It looks very close to a solution (I need to test it first).

 

Edited:

It worked!

Spent some time trying to figure out strange results - I forgot to promote headers first. After that everything worked just great.

 

Could you please advise some good training or book  for Power Query/M?

 

I've found the "M for the Data Monkey", but it seems to be so heavy PowerPivot related... Want to find something more PBD and SSDT focused.

Helpful resources

Announcements
July 2025 community update carousel

Fabric Community Update - July 2025

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

July PBI25 Carousel

Power BI Monthly Update - July 2025

Check out the July 2025 Power BI update to learn about new features.

Top Solution Authors