skip to main content
Power BI
    • What is Power BI
    • Why Power BI
    • Customer stories
    • Data visuals
    • Security
    • Power BI Desktop
    • Power BI Pro
    • Power BI Premium
    • Power BI Mobile
    • Power BI Embedded
    • Power BI Report Server
  • Pricing
    • Azure + Power BI
    • Microsoft 365 + Power BI
    • Dynamics 365 + Power BI
      • Energy
      • Healthcare
      • Manufacturing
      • Media
      • Retail
    • For analysts
    • For IT
      • Overview
      • Embedded analytics
      • Power BI visuals
      • Automation
      • Documentation
      • Community
    • Overview
    • Find consulting services
    • Partner showcase
    • Find a partner
    • Become a partner
    • Instructor-led training
    • Getting started
      • Overview
      • Online workshops
      • Self-guided learning
      • Webinars
      • Documentation
      • Roadmap
      • Overview
      • Issues
      • Give feedback
    • Blog
    • Business intelligence topics
    • Overview
    • Forums
    • Galleries
    • Submit ideas
    • Events
    • User groups
    • Community blog
    • Register
    • ·
    • Sign in
    • ·
    • Help
    Go To
    • Microsoft Power BI Community
    • Welcome to the Community!
    • News & Announcements
    • Get Help with Power BI
    • Desktop
    • Service
    • Report Server
    • Power Query
    • Mobile Apps
    • Developer
    • DAX Commands and Tips
    • Custom Visuals Development Discussion
    • Health and Life Sciences
    • Power BI Spanish Community
    • Translated Spanish Desktop
    • Power Platform Integration - Better Together!
    • Power Platform Integrations
    • Power Platform and Dynamics 365 Integrations
    • Training and Consulting
    • Instructor Led Training
    • Galleries
    • Community Connections & How-To Videos
    • COVID-19 Data Stories Gallery
    • Themes Gallery
    • Data Stories Gallery
    • R Script Showcase
    • Webinars and Video Gallery
    • Quick Measures Gallery
    • 2021 MSBizAppsSummit Gallery
    • 2020 MSBizAppsSummit Gallery
    • 2019 MSBizAppsSummit Gallery
    • Events
    • Ideas
    • Custom Visuals Ideas
    • Issues
    • Issues
    • Events
    • Upcoming Events
    • Community Engagement
    • T-Shirt Design Challenge 2023
    • Community Blog
    • Power BI Community Blog
    • Custom Visuals Community Blog
    • Community Support
    • Community Accounts & Registration
    • Using the Community
    • Community Feedback
    cancel
    Turn on suggestions
    Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.
    Showing results for 
    Search instead for 
    Did you mean: 
    • Microsoft Power BI Community
    • Galleries
    • Quick Measures Gallery
    • SVG Sparklines - Line

    SVG Sparklines - Line

    08-10-2018 04:04 AM - last edited 08-13-2018 14:47 PM

    deldersveld
    MVP
    63970 Views
    LinkedIn LinkedIn Facebook Facebook Twitter Twitter
    deldersveld
    deldersveld
    MVP
    • Mark as New
    • Bookmark
    • Subscribe
    • Mute
    • Subscribe to RSS Feed
    • Permalink
    • Print
    • Report Inappropriate Content

    SVG Sparklines - Line

    ‎08-10-2018 04:04 AM

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

     

     

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

     

    The x axis is built by establishing the min and max values for each date and adjusting the range so that the min corresponds to 0 and max corresponds to 100.

     

    The y axis is built by finding the min and max measure values and adjusting the range from 0 to 100 similar to the x axis. The measure value's position is based on ([Measure Value] - Min) / (Max - Min), a key calculation that I added awhile ago as another Quick Measure. In the case of the y axis though, SVG works from the top to the bottom (0 at top), so 100 - [Y] makes it render correctly. Otherwise, the chart would appear upside down.

     

    After building a table of X and Y coordinates, CONCATENATEX is the magic function that puts the points together into a line.

     

    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 LineColor 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.

     

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

     

    Thanks to my BlueGranite colleague Meagan Longoria for providing a challenge that first made me investigate whether it was possible to dynamically build SVG content in Power BI. Thanks as well to Phil Seamark for suggesting optimizations to my original DAX.

     

    eyJrIjoiOTExYmFjNTAtYzRkNy00YzU3LWJjOWUtY2M2YjM3YjI1MzkwIiwidCI6ImFjYzhhYWE1LWYxOTEtNDgyZi05MjFiLWNmNmMzM2E1ODgzMiIsImMiOjF9

    Preview file
    57 KB
    Power BI SVG Sparklines - Line.pbix
    Labels:
    • Labels:
    • Other
    Message 1 of 17
    63,970 Views
    21
    Reply
    • All forum topics
    • Previous Topic
    • Next Topic
    KatRm
    KatRm
    New Member
    • Mark as New
    • Bookmark
    • Subscribe
    • Mute
    • Subscribe to RSS Feed
    • Permalink
    • Print
    • Report Inappropriate Content

    ‎12-13-2021 05:01 AM

    Hi All,

     

    I have used the code found in the comments above and adjusted in order to create sparklines in a table in Power BI.

    What I would also like to do is to change the color based on the date_id (x-axis). (Change color after specific date) in the sparklne.

    Do you know if this is possible and how can be done?

     

    Thanks a lot

    Message 17 of 17
    13,707 Views
    0
    Reply
    DipanjanGanguly
    Microsoft DipanjanGanguly
    Microsoft
    • Mark as New
    • Bookmark
    • Subscribe
    • Mute
    • Subscribe to RSS Feed
    • Permalink
    • Print
    • Report Inappropriate Content

    ‎04-22-2021 04:01 AM

    Hi David,

     

    Please help me to increase the width of the SVG in the dax, i tried to replace as below - 

    "X",INT(200 * DIVIDE('Table'[Date] - XMinDate, XMaxDate - XMinDate)),
    "Y",INT(100 * DIVIDE([Measure Value] - YMinValue,YMaxValue - YMinValue)))
     
    But i am not getting as expected, i dont want 100 * 100 size , so if i want to change it how to do that in the formula please guide.
    Message 16 of 17
    26,349 Views
    0
    Reply
    rufmau68
    rufmau68 Helper I
    Helper I
    • Mark as New
    • Bookmark
    • Subscribe
    • Mute
    • Subscribe to RSS Feed
    • Permalink
    • Print
    • Report Inappropriate Content

    ‎03-04-2021 09:22 AM

    this work well but if i wish to show a cumulative measure in the sparkline

    how to change sparklineTable ?

    thanks

    Message 15 of 17
    28,624 Views
    0
    Reply
    Nijlal01
    Nijlal01 Helper I
    Helper I
    • Mark as New
    • Bookmark
    • Subscribe
    • Mute
    • Subscribe to RSS Feed
    • Permalink
    • Print
    • Report Inappropriate Content

    ‎09-21-2020 08:27 AM

    Great visualization! I'm trying to change the code such that the measure will be summarized in monthly buckets instead of dialy. If I change this it will give a sawtooth like visual due to the year transition (it will go from 202011 --> 202012 (+1) --> 202101 (+81). Is there an easy way to fix this?

     

    Kind regards,

     

    Lars

    Message 13 of 17
    31,924 Views
    0
    Reply
    JWedge
    JWedge Helper II
    Helper II
    In response to Nijlal01
    • Mark as New
    • Bookmark
    • Subscribe
    • Mute
    • Subscribe to RSS Feed
    • Permalink
    • Print
    • Report Inappropriate Content

    ‎09-30-2020 10:51 AM

    @Nijlal01 Add a new, or modify your existing [Year Month Column] to = Year# * 12 + Month# so for your examples you will have 24251, 24252 and 24253.  Always (+1). 

    Message 14 of 17
    31,889 Views
    0
    Reply
    Jorbangr
    Jorbangr
    Frequent Visitor
    • Mark as New
    • Bookmark
    • Subscribe
    • Mute
    • Subscribe to RSS Feed
    • Permalink
    • Print
    • Report Inappropriate Content

    ‎09-05-2018 10:13 AM

    Is it possible to make the sparkline stretch (or Fill) along the x axis when the width of the column is adjusted in the table? I was hoping to make the sparkline not be so condensed.  

    Message 4 of 17
    62,425 Views
    0
    Reply
    KrishnaPaudel
    Microsoft KrishnaPaudel
    Microsoft
    In response to Jorbangr
    • Mark as New
    • Bookmark
    • Subscribe
    • Mute
    • Subscribe to RSS Feed
    • Permalink
    • Print
    • Report Inappropriate Content

    ‎11-27-2018 06:21 PM

    Hi,

     

    Is it possible to use this when the report is connected to live connection.

     

    I was able to get the Image URL but I am unable to change the data category for the calculated measure to ImageURL. Only Uncategorized, Longitude and Latitude options are available, rest are greyed out.

     

    Thanks,

    Krishna

    Message 5 of 17
    60,940 Views
    0
    Reply
    cmajs
    cmajs
    Frequent Visitor
    In response to KrishnaPaudel
    • Mark as New
    • Bookmark
    • Subscribe
    • Mute
    • Subscribe to RSS Feed
    • Permalink
    • Print
    • Report Inappropriate Content

    ‎12-07-2018 09:37 AM

    David great post!

    Thanks for sharing it. Trying to emulate your model I only get the values string and not the charts. Do am I missing something?

    Message 6 of 17
    60,713 Views
    0
    Reply
    deldersveld
    deldersveld
    MVP
    In response to cmajs
    • Mark as New
    • Bookmark
    • Subscribe
    • Mute
    • Subscribe to RSS Feed
    • Permalink
    • Print
    • Report Inappropriate Content

    ‎12-07-2018 09:39 AM

    Did you include the "data:image/svg+xml;utf8," prefix before the SVG text and then set the measure's data category to ImageURL?

    Message 7 of 17
    60,710 Views
    0
    Reply
    cmajs
    cmajs
    Frequent Visitor
    In response to deldersveld
    • Mark as New
    • Bookmark
    • Subscribe
    • Mute
    • Subscribe to RSS Feed
    • Permalink
    • Print
    • Report Inappropriate Content

    ‎12-07-2018 09:46 AM

    I think so. This is what I have:

     

    VAR SVGImageURL = IF(HASONEVALUE('Diario'[Cadena]),
    "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())
     
     
    Message 8 of 17
    60,708 Views
    0
    Reply
    deldersveld
    deldersveld
    MVP
    In response to cmajs
    • Mark as New
    • Bookmark
    • Subscribe
    • Mute
    • Subscribe to RSS Feed
    • Permalink
    • Print
    • Report Inappropriate Content

    ‎12-07-2018 09:49 AM

    Apart from where this forum transformed my reply from an actual colon symbol to &colun, the variable looks good. Is 'Diario'[Cadena] in your table then as well?

    Message 9 of 17
    60,702 Views
    0
    Reply
    cmajs
    cmajs
    Frequent Visitor
    In response to deldersveld
    • Mark as New
    • Bookmark
    • Subscribe
    • Mute
    • Subscribe to RSS Feed
    • Permalink
    • Print
    • Report Inappropriate Content

    ‎12-07-2018 09:54 AM

    I am using Diario[Cadena] as category and yes,  I have it in my table. Any other idea of what could be wrong?

    Message 10 of 17
    60,701 Views
    0
    Reply
    deldersveld
    deldersveld
    MVP
    In response to cmajs
    • Mark as New
    • Bookmark
    • Subscribe
    • Mute
    • Subscribe to RSS Feed
    • Permalink
    • Print
    • Report Inappropriate Content

    ‎12-07-2018 09:57 AM

    The only other thing I can think of would be in the code above to fill in the "Lines" variable. Are you comfortable sharing your PBIX or the full measure?

    Message 12 of 17
    32,596 Views
    0
    Reply
    deldersveld
    deldersveld
    MVP
    In response to cmajs
    • Mark as New
    • Bookmark
    • Subscribe
    • Mute
    • Subscribe to RSS Feed
    • Permalink
    • Print
    • Report Inappropriate Content

    ‎12-07-2018 09:57 AM

    The only other thing I can think of would be in the code above to fill in the "Lines" variable. Are you comfortable sharing your PBIX or the full measure?

    Message 11 of 17
    32,596 Views
    0
    Reply
    sharath459
    sharath459
    Frequent Visitor
    • Mark as New
    • Bookmark
    • Subscribe
    • Mute
    • Subscribe to RSS Feed
    • Permalink
    • Print
    • Report Inappropriate Content

    ‎08-29-2018 06:32 PM

    Hi David,

     

    Is it possiblle to add two sparlines in the same column, for comparison purpose?

    Message 2 of 17
    62,615 Views
    0
    Reply
    deldersveld
    deldersveld
    MVP
    In response to sharath459
    • Mark as New
    • Bookmark
    • Subscribe
    • Mute
    • Subscribe to RSS Feed
    • Permalink
    • Print
    • Report Inappropriate Content

    ‎08-30-2018 08:14 PM

    You would need to figure out the DAX to separate your lines into different series based on whatever column you need, but I don't see why it couldn't be possible.

    Message 3 of 17
    62,579 Views
    0
    Reply

    Power Platform

    • Overview
    • Power BI
    • Power Apps
    • Power Pages
    • Power Automate
    • Power Virtual Agents

    • Sign in
    • Sign up

    Browse

    • Solutions
    • Partners
    • Consulting Services

    Downloads

    • Power BI Desktop
    • Power BI Mobile
    • Power BI Report Server
    • See all downloads

    Learn

    • Guided learning
    • Documentation
    • Support
    • Community
    • Give feedback
    • Webinars
    • Developers
    • Blog
    • Newsletter

    © 2023 Microsoft

    Follow Power BI

    • Privacy & cookies
    • Manage cookies
    • Terms of use
    • Trademarks
    Consumer Privacy Act (CCPA) Opt-Out Icon Your Privacy Choices