top of page

Using TMDL to generate bulk DAX Measures for Formatting

  • Writer: Aaron Coughlan
    Aaron Coughlan
  • May 2
  • 3 min read

Introduction


In this blog, I will demonstrate how to use the TMDL view in Power BI, to generate multiple DAX measures simultaneously, intended for use in conditional formatting and to improve visual design.


This TMDL script can be executed in any semantic model, as long as the TMDL preview feature is enabled. Instead of manually adding each measure one by one, this allows me to quickly replicate the entire set across multiple semantic models.


As shown the screenshot below, I have prepared a TMDL script that generates 31 measures, creating colours with hex codes, and symbols with UNICHAR codes.



What problem does TMDL solve?


  • Adding and formatting measures manually in bulk is slow and inefficient.

  • Previously, I have used C# scripts in Tabular Editor 2 for automation, but this approach was clunky, difficult to maintain and hard to reuse.

  • Some companies and clients restrict the use of third-party tools like Tabular Editor, making a native, built-in solution like TMDL highly valuable!

    • As a bonus, the colour pallet used here is colour-blind friendly, improving accessibility!


Implementation of TMDL


This section will give you a step-by-step guide on how to set-up TMDL and implement the script to generate your DAX Measures.


Enable TMDL

First, ensure that TMDL view is enabled for your Power BI Model:

  • Go to File > Options and settings > Options > Global > Preview Features, and check the box for TMDL View.

  • After restarting your semantic model, the TMDL View will now appear on the left-hand vertical bar.


Set-up your Model


Before adding new measures, my preference is to put measures in a dedicated measures table which I choose to call Calculations:

  • In the TMDL view, click and drag the Calculations table into the TMDL editor or right-click on the Calculations table and select Script TMDL to > Script tab.

    • This will expose TMDL definition of your measure table.

    • This TMDL documentation from Microsoft gives a clear example on how to get started with scripting in the TMDL view.

  • Now you’re ready to add measures!

    • Paste your measures after the table and lineageTag keywords, but before the  column definitions.

    • Click Apply, and your measures will instantly appear in the model.



Conditional formatting with DAX measures


This section will cover how to utilise the new DAX measures in a Card (new) visual, as shown in the image below.


For background on hex codes, the use of UNICHAR in DAX and Field Values in visuals, please refer to an earlier Datalineo article, “Go with the dataflow - a conditional (formatting) love story”.


Setting up your Card (new) visual


Create a Card (new) visual and add some numeric measure to the Data field.

  • In the format pane > Callout values > Values > Condition Formatting:

    • From the Dropdown, select Field value and then the desired colour, in this case, it is ColourGreenDark.







  • Adding the Rocket Symbol to the Card visual took a little unwanted work, wrapping our UNICHAR measure in a SVG Image URL, which I’m hoping will be more natively solvable at some point.


Gotchas to watch out for!


Preview Status

At the time of writing, TMDL is still a preview feature. Use caution if planning to deploy it widely in enterprise solutions until it’s officially supported by Microsoft.


Hidden Characters in your TMDL Script

I had to install the TMDL Extension in Visual Studio Code to clean up hidden characters and ensure proper indentation in my script.


File Format

I recommend saving your script as a .tmdl file using Visual Studio Code to maintain formatting and avoid issues.


Full TMDL Script


The full TMDL script can be found here on the Datalineo GitHub.


References


 
 
 

Comments


bottom of page