Dynamic Replacement

yevhen

New Member
Joined
Jan 5, 2025
Messages
5
Office Version
  1. 2021
Platform
  1. Windows
Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="rename_delete"]}[Content],
    ReplacementList = {"0".."9", "@", "&", "%"},
    SelectCol = List.Select(Table.ColumnNames(Source), each not Text.Contains(_, "*")),
    RenameCol = List.Transform(SelectCol, each {_, Text.Upper(Text.Replace(_, "1", ""))}),
    RemovedOtherColumns = Table.SelectColumns(Source, SelectCol),
    RenamedColumns = Table.RenameColumns(RemovedOtherColumns,RenameCol)
in
    RenamedColumns

Screenshot 2025-01-09 145428.png

Need help on how to make the replacement more dynamic. For example, when a new column is added, a query checks if it has numbers or special characters in a name and removes them.
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Maybe,

Power Query:
let
A = Excel.CurrentWorkbook(){[Name="rename_delete"]}[Content],
B = each List.Transform(_, each Text.Select(Text.Upper(_), {"A".."Z"})),
C = List.Zip(List.Select(List.Zip({Table.ColumnNames(A),Table.ToColumns(A)}), each List.NonNullCount(_{1}) > 0)),
D = Table.FromColumns(C{1}, B(C{0}))
in D
 
Upvote 0
Here's a pivot/unpivot-based solution. It assumes that you only want to keep the first character in the column name, and make it uppercase. All steps use the UI (no custom M code).

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="rename_delete"]}[Content],
    #"Added Index" = Table.AddIndexColumn(Source, "Index", 1, 1, Int64.Type),
    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Added Index", {"Index"}, "Attribute", "Value"),
    #"Extracted First Characters" = Table.TransformColumns(#"Unpivoted Other Columns", {{"Attribute", each Text.Start(_, 1), type text}}),
    #"Uppercased Text" = Table.TransformColumns(#"Extracted First Characters",{{"Attribute", Text.Upper, type text}}),
    #"Pivoted Column" = Table.Pivot(#"Uppercased Text", List.Distinct(#"Uppercased Text"[Attribute]), "Attribute", "Value", List.Sum),
    #"Removed Columns" = Table.RemoveColumns(#"Pivoted Column",{"Index"})
in
    #"Removed Columns"
 
Upvote 0

Forum statistics

Threads
1,225,684
Messages
6,186,424
Members
453,354
Latest member
Ubermensch22

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