SVG Sparklines - Line
08-10-2018 04:04 AM - last edited 08-13-2018 14:47 PM
08-10-2018 04:04 AM - edited 08-13-2018 02:47 PM
Power BI now allows the dynamic creation of Scalable Vector Graphics (SVG) images. Prior to the August 2018 version, it was possible to build SVG content into custom columns, but now it is possible to create measures that dynamically update to account for user selections and filters.
For additional detail on the method, please see this post on the DataVeld blog: Use SVG Images in Power BI: Part 3
Substitute your own measure in place of [Measure Value], a date or numeric column for 'Table'[Date] for the X axis, and a column for 'Table'[Category] that will appear on the same table row as the sparkline.
The x axis is built by establishing the min and max values for each date and adjusting the range so that the min corresponds to 0 and max corresponds to 100.
The y axis is built by finding the min and max measure values and adjusting the range from 0 to 100 similar to the x axis. The measure value's position is based on ([Measure Value] - Min) / (Max - Min), a key calculation that I added awhile ago as another Quick Measure. In the case of the y axis though, SVG works from the top to the bottom (0 at top), so 100 - [Y] makes it render correctly. Otherwise, the chart would appear upside down.
After building a table of X and Y coordinates, CONCATENATEX is the magic function that puts the points together into a line.
IF(HASONEVALUE('Table'[Category])[...] helps hides the total in a Table or Matrix because the total renders as text and not an image.
Once the sparkline measure is ready, set the Data Category to Image URL and add the sparkline measure to a Table or Matrix visual.
In addition, change the LineColor variable from static to dynamic to create conditional color formatting. In this example, if the Margin % is not greater than 20%, the sparkline should be grey instead of green.
//Note: use %23 instead of # for Firefox compatibility
VAR LineColor = IF([Margin] > 0.20,"%2301B8AA","%23BBBBBB")
Create additional variables for stroke width or any applicable SVG property and subsitute new variables into the SVG text as needed.
Thanks to my BlueGranite colleague Meagan Longoria for providing a challenge that first made me investigate whether it was possible to dynamically build SVG content in Power BI. Thanks as well to Phil Seamark for suggesting optimizations to my original DAX.
09-05-2018 10:13 AM
Is it possible to make the sparkline stretch (or Fill) along the x axis when the width of the column is adjusted in the table? I was hoping to make the sparkline not be so condensed.
11-27-2018 06:21 PM
Is it possible to use this when the report is connected to live connection.
I was able to get the Image URL but I am unable to change the data category for the calculated measure to ImageURL. Only Uncategorized, Longitude and Latitude options are available, rest are greyed out.
12-07-2018 09:46 AM
I think so. This is what I have: