Forum Discussion

Marcl4's avatar
Marcl4
New Member
9 months ago
Solved

UDF: can we pass a column as parameter?

Dear gents,

I'm converting a normal measure to a UDF... and I have a challenge passing a column name as parameter?

My measure should become

Sub_UDF = "Countries: " & pbi.Subtitle(FACT_ITA, DIM_Geo[CountryName],8)

and I get an error Function SUMMARIZE expects a column name as argument number 2.

(tried putting VALUES, SELECTEDVALUES around the column name without success!)
Does it exist a solution or can't it be solved?

The beginning of the UDF looks like that, and we find the SUMMARIZE with a column name as second parameter

DEFINE
    FUNCTION pbi.Subtitle = (
        SourceTable : table expr,
        SourceColumn : expr,
        MaxItems : scalar
    )
    =>
        VAR BaseTable =
            FILTER(
                SUMMARIZE(
                    SourceTable,
                    SourceColumn,
                    "Cnt", [#Projects]
                ),
                [Cnt] > 0
            )
  • Hii Marcl4 

     

    In DAX UDFs you cannot pass a column reference as a parameter.
    UDF parameters only accept expressions, not real column metadata, so functions like SUMMARIZE, GROUPBY, SELECTCOLUMNS, etc. cannot use a parameter in place of a column name. This is a current limitation of DAX: column arguments must be hard-coded inside the function body.

    To achieve dynamic behavior, you must either:

    • write separate UDFs for each column, or

    • redesign the logic to work on values (e.g., using VALUES() or SELECTEDVALUE()), not structural column references.

    There is no workaround today to dynamically inject a column into SUMMARIZE via a UDF parameter.

  • Hi Marcl4  

     

    You used the wrong parameter type. Pls change the type of SourceColumn to anyref, as below:

     

    DEFINE
        FUNCTION pbi.Subtitle = (
            SourceTable : table expr,
            SourceColumn : anyref,
            MaxItems : scalar
        )
        =>
            VAR BaseTable =
                FILTER(
                    SUMMARIZE(
                        SourceTable,
                        SourceColumn,
                        "Cnt", [#Projects]
                    ),
                    [Cnt] > 0
                )

     

     

    Did I answer your question? If yes, pls mark my post as a solution and appreciate your Kudos !

     

    Thank you~

  • Marcl4 

    Workarounds:

     

    1. Create separate UDFs for each column:

    pbi.SubtitleCountry = pbi.Subtitle(FACT_ITA, DIM_Geo[CountryName], 😎
    pbi.SubtitleRegion = pbi.Subtitle(FACT_ITA, DIM_Geo[RegionName], 😎

     

    2. Use Field Parameters to pass column names (not supported in UDFs yet).

     

    3. Rewrite as regular measure with SWITCH:

    Sub_UDF =
    SWITCH(
    SELECTEDVALUE(ColumnSelector[ColumnName]),
    "Country", "Countries: " & pbi.SubtitleCountry(FACT_ITA, 8),
    "Region", "Regions: " & pbi.SubtitleRegion(FACT_ITA, 8),
    BLANK()
    )

     

    If this answer helped, please click Kudos or Accept as Solution.
    -Kedar
    LinkedIn: https://www.linkedin.com/in/kedar-pande

     

6 Replies

  • Hi Marcl4  

     

    You used the wrong parameter type. Pls change the type of SourceColumn to anyref, as below:

     

    DEFINE
        FUNCTION pbi.Subtitle = (
            SourceTable : table expr,
            SourceColumn : anyref,
            MaxItems : scalar
        )
        =>
            VAR BaseTable =
                FILTER(
                    SUMMARIZE(
                        SourceTable,
                        SourceColumn,
                        "Cnt", [#Projects]
                    ),
                    [Cnt] > 0
                )

     

     

    Did I answer your question? If yes, pls mark my post as a solution and appreciate your Kudos !

     

    Thank you~

  • Hii Marcl4 

     

    In DAX UDFs you cannot pass a column reference as a parameter.
    UDF parameters only accept expressions, not real column metadata, so functions like SUMMARIZE, GROUPBY, SELECTCOLUMNS, etc. cannot use a parameter in place of a column name. This is a current limitation of DAX: column arguments must be hard-coded inside the function body.

    To achieve dynamic behavior, you must either:

    • write separate UDFs for each column, or

    • redesign the logic to work on values (e.g., using VALUES() or SELECTEDVALUE()), not structural column references.

    There is no workaround today to dynamically inject a column into SUMMARIZE via a UDF parameter.

  • Marcl4 

    Workarounds:

     

    1. Create separate UDFs for each column:

    pbi.SubtitleCountry = pbi.Subtitle(FACT_ITA, DIM_Geo[CountryName], 😎
    pbi.SubtitleRegion = pbi.Subtitle(FACT_ITA, DIM_Geo[RegionName], 😎

     

    2. Use Field Parameters to pass column names (not supported in UDFs yet).

     

    3. Rewrite as regular measure with SWITCH:

    Sub_UDF =
    SWITCH(
    SELECTEDVALUE(ColumnSelector[ColumnName]),
    "Country", "Countries: " & pbi.SubtitleCountry(FACT_ITA, 8),
    "Region", "Regions: " & pbi.SubtitleRegion(FACT_ITA, 8),
    BLANK()
    )

     

    If this answer helped, please click Kudos or Accept as Solution.
    -Kedar
    LinkedIn: https://www.linkedin.com/in/kedar-pande

     

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi Marcl4 

    Thank you for reaching out to the Microsoft Fabric Forum Community.

    Kedar_Pande rohit1991 xifeng_L Thanks for your inputs

    I hope the information provided by users was helpful. If you still have questions, please don't hesitate to reach out to the community.

     

    • Anonymous's avatar
      Anonymous
      Not applicable

      Hi Marcl4 

      Hope everything’s going smoothly on your end. I wanted to check if the issue got sorted. if you have any other issues please reach community.