cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
teylyn
MVP

M Code to replace underscores in column names

Hi all,

 

my SQL Server DBA tells me that SQL doesn't like spaces in column names, and all tables and views in that project have unsightly underscores. In Power BI, I want to replace them with blanks, but changing all the column names manually is way too time consuming and doesn't scale well if more columns are added to the table or view.

 

Therefore, I wrote this little piece of M code that I put at the beginning of the query, just after navigating to the view or table I want. I hope that some of you find this useful.

 

let
  Source = Sql.Database("YourServer", "YourDatabase"),
  Navigation = Source{[Schema = "YourSchema", Item = "YourTableorView"]}[Data],
  // SQL Server doesn't like blanks in column names, so they come with underscores.
  // We replace underscores in any column name with blanks
  // list column names, turn the list into a table duplicate the name column, replace underscore with blank
  // turn the table into a list of lists
  ColumNames = Table.ColumnNames(Navigation),
  ColumnNamesTable = Table.FromList(ColumNames, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
  DuplicateColumnName = Table.DuplicateColumn(ColumnNamesTable, "Column1", "Column1 - Copy"),
  ReplaceCharacter = Table.ReplaceValue(DuplicateColumnName, "_", " ", Replacer.ReplaceText, {"Column1 - Copy"}),
  ColumnNamesList = Table.ToRows(ReplaceCharacter),
  // rename the original column names with the names from the list of lists
  RenamedColumnsFromList = Table.RenameColumns(Navigation,ColumnNamesList)
in
  RenamedColumnsFromList

Let me know how you get on with this.

 

cheers, teylyn

2 ACCEPTED SOLUTIONS
wdx223_Daniel
Super User II
Super User II

Table.RenameColumns(Navigation,List.Transform(Table.ColumnNames(Navigation),each {_,Text.Replace(_,"_"," ")}))

View solution in original post

ImkeF
Super User II
Super User II

Hi @teylyn  and @wdx223_Daniel ,
I've been there as well before I've discovered that the M-language has some nice functions that are not exposed through the UI. Transforming column names with a custom function is one of them: 

Table.TransformColumnNames(Source, each Text.Replace(_, "_", " ") )

 

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

View solution in original post

3 REPLIES 3
ImkeF
Super User II
Super User II

Hi @teylyn  and @wdx223_Daniel ,
I've been there as well before I've discovered that the M-language has some nice functions that are not exposed through the UI. Transforming column names with a custom function is one of them: 

Table.TransformColumnNames(Source, each Text.Replace(_, "_", " ") )

 

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

View solution in original post

teylyn
MVP

@wdx223_Daniel Great to see that it can be done in one step. I find the M syntax less than intuitive when it comes to writing nested formulas.

wdx223_Daniel
Super User II
Super User II

Table.RenameColumns(Navigation,List.Transform(Table.ColumnNames(Navigation),each {_,Text.Replace(_,"_"," ")}))

View solution in original post

Helpful resources

Announcements
PBI User Groups

Welcome to the User Group Public Preview

Check out new user group experience and if you are a leader please create your group!

MBAS on Demand

Microsoft Business Applications Summit sessions

On-demand access to all the great content presented by the product teams and community members! #MSBizAppsSummit #CommunityRocks

MBAS Attendee Badge

Claim Your Badge & Digital Swag!

Check out how to claim yours today!

secondImage

Are You Ready?

Test your skills now with the Cloud Skills Challenge.

Top Solution Authors
Users online (855)