Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
katyfailoo
Advocate I
Advocate I

SVG Sparkline for rolling 12 months in a table matrix

Hi All,

 

The goal is to have a trailing 12 month sparkline on a matrix table. To achieve this, I used a SVG code . However, I am having issues with my SVG code (see below).

 

1 Sparkline Matrix_Test =

// Static line color - use %23 instead of # for Firefox compatibility
VAR LineColor = "%2301B8AA"

// X Axis
VAR XMinDate = MIN(DATE(YEAR(TODAY())-1,MONTH(TODAY()),1))
VAR XMaxDate = max(DATE(YEAR(TODAY()),MONTH(TODAY()),1)-1)

// Obtain overall min and overall max measure values when evaluated for each date
VAR YMinValue = MINX(FILTER(GLDate, XMinDate <= GLDate[DATE] && XMaxDate >= GLDate[DATE]),CALCULATE([Sales Amount],'Time Intelligence'[Name] = "MTD"))
VAR YMaxValue = MAXX(FILTER(GLDate, XMinDate <= GLDate[DATE] && XMaxDate >= GLDate[DATE]),CALCULATE([Sales Amount],'Time Intelligence'[Name] = "MTD"))

// Build table of X & Y coordinates and fit to 100 x 100 viewbox [This is where I get stuck]
VAR SparklineTable = ADDCOLUMNS(
SUMMARIZE(GLDate,GLDate[DATE]),
"X",INT(100 * DIVIDE(GLDate[DATE] - XMinDate, XMaxDate - XMinDate)),
"Y",INT(100 * DIVIDE(CALCULATE([Sales Amount],'Time Intelligence'[Name] = "MTD") - 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(HomeRegion[DIVISION]),
"data&colon;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
 
I need some guidance on what am I need to do for the next block of code where I need to build table of X & Y coordinates and fit to 100 x 100 viewbox. Cann anyone help me? Or does anybody have another solution. I am pretty new to adding a sparkline to the matrix. 
1 ACCEPTED SOLUTION
v-lionel-msft
Community Support
Community Support

Hi @katyfailoo ,

 

Step1, Check whether the “LineColor” and the “Lines” parameters return the correct value.

 

v-lionel-msft_3-1608606187710.png

 

Step2, modify the SVG code.

 

 

 

Measure = 
// My test
//Do not copy the following code directly, “data&colon;image/svg+xml;utf8” has changed.
"data&colon;image/svg+xml;utf8," &
"
<svg xmlns='http://www.w3.org/2000/svg' version='1.1' x='0px' y='0px' viewBox='0 0 100 100'>
  <polyline points='20,20 40,25 60,40 80,120 120,140 200,180' style='fill:none; stroke:#2301B8AA; stroke-width:3' />
</svg>
"

 

 

 

v-lionel-msft_4-1608606453049.png

The most important thing is not to forget to set the measure to "Image URL".

v-lionel-msft_0-1608612474015.png

 

Or please provide your sample .pbix file if your problem is still not resolved

 

Best regards,
Lionel Chen

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

 

View solution in original post

4 REPLIES 4
rufmau68
Helper I
Helper I

Im new in pbi but

if I wish to build a cumulative sum sparkline ?

how have to change SparklineTable

 

v-lionel-msft
Community Support
Community Support

Hi @katyfailoo ,

 

Step1, Check whether the “LineColor” and the “Lines” parameters return the correct value.

 

v-lionel-msft_3-1608606187710.png

 

Step2, modify the SVG code.

 

 

 

Measure = 
// My test
//Do not copy the following code directly, “data&colon;image/svg+xml;utf8” has changed.
"data&colon;image/svg+xml;utf8," &
"
<svg xmlns='http://www.w3.org/2000/svg' version='1.1' x='0px' y='0px' viewBox='0 0 100 100'>
  <polyline points='20,20 40,25 60,40 80,120 120,140 200,180' style='fill:none; stroke:#2301B8AA; stroke-width:3' />
</svg>
"

 

 

 

v-lionel-msft_4-1608606453049.png

The most important thing is not to forget to set the measure to "Image URL".

v-lionel-msft_0-1608612474015.png

 

Or please provide your sample .pbix file if your problem is still not resolved

 

Best regards,
Lionel Chen

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

 

Greg_Deckler
Super User
Super User

@katyfailoo - SVG Sparklines - Line (powerbi.com) Try this Quick Measure gallery entry.


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.