Skip to content

Instantly share code, notes, and snippets.

@deldersveld
Last active October 5, 2023 20:41
Show Gist options
  • Star 21 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save deldersveld/62523ca8350ac97797131560cb317677 to your computer and use it in GitHub Desktop.
Save deldersveld/62523ca8350ac97797131560cb317677 to your computer and use it in GitHub Desktop.
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
Copy link

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

@ThomasDay
Copy link

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

@mshparber
Copy link

Thanks a lot! Excellent solution!
Question - why HASONEVALUE('Table'[Category])? What is this field and why to check HASONEVALUE?
Thanks!

@dataDad
Copy link

dataDad commented Sep 10, 2020

I also am wondering about the question above from mshparber.

@dataDad
Copy link

dataDad commented Sep 13, 2020

the field to check with the HASNONEVALUE is the category of what's being measured. For example, I used a sparkline with the awesome code above to trend stocks. In my DAX , my category was the Stock Ticker.

@powerlars
Copy link

Thanks a lot! I was wondering if there is an easy way to summarize the measure value in a monthly view since daily gives many spikes. I tried this by changing the date to a month integer number YYYYMM. Unfortunatly this gives a sort of sawtooth graph because with a year transition it will go from 202012 to 202101.

@MJBoes
Copy link

MJBoes commented Mar 25, 2021

@powerlars, late but I see you asked the same thing I figured out today. I simply added a column with a day ordinal in my date dimension:
DataDay = 'Date'[Date]-MIN('Date'[Date])
After that, each granular level can be chosen (in the example below it's by quarter) and properly spaced:

VAR GraphData=
	SUMMARIZE(
    	FILTER(VALUES('Date'[Calendar Year Quarter Number]),[Sales Amount]>0)
    	, 'Date'[Calendar Year Quarter Number]
    	, "DayNumber", MIN('Date'[DataDay])
    	,"Sales Amount", [Sales Amount]
    )
VAR XMin=MINX(GraphData, [DayNumber])
VAR XMax=MAXX(GraphData, [DayNumber])
VAR YMin=MINX(GraphData, [Sales Amount])
VAR YMax=MAXX(GraphData, [Sales Amount])
VAR LineData=ADDCOLUMNS(
	GraphData,
	"X",INT(100*DIVIDE([DayNumber]-XMin,XMax-XMin)),
	"Y",INT(100*DIVIDE([Sales Amount]-YMin,YMax-YMin))
)
VAR Lines = CONCATENATEX(LineData,[X] & "," & 100-[Y]," ", [Calendar Year Quarter Number])

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment