Until coming across this piece of code, I often used the DAX formatter (https://www.daxformatter.com/) to format all of my Tabular Model measures.

However, if you end up writing a lot of measures in your Tabular Model this can become quite tedious.

There is a faster method however. Using Tabular Editor 2 and some basic C# code you can format all of your Tabular model measures at once.

Assuming you have Tabular Editor 2 installed on your machine, open Tabular Editor 2 and click on the Advanced Scripting option. Now paste in the C# code below.

foreach (var m in Model.AllMeasures) { 
    m.Expression = FormatDax(m.Expression); 
}

Click on the green plus symbol to save this code as a custom action. Name the custom action ‘Format All DAX Measures’.

Tick the box ‘Measure’ for the context for which this action will be available.

Now, whenever you connect Tabular Editor 2 to your Tabular model you can click Samples -> Custom Actions -> Format All DAX Measures and click the green play button, this code will loop through each of your measures and format them one by one. Once completed successfully, you should see a small message similar to the snip below.

I hope this helps!

Tags: ,