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

Join us at FabCon Atlanta from March 16 - 20, 2026, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM. Register now.

Reply
masplin
Impactful Individual
Impactful Individual

Replace column name only if its wrong

Hi

 

Sure this is simply for you clever people! I havebene givne a folder containing 400 hundred folders each containing several exel files.  I hoped thefiles al lhad an idnetical strucutre, but they don't. I havefound some of the older files use the column name "Formal Notice Date" in the 6th column, whereas newer files call the same field "At Risk Date".  So is there a way to test if the 6th column has this heading and replace it if it does otherwise do nothing?

 

I had some luck with this

 Table.RenameColumns(#"Promoted Headers",{{Table.ColumnNames(#"Promoted Headers"){5}, "At Risk Date"}})

but fell over when on a file column 5 and 6 were reversed so now column 5 was already called "At Risk Date"

 

I tried to make up sme syntax, but clearly my M is not up to the task. I couldn't work out the else do nothing.

= Table.RenameColumns(#"Promoted Headers", if Record.Field(_,Table.ColumnNames(#"Promoted Headers"){5}="Formal Notice Date") then {{Table.ColumnNames(#"Promoted Headers"){5}, "At Risk Date"}} else {{Table.ColumnNames(#"Promoted Headers"){5}, Record.Field(_,Table.ColumnNames(#"Promoted Headers"){5}}}))

 

Since its clear the columns could be in any order ideally it would test if any column was caleld "Formal Notice Date" and replace it with "At Risk Date".

 

Appreicate any help as impossible for me to locate thefiles that have slightly differnet structure.

Mike

 

 

 

 

2 ACCEPTED SOLUTIONS
AlB
Community Champion
Community Champion

Hi @masplin 

I don't understand what you are trying to do with Record.Field. Try this:

= if Table.ColumnNames(#"Promoted Headers"){5} = "Formal Notice Date" then Table.RenameColumns(#"Promoted Headers",{{"Formal Notice Date", "At Risk Date"}}) else #"Promoted Headers"

 

SU18_powerbi_badge

Please accept the solution when done and consider giving a thumbs up if posts are helpful. 

Contact me privately for support with any larger-scale BI needs, tutoring, etc.

 

View solution in original post

AlB
Community Champion
Community Champion

@masplin 

= if List.Contains( Table.ColumnNames(#"Promoted Headers"), "Formal Notice Date") then Table.RenameColumns(#"Promoted Headers",{{"Formal Notice Date", "At Risk Date"}}) else #"Promoted Headers"

 

SU18_powerbi_badge

Please accept the solution when done and consider giving a thumbs up if posts are helpful. 

Contact me privately for support with any larger-scale BI needs, tutoring, etc.

 

View solution in original post

4 REPLIES 4
AlB
Community Champion
Community Champion

@masplin 

= if List.Contains( Table.ColumnNames(#"Promoted Headers"), "Formal Notice Date") then Table.RenameColumns(#"Promoted Headers",{{"Formal Notice Date", "At Risk Date"}}) else #"Promoted Headers"

 

SU18_powerbi_badge

Please accept the solution when done and consider giving a thumbs up if posts are helpful. 

Contact me privately for support with any larger-scale BI needs, tutoring, etc.

 

masplin
Impactful Individual
Impactful Individual

brilliant thnaks so much

AlB
Community Champion
Community Champion

Hi @masplin 

I don't understand what you are trying to do with Record.Field. Try this:

= if Table.ColumnNames(#"Promoted Headers"){5} = "Formal Notice Date" then Table.RenameColumns(#"Promoted Headers",{{"Formal Notice Date", "At Risk Date"}}) else #"Promoted Headers"

 

SU18_powerbi_badge

Please accept the solution when done and consider giving a thumbs up if posts are helpful. 

Contact me privately for support with any larger-scale BI needs, tutoring, etc.

 

masplin
Impactful Individual
Impactful Individual

My M is like speaking Frenech from a phrase book!!!   That's great thnaks

 

Is there a way to make this if any column has that name as found sometimes it was column 4 called "Formal notice date"? I realsie I coudl just repeat the clause multiple times to test each column. 

Helpful resources

Announcements
Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

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

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.