---
name: svg-visuals
description: SVG generation via DAX measures and extension measures with ImageUrl data category for inline visualizations in PBIR reports, including ready templates for matrix and table cells, new card images and per-button images on the new button slicer. Automatically invoke when the user mentions "SVG visual", "DAX sparkline", "SVG measure", "inline graphics with DAX", "ImageUrl data category", "extension measure", or asks to create any DAX-generated chart (progress bars, bullet charts, KPI indicators, data bars, gauges, donut charts, lollipop charts, dumbbell charts, status pills, overlapping bars, boxplots, IBCS bars, jitter plots, box-and-whisker charts).
---
# SVG Visuals via DAX Measures (PBIR)
> **Use `pbir` for every report mutation.** The `pbir-format` skill is read-only schema context.
> If the CLI is unavailable or lacks an operation, stop and report the gap.
Generate inline SVG graphics using DAX measures that return SVG markup strings. These render as images in table, matrix, card, image, and slicer visuals. Store as extension measures in `reportExtensions.json`.
## How It Works
1. A DAX measure returns an SVG string prefixed with `data:image/svg+xml;utf8,`
2. The measure's `dataCategory` is set to `ImageUrl`
3. Power BI renders the SVG as an image in supported visuals
## Supported Visuals
- Table (`tableEx`): `grid.imageHeight` / `grid.imageWidth` -- `references/svg-table-matrix.md`
- Matrix (`pivotTable`): same as table -- `references/svg-table-matrix.md`
- Image (`image`): `sourceType='imageData'` + `sourceField` -- `references/svg-image-visual.md`
- Card/New (`cardVisual`): the `image` object (image area) or `cardImage` (callout image) -- `references/svg-template-matrix.md`, patterns in `references/svg-card-slicer.md`
- Slicer/New (`advancedSlicerVisual`): one image per button through the image "Select from data" slot -- `references/svg-template-matrix.md`
## Start From a Template
Before writing DAX by hand, check `references/svg-template-matrix.md`: it maps each pattern (bullet, progress, pill, sparkline, dumbbell, diverging, delta, pin, gauge) to the hosts it fits (an SVG on its own goes in an image visual, a card is for a number), shows the verified PBIR wiring per host, and gives the one-step `pbir visuals svg` / `pbir add gauge-card` commands that write the pattern as a thin report measure and wire it in. Hand-write SVG DAX only for a shape no template covers.
## Workflow: Creating an SVG Measure
### Step 0: Design and Preview
Before writing DAX, design the SVG visually:
1. **Query the model first** -- use DAX Studio or Tabular Editor CLI to get actual values with the intended filter context. Use real numbers, not placeholders.
2. **Write static SVG to a temp file** -- save to `/tmp/mockup.svg` and `open` it in a browser to preview layout, colors, and proportions.
3. **Ask for feedback** before converting to DAX -- iterating on static SVG is far easier than on DAX string concatenation.
4. **Colors must be hex codes with `#`** -- e.g., `fill='#2B7A78'`. Never use `%23` URL encoding or named colors. Always hex.
### Step 1: Create the Extension Measure
Create the extension measure in `reportExtensions.json` manually (see the `pbir-format` skill in the pbip plugin for JSON structure).
```python
# Example using pbir_object_model (if available):
report.add_extension_measure(
table="Orders",
name="Sparkline SVG",
expression='''
VAR _Prefix = "data:image/svg+xml;utf8,"
RETURN _Prefix & _Bar & _Suffix
''',
data_type="Text",
data_category="ImageUrl",
display_folder="SVG Charts",
)
report.save()
```
### Step 1b: Review
Before presenting the measure to the user, dispatch the `svg-reviewer` agent to validate syntax and provide design feedback.
### Step 2: Bind to a Visual
Extension measures use `"Schema": "extension"` in the SourceRef:
```json
{
"field": {
"Measure": {
"Expression": {
"SourceRef": {"Schema": "extension", "Entity": "Orders"}
},
"Property": "Sparkline SVG"
}
}
}
```
For **image visuals**, bind the SVG measure through `pbir add visual image --image`; see
`references/svg-image-visual.md`.
### Step 3: Validate
Validate with `pbir validate "Report.Report" --all` and inspect bindings with
`pbir visuals bind "...Visual" --show`.
## Prefer UDF Libraries Over Custom DAX
Before writing a custom SVG measure from scratch, check whether an existing UDF library already provides the chart type:
- **PowerofBI.IBCS** (Andrzej Leszkiewicz) -- IBCS-compliant bar, column, waterfall, pin, small multiples, and P&L charts. Preferred for business reporting with AC/PY/BU/FC comparisons. Install from https://daxlib.org/package/PowerofBI.IBCS/
- **DaxLib.SVG** (Jake Duddy) -- general-purpose sparklines, bars, boxplots, heatmaps, jitter, violin, progress bars, pills. Install from https://daxlib.org/package/DaxLib.SVG/ -- source at https://github.com/daxlib/dev-daxlib-svg
- **PowerBI MacGuyver Toolbox** (Stepan Resl / Data Goblins) -- C# scripts that generate SVG measures via Tabular Editor
To check if a library is installed, look for functions/measures starting with `PowerofBI.IBCS.`, `Viz.`, `Compound.`, or `Element.`. Only write custom SVG DAX when no library function covers the required visualization. See `references/community-examples.md` for full function listings and additional libraries.
### Installing UDF libraries
UDF libraries are installed into the semantic model, not the report. Use one of these tools:
- **Tabular Editor CLI** (`te` command) -- use the `te-docs` skill for guidance
- **Power BI MCP server** -- if available, use it to modify the model directly
- **`connect-pbid` skill** -- connect to Power BI Desktop's local Analysis Services instance via TOM/PowerShell
- **`tmdl` skill** -- edit TMDL files directly in a PBIP project (last resort)
## DAX SVG Conventions
### Measure Structure (VAR Pattern)
Every SVG measure must follow a strict VAR-based structure. Organize code into clearly separated regions:
```dax
SVG Measure =
-- CONFIG: Input fields and visual parameters
VAR _Actual = [Sales Amount]
VAR _Target = [Sales Target]
VAR _Scope = ALLSELECTED ( 'Product'[Category] )
-- CONFIG: Colors
VAR _BarColor = "#5B8DBE"
VAR _TargetColor = "#333333"
-- NORMALIZATION: Scale values to SVG coordinate space
VAR _AxisMax = CALCULATE( MAXX( _Scope, [Sales Amount] ), REMOVEFILTERS( 'Product'[Category] ) ) * 1.1
VAR _AxisRange = 100
VAR _ActualNormalized = DIVIDE( _Actual, _AxisMax ) * _AxisRange
-- SVG ELEMENTS: One VAR per visual element
VAR _SvgPrefix = "data:image/svg+xml;utf8,"
-- ASSEMBLY: Combine in rendering order (back to front)
VAR _SVG = _SvgPrefix & _Sort & _Bar & _TargetLine & _SvgSuffix
RETURN _SVG
```
Key conventions:
- **CONFIG section first** -- input measures, scope column, colors, font settings. Users change only this section.
- **NORMALIZATION section** -- scale raw values to SVG coordinate space (see below)
- **SVG ELEMENTS** -- one VAR per ``, ``, ``, ``, etc.
- **ASSEMBLY** -- concatenate elements in document order (first = back layer, last = front)
- **`` sort trick** -- embed `FORMAT(_Actual, "000000000000")` in a `` tag so the table/matrix can sort by the SVG column
### Axis Normalization (Critical)
SVG coordinates must be normalized to a fixed range. Raw measure values (e.g., 1,234,567) cannot be used directly as pixel coordinates. The standard pattern:
```dax
-- 1. Define the SVG coordinate range
VAR _BarMin = 0 -- leftmost position (or offset for labels)
VAR _BarMax = 100 -- rightmost position
-- 2. Find the maximum value across all rows in the visual's filter context
VAR _Scope = ALLSELECTED( 'Table'[GroupColumn] )
VAR _MaxInScope = CALCULATE( MAXX( _Scope, [Measure] ), REMOVEFILTERS( 'Table'[GroupColumn] ) )
VAR _AxisMax = _MaxInScope * 1.1 -- 10% padding
-- 3. Normalize each value to the SVG range
VAR _AxisRange = _BarMax - _BarMin
VAR _Normalized = DIVIDE( _Actual, _AxisMax ) * _AxisRange
```
Use `ALLSELECTED` for the scope when the chart should respond to slicer context. Use `ALL` for a fixed axis across all filter contexts. The `* 1.1` padding prevents bars from touching the edge.
### HASONEVALUE Guard
Table/matrix SVG measures must guard against subtotal/total rows where multiple categories are in scope:
```dax
IF( HASONEVALUE( 'Table'[GroupColumn] ),
-- SVG code here
)
```
Without this guard, the measure evaluates on grand total rows with meaningless aggregated values.
### Escaping and Color Rules
- **Single quotes for SVG attributes** -- avoids DAX double-quote escaping: `fill='#2196F3'`
- **Double quotes in DAX**: escape as `""` (DAX convention)
- **`viewBox`** for responsive scaling: `viewBox='0 0 100 25'`
- **`xmlns`** required on `