SVG Sparklines - Categorical Columns

1578 Views
Highlighted
Advisor
Posts: 348
Registered: ‎06-26-2015

SVG Sparklines - Categorical Columns

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 "Column" measure formula on GitHub

 

 

Substitute your own measure in place of [Measure Value], a column for 'Table'[Customer Segment] for the categorical X axis, and a column for 'Table'[Category] that will appear on the same table row as the sparkline.

 

Since the x axis is categorical, it is built and sorted using RANKX. Bar Width is found by dividing 100 by the number of distinct categories, and columns are adjusted to the range 0-100 by multiplying Rank * Bar Width.

 

The y axis is complex because of the need to account for a mix of positive and negative values. If all Y values were positive, the axis could use [Measure Value] / Max and would be fairly simple. Instead, depending on the combination of positive only, negative only, and mixing negative and positive values, there are SWITCH( ) statements used for establishing both the SVG Y value and the Height of the bar.

 

CONCATENATEX combines the separate SVG rectangles.

 

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 BarColor 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. Because Firefox will not render # in SVG, substitute %23 in place of # if using hex colors.

 

//Note: use %23 instead of # for Firefox compatibility
VAR BarColor = 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.

Attachment
Regular Visitor
Posts: 38
Registered: ‎03-22-2017

Re: SVG Sparklines - Categorical Columns

Question: One thing that's not clear to me is whether the images these use have to sit in a square container in the grid. In the case above, if there were say 8 more series, meaning twelve columns in all, is it possible to have the resulting SVG -generated sparkline sitting in a rectangular grid instead of a square grid, so that the spacing between rows is exactly as it is in the file, while leaving the image at the same size as it currently is? i.e. like this:


7-09-2018 12-15-51 PM.jpg

Advisor
Posts: 348
Registered: ‎06-26-2015

Re: SVG Sparklines - Categorical Columns

The table and matrix visuals limit you to a square grid.

Regular Visitor
Posts: 38
Registered: ‎03-22-2017

Re: SVG Sparklines - Categorical Columns

[ Edited ]

 

Bugger!

 

My understanding is that MS could change this if they wanted...they just didn't think we'd be doing things like producing Sparklines using SVG measures.

 

Of course, what we really need MS to do is to implement sparklines natively in a Table/Matrix, as per this idea on the Ideas forum.

 

Preferably multi-series sparklines like I talk about at this comment on an Excel thread.