SVG Sparklines - Line
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
Get the DAX: SVG Sparkline "Line" measure formula on GitHub
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.
eyJrIjoiOTExYmFjNTAtYzRkNy00YzU3LWJjOWUtY2M2YjM3YjI1MzkwIiwidCI6ImFjYzhhYWE1LWYxOTEtNDgyZi05MjFiLWNmNmMzM2E1ODgzMiIsImMiOjF9
16 Replies
- sharath459Frequent Visitor
Hi David,
Is it possiblle to add two sparlines in the same column, for comparison purpose?
- deldersveldResident Rockstar
You would need to figure out the DAX to separate your lines into different series based on whatever column you need, but I don't see why it couldn't be possible.
- JorbangrFrequent Visitor
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.
- KrishnaPaudelMicrosoft Employee
Hi,
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.
Thanks,
Krishna
- cmajsFrequent Visitor
David great post!
Thanks for sharing it. Trying to emulate your model I only get the values string and not the charts. Do am I missing something?
- Nijlal01Helper I
Great visualization! I'm trying to change the code such that the measure will be summarized in monthly buckets instead of dialy. If I change this it will give a sawtooth like visual due to the year transition (it will go from 202011 --> 202012 (+1) --> 202101 (+81). Is there an easy way to fix this?
Kind regards,
Lars
- AnonymousNot applicable
Nijlal01 Add a new, or modify your existing [Year Month Column] to = Year# * 12 + Month# so for your examples you will have 24251, 24252 and 24253. Always (+1).
- rufmau68Helper III
this work well but if i wish to show a cumulative measure in the sparkline
how to change sparklineTable ?
thanks
- DipanjanGangulyMicrosoft Employee
Hi David,
Please help me to increase the width of the SVG in the dax, i tried to replace as below -
"X",INT(200 * DIVIDE('Table'[Date] - XMinDate, XMaxDate - XMinDate)),"Y",INT(100 * DIVIDE([Measure Value] - YMinValue,YMaxValue - YMinValue)))But i am not getting as expected, i dont want 100 * 100 size , so if i want to change it how to do that in the formula please guide. - AnonymousNot applicable
Hi All,
I have used the code found in the comments above and adjusted in order to create sparklines in a table in Power BI.
What I would also like to do is to change the color based on the date_id (x-axis). (Change color after specific date) in the sparklne.
Do you know if this is possible and how can be done?
Thanks a lot