Skip to content

Instantly share code, notes, and snippets.

@DmitriyVlasov
Last active August 9, 2018 09:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DmitriyVlasov/9436da41b71b52eafc2f3fbc25fc0572 to your computer and use it in GitHub Desktop.
Save DmitriyVlasov/9436da41b71b52eafc2f3fbc25fc0572 to your computer and use it in GitHub Desktop.
Example Create DAX Measure generate sparkline with SVG
Sparkline Line =
// Sample get from: https://powerbi.microsoft.com/en-us/blog/power-bi-desktop-august-2018-feature-summary/
// Formared width: http://www.daxformatter.com/
// Issue: SparlineMeasure var won't work when referenced (all bars end up 100%)
// Issue: Refactor to avoid nested SUMMARIZEs
// Issue: Negative values currently will not appear
VAR SparklineMeasure =
SUM ( Sales[SalesAmount] ) // don't use this below per issue above
VAR SparklineMeasureTarget =
SUM ( Sales[SalesAmount] )
VAR PointCount =
DISTINCTCOUNT ( Sales[OrderDate] ) + 1
VAR PointWidth =
INT ( DIVIDE ( 100, PointCount, 0 ) )
VAR LineColor = "#01b8aa"
VAR Lines =
CONCATENATEX (
ADDCOLUMNS (
SUMMARIZE ( 'Sales', 'Sales'[OrderDate] ),
"Height", INT (
100
* DIVIDE (
SUM ( Sales[SalesAmount] ),
MAXX (
SUMMARIZE (
ALLSELECTED ( 'Sales' ),
'Sales'[OrderDate],
"MeasureValue", SUM ( Sales[SalesAmount] )
),
[MeasureValue]
)
)
),
// The Index provides the horizontal axis for the sparkline
"Index", RANKX ( 'Sales', 'Sales'[OrderDate], 'Sales'[OrderDate], ASC, DENSE )
),
PointWidth * [Index]
& ","
& 100 - [Height],
",",
[Index], ASC
) // Add points to line and remember to set Data Category to Image URL
RETURN
IF( HASONEVALUE( 'Sales'[SubCategory] ),
"data:image/svg+xml;utf8," &
"<svg xmlns='http://www.w3.org/2000/svg'
x='0px'
y='0px'
width='100'
height='100'
viewBox='0 0 100 100'" &
">" &
"<polyline
fill='none'
stroke='" & LineColor & "'" &
" stroke-width='3'
points='" & Lines & "'" &
"/>"
"</svg>", BLANK()
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment