Forum Discussion

Ankan2508's avatar
Ankan2508
Advocate II
7 months ago
Solved

UDF not working

I have created a UDF, but it is showing multiple errors. Can anybody resolve this?

createOrReplace

    function 'XU.Sparkline.MinMax' =
            (
                XAxis : SCALAR VAL,
                XLabel : STRING VAL,
                YValue : SCALAR VAL,
                LineColour : STRING VAL,
                MaxColour : STRING VAL,
                MinColour : STRING VAL,
                DividerColour : STRING VAL
            )
            =>
                VAR PointColour = "%23FFFFFF"
       
                /* -------------------- X Axis -------------------- */
                VAR XMin = MINX ( ALLSELECTED ( XAxis ), XAxis )
                VAR XMax = MAXX ( ALLSELECTED ( XAxis ), XAxis )
       
                /* -------------------- Y Axis -------------------- */
                VAR YMin =
                    MINX (
                        ALLSELECTED ( XAxis ),
                        YValue
                    )
       
                VAR YMax =
                    MAXX (
                        ALLSELECTED ( XAxis ),
                        YValue
                    )
       
                /* -------------------- Coordinate Table -------------------- */
                VAR SparkTable =
                    ADDCOLUMNS (
                        VALUES ( XAxis ),
                        "X",
                            INT (
                                150
                                    * DIVIDE ( XAxis - XMin, XMax - XMin )
                            ),
                        "Y",
                            INT (
                                50
                                    * DIVIDE ( YValue - YMin, YMax - YMin )
                            ),
                        "Value", YValue,
                        "Label", XLabel
                    )
       
                /* -------------------- Line Path -------------------- */
                VAR Lines =
                    CONCATENATEX (
                        SparkTable,
                        [X] & "," & 50 - [Y],
                        " ",
                        XAxis
                    )
       
                /* -------------------- Min / Max -------------------- */
                VAR MaxVal = MAXX ( SparkTable, [Value] )
                VAR MinVal = MINX ( SparkTable, [Value] )
       
                VAR MaxX = MAXX ( FILTER ( SparkTable, [Value] = MaxVal ), [X] )
                VAR MaxY = MAXX ( FILTER ( SparkTable, [Value] = MaxVal ), [Y] )
       
                VAR MinX = MAXX ( FILTER ( SparkTable, [Value] = MinVal ), [X] )
                VAR MinY = MAXX ( FILTER ( SparkTable, [Value] = MinVal ), [Y] )
       
                /* -------------------- Divider -------------------- */
                VAR DividerY = 56
       
                /* -------------------- Labels -------------------- */
                VAR XLabels =
                    CONCATENATEX (
                        SparkTable,
                        "<text x='" & [X] & "' y='70'
                            font-size='10'
                            font-family='Segoe UI'
                            text-anchor='middle'
                            fill='%236B7280'>" &
                            [Label] &
                        "</text>",
                        "",
                        XAxis
                    )
       
                /* -------------------- SVG -------------------- */
                RETURN
                    "data&colon;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 & "' />" &
           
                        XLabels &
           
                    "</svg>"
        lineageTag: 1b3a1dac-c3e6-4643-8508-c27cc7c4a1e4
  • 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&colon;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:

    1. ​Ensure the return string starts with data&colon;image/svg+xml;utf8,.
    2. ​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

  • AshokKunwar's avatar
    AshokKunwar
    Continued 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&colon;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:

    1. Remove UDF Headers: Power BI Desktop does not currently support createOrReplace function syntax for DAX measures. Simply create a New Measure.
    2. 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]).
    3. 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.
    4. 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!

    • Ankan2508's avatar
      Ankan2508
      Advocate II

      I'm creating a User Defined Function (UDF) in DAX here, not a measure, sir. So this solution is not correct.

      • AshokKunwar's avatar
        AshokKunwar
        Continued 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&colon;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:

        1. ​Ensure the return string starts with data&colon;image/svg+xml;utf8,.
        2. ​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.

  • AshokKunwar's avatar
    AshokKunwar
    Continued Contributor

    Hii Ankan2508 

    If this helped, please mark it as a Solution and leave a Kudo! It helps the community find the answer faster."

  • 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-msft's avatar
      v-kpoloju-msft
      Community 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.