Adding Prefix

I had a date table with 50+ columns and all the columns needed to have "Effective" in the front. So instead of just Date, I needed it to be "Effective Date". If it was just a couple of columns, I could rename them manually. We can also rename columns dynamically using M when the data is imported in PowerQuery. But imagine a scenario where you are importing one date table and you want to create another date table using calculated table (for role playing dimensions) where the new date table needs to have all the columns prefixed with "Effective" or "Ship" etc. That's where below techniques will be helpful. You will need to install Tabular Editor first.

1. Rename + Regex

  1. Open Tabular Editor and connect it to the Power BI Desktop instance
  2. Select all the columns you want to rename
  3. Press F2 or right click > Batch Rename
  4. Select "Use Regular Expressions"
  5. In the Find option, enter \w.+ . This will select start of the line.
  6. In the Replace, enter Effective $&. $& will select the existing string.
  7. Click Ok.

2. Using C# Script

  1. Open Tabular Editor and connect it to the Power BI Desktop instance
  2. In the C# Script window, enter below script
  3. Select all the columns you want to rename
  4. Run the script
//1. Change the prefix below, notice the space at the end.
//2. Select all the columns to rename

var prefix = "Effective ";

foreach (var col in Selected.Columns)
    {

        col.Name = prefix + col.Name;
    }

Note here that we are editing the tabular object model which means these changes won't create a step in the Power Query. Depending on your scenario, data source, data quality etc. you may or may not want that. I showed this for columns but you can use it for renaming measures as well. For measures, change the C# script from Selected.Columns to Selected.Measures.

References

  1. https://regexr.com/
  2. Useful C# scripts : https://docs.tabulareditor.com/te2/Useful-script-snippets.html