Sub topic 11
Drawing Status Icons with SVG Measures
Earlier chapters used DAX measures to return color strings for cell backgrounds and font colors. This chapter goes one step further. A measure can return a small picture, so a table or matrix can show a status icon computed from your data. The picture is an SVG (Scalable Vector Graphics, a text format that describes shapes), packed into a string that Power BI treats as an image.
This chapter assumes you can already:
- write a measure that computes a variance, such as
Variance %, and wrap it inDIVIDE - use
VARandSWITCH ( TRUE (), ... )for threshold logic - use
ISINSCOPEto control what a matrix shows on totals - find a measure's properties in Model view or the Measure tools ribbon
Why use an SVG measure
The built-in icon option in conditional formatting gives you a fixed set of icons with rules you set in a dialog. That works for simple cases. An SVG measure gives you more control:
| Need | Built-in icons | SVG measure |
|---|---|---|
| Custom shapes and colors | Fixed icon sets | Any shape you can describe in SVG |
| Reuse the same logic across visuals | Rules are set per visual | One measure, referenced anywhere |
| Thresholds from a slicer | Not available | Yes, if the measure reads the parameter |
| Setup effort | Low | Higher, because you write and test the string |
The trade-off is maintenance. You own the string format, so a typo produces a blank cell or raw text, not an error message.
How the pieces connect
A visual cannot tell that a string is an image unless the model says so. Two things have to be true:
- The measure returns a valid data URI (a string that carries the file contents inline instead of pointing to a file).
- The measure's data category is set to Image URL.
The anatomy of the string
The measure from the plan builds this string:
data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='16' height='16'><circle cx='8' cy='8' r='7' fill='%2327AE60'/></svg>It has three parts:
data:image/svg+xml;utf8,is the prefix. It tells the visual that the rest is SVG text, not a web address.<svg ...>is the SVG document. Thexmlnsattribute is required. Without it, many renderers draw nothing.width='16' height='16'sets the drawing canvas. The coordinates inside the SVG (cx='8',r='7') are relative to this canvas.
Two details cause most mistakes:
- Quotes. DAX strings use double quotes, so write SVG attributes with single quotes. If you need a literal double quote inside a DAX string, you must double it (
""), which makes the string hard to read. - The
#character. In a URI,#starts a fragment, so a color like#27AE60cuts the string off. Write it as%23, so#27AE60becomes%2327AE60.
A first working icon
Start with the base measures from earlier chapters, then add the icon measure.
Total Sales = SUM ( Sales[Amount] )
Sales Target = SUM ( Targets[TargetAmount] )
Variance % =
DIVIDE ( [Total Sales] - [Sales Target], [Sales Target] )
Status Icon =
VAR v = [Variance %]
VAR fill = IF ( v >= 0, "%2327AE60", "%23C0392B" )
RETURN
IF (
ISBLANK ( v ),
BLANK (),
"data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='16' height='16'><circle cx='8' cy='8' r='7' fill='" & fill & "'/></svg>"
)The logic has the same shape as a color measure. A variable holds the value, another variable picks the fill, and the RETURN expression assembles the string. The ISBLANK check comes first in intent: when there is no target, the measure returns BLANK (), so the cell stays empty instead of showing a misleading green or red circle.
Set the data category
- Select the
Status Iconmeasure in the Data pane. - In the Measure tools ribbon, open Data category.
- Choose Image URL.
Then add the measure to the Rows or Columns well of a table, or to the Values well of a matrix. The cell shows a circle. If you see the long text string instead, the data category is not set.
Three states with different shapes
Two colors are rarely enough. Reusing the thresholds from the color measure gives you three states. This version also changes the shape, so the status does not depend on color alone.
Status Icon (Shapes) =
VAR v = [Variance %]
VAR shape =
SWITCH (
TRUE (),
v >= 0.05, "<circle cx='8' cy='8' r='7' fill='%232E7D32'/>",
v >= 0, "<rect x='2' y='2' width='12' height='12' fill='%23F9A825'/>",
"<polygon points='8,1 15,15 1,15' fill='%23C62828'/>"
)
RETURN
IF (
ISBLANK ( v ),
BLANK (),
"data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='16' height='16' viewBox='0 0 16 16'>"
& shape
& "</svg>"
)The structure separates the part that changes (the shape) from the part that does not (the wrapper). To add a fourth state, add one more line to the SWITCH. The wrapper stays untouched.
The ISBLANK guard is not optional here. In a comparison, DAX treats a blank as 0 in most cases. A blank variance would fail v >= 0.05 but pass v >= 0, and the cell would get the amber square. Checking for blank before the comparison avoids that.
Controlling totals and matrix levels
A total row has its own variance, and an icon on it often means little. Reuse the ISINSCOPE pattern from the totals chapter:
Status Icon (Matrix) =
IF (
ISINSCOPE ( Product[Category] ),
[Status Icon (Shapes)],
BLANK ()
)ISINSCOPE is true only on rows grouped by Product[Category]. The grand total and any other level return blank, so no icon appears there. If you want icons at a second level, such as Product[Subcategory], combine the checks with ||.
Sizing and layout
The width and height attributes set the canvas, but the visual decides how large the icon appears. In a table or matrix, the image is scaled to fit the cell, and the image size options in the visual's cell formatting settings control the display size. Keep these points in mind:
- Use a
viewBoxwhen you want predictable scaling.viewBox='0 0 16 16'tells the renderer that the drawing uses a 16 by 16 coordinate space, whatever size it is displayed at. - Keep shapes simple. Every character in the string is stored and evaluated for each cell, so a long SVG repeated across thousands of rows slows the visual.
- Check the result at the size your users will see. A detailed icon that looks fine at 64 pixels may be a smudge at 16.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Long text appears in the cell | Data category is not Image URL | Set the category on the measure |
| Cell is empty everywhere | Missing `xmlns` attribute, or a stray `#` in a color | Check the SVG opening tag and replace `#` with `%23` |
| Cell is empty for some rows | Variance is blank, which is intended | Verify with a card or table showing `[Variance %]` |
| Measure will not save | Unbalanced double quotes in the DAX string | Use single quotes inside SVG and match each `"` |
| Icon appears on totals | No scope check | Wrap the measure in `ISINSCOPE` |
| Wrong state on rows with no target | Blank treated as 0 | Test `ISBLANK` before comparing |
To debug, put [Variance %] and the icon measure side by side in a table. Confirm the value first. If the value is right and the icon is wrong, the problem is in the string or the category. You can also temporarily return the shape variable as text, to see which branch of the SWITCH was chosen.
Limits to know about
- Image URL measures render in tables and matrices. Do not expect them to work as markers or labels inside chart visuals. For charts, use color measures instead, as covered in the chapter on charts and titles.
- Some environments restrict image display. Data URIs can behave differently in exported files or embedded reports, so test in the place where users will actually view the report.
- The string is plain text, so anyone with edit access can read and change it. Keep business rules in the model, not hidden in a visual.
Exercise
Exercise
A matrix groups rows by Product[Category]. Your measure Status Icon (Shapes) shows a triangle in the Grand Total row, and shows an amber square for a category that has no target. Which two changes fix both problems, and why does the second problem happen?
Show answer
First, wrap the icon in IF ( ISINSCOPE ( Product[Category] ), [Status Icon (Shapes)], BLANK () ) so the total row, which is not grouped by category, returns blank. Second, test ISBLANK ( v ) before any comparison and return BLANK () for it. The amber square appears because DAX treats a blank variance as 0 in the comparison v >= 0, so a missing target passes the "meets target" test unless the blank check runs first.
Exercise
Your icon measure returns the correct string, but the table cell shows the text data:image/svg+xml;utf8,<svg .... Name the cause and the fix. Then explain why the fill color #27AE60 would break the string even after you fix that.
Show answer
The measure's data category is not set to Image URL, so the visual treats the result as text. Select the measure and set Data category to Image URL in the Measure tools ribbon. Separately, a literal # starts a URI fragment and cuts off the rest of the string, so the color must be written as %2327AE60.