Skip to main content
Post

#PowerPlatformTip 181: 'Color SVG Icons Dynamically with a Data URI in Power Apps'

#PowerPlatformTip 181: 'Color SVG Icons Dynamically with a Data URI in Power Apps'

TL;DR: Put the SVG markup straight into the Image property as a data URI ("data:image/svg+xml;utf8," & EncodeUrl("<svg ...>")) and drive the fill attribute from your record with If() or Switch(). A single Image control recolors itself per row - red for overdue, green for done - without uploading a single PNG or fetching an external file.

💡 Challenge

Status indicators usually end up as a folder full of near-identical images: status_red.png, status_yellow.png, status_green.png, maybe a few more. Every new state means another asset to create, upload and keep in sync. The images don’t scale crisply on high-DPI screens, they bloat the app, and switching color at runtime means either an Image formula that picks between many files (which you still have to create and maintain) or visibility hacks with stacked controls.

✅ Solution

A Power Apps Image control accepts inline images through the data URI scheme, so you can hand it SVG markup directly. Because that markup is just a text string, you can concatenate a data-bound color into the SVG fill attribute. One control renders any state, the color follows the record, and nothing is ever downloaded.

🔧 How It’s Done

1. Set the Image property to an inline SVG

Wrap the SVG in "data:image/svg+xml;utf8," & EncodeUrl(...). EncodeUrl escapes the angle brackets and spaces so the string is a valid URI. It also turns the # of a hex color into %23, which is exactly what a data URI needs (a raw # would be read as the start of a fragment and cut the SVG off). Use single quotes inside the SVG so they don’t collide with the Power Fx double quotes.

"data:image/svg+xml;utf8," & EncodeUrl(
    "<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='" &
    If(ThisItem.IsOverdue, "#ff0000", "#00aa00") &
    "'><path d='M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2z'/></svg>"
)

The icon now turns red when ThisItem.IsOverdue is true and green otherwise, per record. In a gallery you see one circle per row, filled red for overdue rows and green for the rest.

2. Handle more than two states with Switch()

For a real traffic-light (red / amber / green), swap the If() for a Switch() on your status column. The example assumes a Choice column, whose text is in .Value; for a plain text column compare ThisItem.Status directly, and for a typed Choice column you can also compare against the choice options instead of text:

"data:image/svg+xml;utf8," & EncodeUrl(
    "<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='" &
    Switch(ThisItem.Status.Value,
        "Overdue",     "#ef4444",
        "Due soon",    "#f59e0b",
        "Completed",   "#22c55e",
        /* default */  "#9ca3af"
    ) &
    "'><path d='M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2z'/></svg>"
)

3. Keep it sharp and sized

Define the size of the SVG via the viewBox (and, if you like, width/height) attribute so the icon scales cleanly with its container. Set the Image control’s own Width/Height and leave ImagePosition at Fit for crisp rendering on any DPI.

4. Reuse it as a component

Move the formula into a canvas component with a custom Color and Status input property. Every gallery, form and screen then shares one icon definition - change the path once, and it updates everywhere.

🎉 Result

One Image control replaces the whole stack of status PNGs. The color reacts instantly to the underlying data, the graphic stays vector-sharp at any size, and there are zero external assets to upload, cache or break.

🌟 Key Advantages

🔸 One control, any color - the fill is bound to your record, so red/amber/green come from data, not from separate files.

🔸 No asset sprawl - nothing to upload or version; a new state is just another Switch() branch.

🔸 Crisp and lightweight - SVG scales without pixelation on high-DPI displays and keeps the app small.

🔸 Nothing to fetch - the icon is inline, so there’s no external request and no broken-link risk.

⚠️ Watch out: Use single quotes inside the SVG and always wrap it in EncodeUrl, otherwise the double quotes, # and </> characters break the string. Keep the markup lean, because the string is rebuilt per row.

🎥 Video Tutorial

🛠️ FAQ

1. Why do I use single quotes inside the SVG? The Power Fx string is delimited by double quotes, so using single quotes for the SVG attributes avoids clashing and doubled-quote escaping. EncodeUrl then turns the whole thing into a valid URI.

2. Do I need EncodeUrl, or can I paste the raw SVG? Use EncodeUrl. It escapes spaces, <, >, # and other characters that would otherwise make the data URI invalid in some browsers. It’s the reliable, portable approach.

3. Can the color come from a Dataverse or SharePoint column? Yes. Any expression that returns a hex string works - If(), Switch(), a choice column’s value, or a lookup into a color table. The icon simply reflects whatever the formula returns.

4. Does this work outside galleries? Absolutely. It works in any Image control - forms, headers, cards. Replace ThisItem with your context or global variable.

5. Is there a performance cost? For simple icons, no. The string is small and rendered client-side. Very large, complex SVGs in long galleries add cost, since the string is evaluated per row.

🔸 #PowerPlatformTip 86: ‘Free Power Apps Icons’ - where to grab SVG markup and the data-URI basics this tip builds on.

🔸 #PowerPlatformTip 145: ‘Power Platform Tools’ - includes an SVG Colorizer that generates ready-to-use EncodeUrl strings.

🔸 #PowerPlatformTip 146: ‘Encode & Decode Functions’ - background on EncodeUrl and escaping special characters.

This tip is by Marcel Lehmann, Microsoft MVP (Business Applications) and founder of the Power Platform Academy, which runs free live workshops (in German). See the next free workshop dates (in German). For companies that want this implemented for them, see KMUpower: Power Platform consulting & automation for SMEs (DACH).

This post is licensed under CC BY 4.0 by the author.

Microsoft and the Power Platform product names (for example Power Apps, Power Automate, Copilot Studio) are trademarks of the Microsoft group of companies. This is an independent website. It is not operated, sponsored or endorsed by Microsoft.