Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Sparkline Line =
// Static line color - use %23 instead of # for Firefox compatibility
VAR LineColor = "%2301B8AA"
// "Date" field used in this example along the X axis
VAR XMinDate = MIN('Table'[Date])
VAR XMaxDate = MAX('Table'[Date])
// Obtain overall min and overall max measure values when evaluated for each date
VAR YMinValue = MINX(VALUES('Table'[Date]),CALCULATE([Measure Value]))
VAR YMaxValue = MAXX(VALUES('Table'[Date]),CALCULATE([Measure Value]))
// Build table of X & Y coordinates and fit to 100 x 100 viewbox
VAR SparklineTable = ADDCOLUMNS(
SUMMARIZE('Table','Table'[Date]),
"X",INT(100 * DIVIDE('Table'[Date] - XMinDate, XMaxDate - XMinDate)),
"Y",INT(100 * DIVIDE([Measure Value] - YMinValue,YMaxValue - YMinValue)))
// Concatenate X & Y coordinates to build the sparkline
VAR Lines = CONCATENATEX(SparklineTable,[X] & "," & 100-[Y]," ", [Date])
// Add to SVG, and verify Data Category is set to Image URL for this measure
VAR SVGImageURL = IF(HASONEVALUE('Table'[Category]),
"data:image/svg+xml;utf8," &
"<svg xmlns='http://www.w3.org/2000/svg' x='0px' y='0px' viewBox='0 0 100 100'>" &
"<polyline fill='none' stroke='" & LineColor &
"' stroke-width='3' points='" & Lines &
"'/></svg>",
BLANK())
RETURN SVGImageURL
@williamspsouza

This comment has been minimized.

Copy link

williamspsouza commented Jan 8, 2019

Hi David. Thanks a lot for this code. Is it possible change this Sparkline Chart for Column Chart?

@ThomasDay

This comment has been minimized.

Copy link

ThomasDay commented Feb 7, 2019

Hello David, I produced some sparkline svg image code via the video Brian Grants video where he referenced your code. The svg code works perfectly--meaning the result is what Brian's video suggests--but the matrix visual does not size properly to the svg image size for the sparkline. I posted pictures and resulting image code on the power bi forum here: https://community.powerbi.com/t5/Desktop/svg-image-size-matrix-makes-huge-space-for-small-image/td-p/618223 Would you kindly take a look and see what you think? ...I suspect it's something obvious to you. Thank you, Tom

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.