cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
acanepa
Resolver I
Resolver I

[Custom Function Guide] Column Standardizer

Have you worked with an Excel or CSV file that changes slightly their column names? Whatever the reason, this seems to make your Power BI refresh crash.
As you know, Power Query is very susceptible to any change, especially using Functions such as Table.ReorderColumns or Table.SelectRows.

To solve that problem we have developed a function called ColumnStandardizer. The idea as the name suggests is to standardize a set of columns from a given table based on a list of predefined columns.

 

Example #1

Assuming receiving a monthly table like the one below.
The column headers are roughly the same, but rarely are exactly the same, so appending a folder will not work as expected.

 

acanepa_0-1654781171528.png

 

 

Standard Names

The first step is to decide on the standard names of your columns. We have defined the ones for the table above.

  1. Date Period
  2. Date Time
  3. Sales Amount
  4. Profit Amount

That means we have removed unnecessary characters and added a space for clarity.

How to use the function

Steps

  1. Define your standard columns as a list.
  2. Get the columns name as a list from your table.
  3. Pass the two lists to ColumnStandardizer.

To simplify the code I’ve used Source = Table.

let
  Source = Table, 
  GetColumnNames = Table.ColumnNames(Source), 
  RenameList = ColumnStandardizer(
    GetColumnNames, 
    {"Date Period", "Date Time", "Sales Amount", "Profit Amount"}
  ), 
  ReplaceColumnNames = Table.RenameColumns(Source, RenameList)
in
  ReplaceColumnNames

The second argument of ColumnStandardizer defines the standard list of columns {"Date Period", "Date Time", "Sales Amount", "Profit Amount"}. This can be also defined in a Power Query variable as we did.

Example #2

The same example as before, the only difference is we have an additional column we weren’t expecting, Margin.

Date.Period Date.Time Sales.Amount Profit_Amount Margin

 

acanepa_1-1654781198618.png

 

Well, that’s not a problem. We can do the same we did before, and we just need to add an additional step with Table.SelectColumns.

Basically, because we have standardised the column names, and we have defined that list beforehand, we use the list again in Table.SelectColumns

let
  Source = Table, 
  GetColumnNames = Table.ColumnNames(Source), 
  StandardColumns = {"Date Period", "Date Time", "Sales Amount", "Profit Amount"}, 
  RenameList = ColumnStandardizer(GetColumnNames, StandardColumns), 
  ReplaceColumnNames = Table.RenameColumns(
    Source, 
    ColumnStandardizer(
      GetColumnNames, 
      {"Date Period", "Date Time", "Sales Amount", "Profit Amount"}
    )
  ), 
  SelectColumns = Table.SelectColumns(ReplaceColumnNames, StandardColumns)
in
  SelectColumns

Source

  1. The code is stored on Github:
    https://github.com/acanepas/ColumnStandardizer
  2. Power BI Sample
    https://github.com/acanepas/ColumnStandardizer/blob/main/ColumnStandardizer%20Example.pbix
1 REPLY 1
v-kkf-msft
Community Support
Community Support

Hi @acanepa ,

 

Thank you for sharing, this is a very good article.

 

Best Regards,
Winniz

Helpful resources

Announcements
August 1 episode 9_no_dates 768x460.jpg

The Power BI Community Show

Watch the playback when Priya Sathy and Charles Webb discuss Datamarts! Kelly also shares Power BI Community updates.

Power BI Dev Camp Session 24 without aka link and time 768x460.jpg

Ted's Dev Camp - July 28, 2022

Watch Session 24 of Ted's Dev Camp along with past sessions!

Power Platform Conf 2022 768x460.jpg

Join us for Microsoft Power Platform Conference

The first Microsoft-sponsored Power Platform Conference is coming in September. 100+ speakers, 150+ sessions, and what's new and next for Power Platform.

Top Solution Authors