Use Tabular Editor C# to add custom visuals to Power BI reports

Written by Kurt Buhler | Sep 19, 2024 7:00:00 AM

Key takeaways

  • SVG measures create small custom visuals: DAX measures containing a dynamic SVG specification render as small charts in tables, matrixes, and card visuals with pixel-perfect control.
  • A C# script library streamlines them: Over two dozen ready-made visual templates let you add SVG measures without hand-writing the complex SVG specification.
  • Plug-and-play from Tabular Editor 3: Copy a template script, run it against a table, select your fields, then format the table visual's image size.
  • Save preferred templates as macros: Add the scripts to Tabular Editor macros so you can insert a visual at the push of a button.

Introduction

This article shows how to use a Tabular Editor 3 C# script library to add SVG measures to Power BI report visuals without hand-writing each SVG specification.

SVG measures and the template library

IMPORTANT

Short on time? The link to the template library is here.

In Power BI there are many options to create custom visuals for your reports. An increasingly popular technique is to create DAX measures that contain a dynamic SVG specification. These “SVG measures” render as small custom visuals in certain visuals like tables, matrixes, and the card visual. These visuals allow for pixel-perfect control and can result in some creative and powerful charts.

You can see an example of these SVG visuals below:

Figure 1: Examples of SVG visuals used in a table of a Power BI report.

However, creating these SVG specifications is complex, time-consuming, and prone to error. Furthermore, these measures can be a burden for the semantic model; if they aren’t placed in separate tables and display folders, users might confuse them with proper model measures.

To mitigate these costs, many in the Power BI community have shared their own SVG visual templates, but those less experienced in Power BI or in dealing with SVGs might struggle to implement these templates in their own work. To help with this, we’ve created a library of C# scripts to streamline the implementation of these templates. This library includes over two dozen original visual designs that you can use in your own Power BI reports.

Figure 2: The library contains over two dozen templates of various chart types. Most templates focus on comparing actual versus target for a single category but can be extended to multiple targets and multiple categories. The number of templates is growing over time, and community contributions are welcome.

NOTE

This library of templates is maintained by Kurt Buhler (Data Goblins) and was created in collaboration with Tabular Editor. If you want to convert an SVG template to a C# script template, contact Kurt Buhler via the appropriate social media channels (LinkedIn or X).

How to use these scripts

Watch the walkthrough video on YouTube

These scripts are provided as a “plug-and-play” solution for Tabular Editor 3. To use them, simply follow the instructions below:

  1. Navigate to the library and copy the script for the chart template you want to use. Ensure that you apply the correct chart type for your scenario.
  2. In Tabular Editor, connect to your model and select a table where you want to add the measure. This can be a local model open in Power BI Desktop or a remote model in a PPU or Fabric workspace.
  3. Paste the script in a new “C# Script” window. Run the script while selecting a table where you want the measure to be inserted.
  4. Follow the instructions in the script to select the fields to use in the visual. Note that you can only add fields that are not hidden; you’ll need to make any hidden fields visible first.

  1. The script will make the measure, including:
    • Adding the DAX code.
    • Creating the measure.
    • Setting measure properties, like DataCategory and Description.
    • Organizing the measure into folders to segregate it away from the “rest” of the semantic model.
  2. Validate the measure DAX and the SVG specification (if necessary), then rename the measure, appropriately.
  3. In the Power BI report connected to the model, create a new table visual. Add the “Group by” column (that you selected in the script) to the table, and format the visual. During this formatting, set the “Image size” property to the recommended values for the template (typically ‘Height’ of 25px and ‘Width’ of 100px). Note that the visuals are designed to work with a table, but might also work with matrixes or the new card visual; however, you might need to make minor adjustments for the latter.

  1. Test the visual in different filter contexts to ensure it behaves as expected, doesn’t contain errors, and doesn’t have any performance issues.
  2. (Optional) Add the scripts for your preferred visual templates to your Tabular Editor macros. That way, you can implement it at the push of a button whenever you want.

NOTE

You will need to adjust the resulting DAX code in the measure if you want to customize the visual to your specific needs. Examples of this might be adjusting the color or size, changing it to work with multiple categories or targets, or adding either vector properties (like stroke) or new vectors (like labels).

For further reading

In conclusion

We hope that these C# scripts and SVG visual templates can help you to improve your reports. Creating and using templates for your data models and reports is an important step to not only improve your productivity but also can help improve the maturity of your team and organization in reporting. One way to do this is by using C# scripts in Tabular Editor, which are beneficial not only for creating data models, but also visuals.

Take your semantic models further with Tabular Editor.

Give Tabular Editor a spin
Plagiarism-freeScanned Written by human, not AIScanned