Share feedback directly with Fabric product managers, participate in targeted research studies and influence the Fabric roadmap.
Sign up nowGet Fabric certified for FREE! Don't miss your chance! Learn more
I have created a UDF, but it is showing multiple errors. Can anybody resolve this?
Solved! Go to Solution.
If you want to use this as a "Function" without creating a standard measure for every visual, you must use the DEFINE syntax in the DAX Query View.
Step 1: The "Function" Definition
You cannot use createOrReplace. Instead, use the DEFINE VAR syntax. This allows the logic to act as a function within the scope of your query.
DEFINE
-- This acts as your "UDF" logic
MEASURE 'Table'[XU.Sparkline.MinMax] =
VAR LineColour = "#0078D4"
VAR MaxColour = "#228B22"
VAR MinColour = "#DC143C"
VAR PointColour = "%23FFFFFF"
-- Your SVG Logic here...
VAR MaxX = [MaxX_Logic]
VAR MaxY = [MaxY_Logic]
RETURN
"data:image/svg+xml;utf8,<svg ...>" &
"<circle cx='" & MaxX & "' cy='" & 50 - MaxY & "' ... />" &
"</svg>"
-- This part executes the "Function"
EVALUATE
SUMMARIZECOLUMNS(
'Table'[Category],
"Sparkline", 'Table'[XU.Sparkline.MinMax]
)
Step 2: Why the createOrReplace syntax is failing
The createOrReplace syntax is part of TMSL (Tabular Model Scripting Language) or C# scripts used in Tabular Editor—it is not valid DAX code that can be pasted into the formula bar.
Step 3: Handling the SVG String
To ensure the "UDF" output renders as an image:
While Power BI doesn't have a specific "UDF" object, the DAX Query View is the designated environment for defining and testing function-like logic before committing it to the model as a Measure.
Hi @Ankan2508,
Thank you for reaching out to the Microsoft Fabric Community Forum. Also, thanks to @AshokKunwar, for his inputs on this thread.
Has your issue been resolved? If the response provided by the community member @AshokKunwar, addressed your query, could you please confirm? It helps us ensure that the solutions provided are effective and beneficial for everyone.
Hope this helps clarify things and let me know what you find after giving these steps a try happy to help you investigate this further.
Thank you for using the Microsoft Community Forum.
Hi @Ankan2508,
Just wanted to follow up. If the shared guidance worked for you, that’s wonderful hopefully it also helps others looking for similar answers. If there’s anything else you'd like to explore or clarify, don’t hesitate to reach out.
Thank you.
Hii @Ankan2508
If this helped, please mark it as a Solution and leave a Kudo! It helps the community find the answer faster."
hii @Ankan2508
To fix the errors, create a standard New Measure and use the following code. I have cleaned up the syntax to be valid DAX.
XU.Sparkline.MinMax =
/* Define your parameters here or use column references */
VAR LineColour = "#0078D4"
VAR MaxColour = "#228B22"
VAR MinColour = "#DC143C"
VAR DividerColour = "#E5E7EB"
VAR PointColour = "%23FFFFFF"
/* -------------------- Data Context -------------------- */
// Replace 'Table'[Date] and [YourValueMeasure] with your actual fields
VAR SparkTable =
ADDCOLUMNS (
ALLSELECTED ( 'Table'[Date] ),
"X_Raw", 'Table'[Date],
"Y_Raw", [YourValueMeasure]
)
/* -------------------- X Axis -------------------- */
VAR XMin = MINX ( SparkTable, [X_Raw] )
VAR XMax = MAXX ( SparkTable, [X_Raw] )
/* -------------------- Y Axis -------------------- */
VAR YMin = MINX ( SparkTable, [Y_Raw] )
VAR YMax = MAXX ( SparkTable, [Y_Raw] )
/* -------------------- Coordinate Scaling -------------------- */
VAR CoordinateTable =
ADDCOLUMNS (
SparkTable,
"X", INT ( 150 * DIVIDE ( [X_Raw] - XMin, XMax - XMin ) ),
"Y", INT ( 50 * DIVIDE ( [Y_Raw] - YMin, YMax - YMin ) )
)
/* -------------------- Line Path -------------------- */
VAR Lines =
CONCATENATEX (
CoordinateTable,
[X] & "," & 50 - [Y],
" ",
[X_Raw] ASC
)
/* -------------------- Min / Max Logic -------------------- */
VAR MaxVal = MAXX ( CoordinateTable, [Y_Raw] )
VAR MinVal = MINX ( CoordinateTable, [Y_Raw] )
VAR MaxPoint = FILTER ( CoordinateTable, [Y_Raw] = MaxVal )
VAR MaxX = MAXX ( MaxPoint, [X] )
VAR MaxY = MAXX ( MaxPoint, [Y] )
VAR MinPoint = FILTER ( CoordinateTable, [Y_Raw] = MinVal )
VAR MinX = MAXX ( MinPoint, [X] )
VAR MinY = MAXX ( MinPoint, [Y] )
VAR DividerY = 56
/* -------------------- SVG Construction -------------------- */
RETURN
"data:image/svg+xml;utf8," &
"<svg xmlns='http://www.w3.org/2000/svg' viewBox='-7 -7 164 85'>" &
"<line x1='0' x2='150' y1='" & DividerY & "' y2='" & DividerY & "' stroke='" & DividerColour & "' stroke-width='1' />" &
"<polyline fill='none' stroke='" & LineColour & "' stroke-width='3' stroke-linecap='round' stroke-linejoin='round' points='" & Lines & "' />" &
"<circle cx='" & MaxX & "' cy='" & 50 - MaxY & "' r='4' stroke='" & MaxColour & "' stroke-width='3' fill='" & PointColour & "' />" &
"<circle cx='" & MinX & "' cy='" & 50 - MinY & "' r='4' stroke='" & MinColour & "' stroke-width='3' fill='" & PointColour & "' />" &
"</svg>"
If this solution helps you retrieve the correct Previous Dates, please mark this as the "Accepted Solution" to help others!
I'm creating a User Defined Function (UDF) in DAX here, not a measure, sir. So this solution is not correct.
If you want to use this as a "Function" without creating a standard measure for every visual, you must use the DEFINE syntax in the DAX Query View.
Step 1: The "Function" Definition
You cannot use createOrReplace. Instead, use the DEFINE VAR syntax. This allows the logic to act as a function within the scope of your query.
DEFINE
-- This acts as your "UDF" logic
MEASURE 'Table'[XU.Sparkline.MinMax] =
VAR LineColour = "#0078D4"
VAR MaxColour = "#228B22"
VAR MinColour = "#DC143C"
VAR PointColour = "%23FFFFFF"
-- Your SVG Logic here...
VAR MaxX = [MaxX_Logic]
VAR MaxY = [MaxY_Logic]
RETURN
"data:image/svg+xml;utf8,<svg ...>" &
"<circle cx='" & MaxX & "' cy='" & 50 - MaxY & "' ... />" &
"</svg>"
-- This part executes the "Function"
EVALUATE
SUMMARIZECOLUMNS(
'Table'[Category],
"Sparkline", 'Table'[XU.Sparkline.MinMax]
)
Step 2: Why the createOrReplace syntax is failing
The createOrReplace syntax is part of TMSL (Tabular Model Scripting Language) or C# scripts used in Tabular Editor—it is not valid DAX code that can be pasted into the formula bar.
Step 3: Handling the SVG String
To ensure the "UDF" output renders as an image:
While Power BI doesn't have a specific "UDF" object, the DAX Query View is the designated environment for defining and testing function-like logic before committing it to the model as a Measure.
If you love stickers, then you will definitely want to check out our Community Sticker Challenge!
Check out the January 2026 Power BI update to learn about new features.
| User | Count |
|---|---|
| 62 | |
| 58 | |
| 45 | |
| 21 | |
| 18 |
| User | Count |
|---|---|
| 119 | |
| 116 | |
| 37 | |
| 34 | |
| 30 |