Forum Discussion
UDF not working
I have created a UDF, but it is showing multiple errors. Can anybody resolve this?
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.
- If using Power BI Desktop: Use a Measure.
- If using Tabular Editor: Use the Scripting window to deploy the measure to the model metadata.
Step 3: Handling the SVG String
To ensure the "UDF" output renders as an image:
- Ensure the return string starts with data:image/svg+xml;utf8,.
- Ensure you have escaped the # symbol as %23 if your colors aren't rendering in certain browsers.
Technical Summary
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.
6 Replies
- AshokKunwarContinued Contributor
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>"How to resolve the specific syntax errors:
- Remove UDF Headers: Power BI Desktop does not currently support createOrReplace function syntax for DAX measures. Simply create a New Measure.
- Define Table/Measure References: DAX cannot use generic parameters like XAxis : SCALAR VAL. You must reference your actual Table and Columns (e.g., 'Sales'[Date]).
- Data Category: After creating the measure, go to Measure Tools and set the Data Category to Image URL. This is required for Power BI to render the SVG instead of showing the text.
- Color Formatting: SVG colors inside DAX strings work best with Hex codes. Ensure DividerColour etc. are valid hex strings (e.g., #FFFFFF).
If this solution helps you retrieve the correct Previous Dates, please mark this as the "Accepted Solution" to help others!
- Ankan2508Advocate II
I'm creating a User Defined Function (UDF) in DAX here, not a measure, sir. So this solution is not correct.
- AshokKunwarContinued Contributor
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.
- If using Power BI Desktop: Use a Measure.
- If using Tabular Editor: Use the Scripting window to deploy the measure to the model metadata.
Step 3: Handling the SVG String
To ensure the "UDF" output renders as an image:
- Ensure the return string starts with data:image/svg+xml;utf8,.
- Ensure you have escaped the # symbol as %23 if your colors aren't rendering in certain browsers.
Technical Summary
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.
- AshokKunwarContinued Contributor
Hii Ankan2508
If this helped, please mark it as a Solution and leave a Kudo! It helps the community find the answer faster."
- v-kpoloju-msftCommunity Support
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.- v-kpoloju-msftCommunity Support
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.