Sub topic 11
Drawing status icons with SVG measures
This chapter assumes you can write basic DAX measures, build a table or matrix in Power BI Desktop, and find the Format pane. It also assumes you have already written a measure that returns a color. Here you will return a small picture instead of a color.
Why return an image from a measure
Format by field value can color a cell or a font. It cannot draw a shape. If you want a green circle or a red circle next to a number, you need the visual to display an image.
Power BI can do this when a measure returns an SVG written as a data URI. SVG is a text format for drawings. A data URI is a string that holds the whole file inline, so no image file or web link is needed. A table or matrix shows the string as a picture when the measure's data category is set to Image URL.
The measure
The measure below reuses [Variance %] from the earlier chapters. It draws a green circle at or above target and a red circle below it.
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>"
)Three details matter here.
- The
#in a hex color is written as%23. In a URI,#starts a fragment and would cut the string short. - Attribute values inside the SVG use single quotes. This avoids escaping the double quotes that delimit the DAX string.
- The
ISBLANKtest returnsBLANK ()when there is no variance. The cell then shows no icon, which is better than a misleading red or green one.
Telling Power BI the text is an image
The measure returns text, so Power BI shows the raw string until you change the data category.
- In the Data pane, select the
Status Iconmeasure. - Open Measure tools in the ribbon.
- Set Data category to Image URL.
- Add the measure to a table or matrix.
The icon should now appear in each row. If you still see the long string, check that you set the category on the measure itself and not on another field.
Extending the shape
You can change the drawing by editing the SVG part of the string. Swap the <circle> element for a <rect> for a square, or add a second shape for a richer icon. Use SWITCH ( TRUE (), ... ) to pick among more than two fills, as in the color measure from the threshold chapter.
Keep the SVG short. The string is rebuilt for every cell the visual draws, so long drawings add up in large tables.
Limits to know
- Image URL measures work in tables and matrices. They do not work as conditional colors in chart series.
- Totals and subtotals evaluate the same measure. Wrap it in
ISINSCOPEas you did for the color measure if you want icons only on certain levels. - Color alone still carries the meaning. A red circle and a green circle look alike to some viewers. Pair the icon with a number, or draw different shapes for each state.
Exercise
Your Status Icon measure shows a long string starting with "data:image/svg+xml" in the table instead of a circle. What is the most likely fix, and why do the hex colors in the string use %23 instead of #?
Show answer
Set the measure's data category to Image URL in Measure tools so the visual renders the string as an image. The colors use %23 because # is the URI fragment marker and would end the data URI early. %23 is the percent-encoded form of #.
Exercise
Change the Status Icon measure so it shows an amber circle (#F9A825) when the variance is at least 0 but below 0.05, a green circle at 0.05 or more, and a red circle below 0. Which function should you use for the choice?
Show answer
Use SWITCH ( TRUE (), ... ) to set the fill. For example: VAR fill = SWITCH ( TRUE (), v >= 0.05, "%2327AE60", v >= 0, "%23F9A825", "%23C0392B" ). Keep the ISBLANK check around the final string so blank variances return no icon.