Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
MangoMagic
Regular Visitor

Fabric User Data Function parameter defaults?

Was trying to create a function which runs a stored proc and returns some results, is there a way to set default values in parameters, e.g. to make parameters optional and default them if no value is given?

I tried following but it doesn't seem to work, the function doesn't even publish when I try and set default values 😞

 

@udf.connection(argName="sqlDB",alias="Control")
@udf.function()
def GetWatermark(sqlDB: fn.FabricSqlConnection, ID: int, option: str = "Get watermark", description: str = None)-> list:
    option = option.lower()
    
    if option == "get watermark":
        query = "exec exec [dbo].[Get_ETLSettings] @ID = {ID}, @Option = '{option}';"

    #if option == "hour":

    # Establish a connection to the SQL database
    connection = sqlDB.connect()
    cursor = connection.cursor()

    # Execute the query
    cursor.execute(query)

    # Fetch all results
    results = []
    for row in cursor.fetchall():
        results.append(row)

    # Close the connection
    cursor.close()
    connection.close()
        
    return results

 

1 ACCEPTED SOLUTION
v-karpurapud
Community Support
Community Support

Hi @MangoMagic 

Thank you for contacting the Microsoft Fabric Community Forum.

 

Currently, Microsoft Fabric's Python User Defined Functions (UDFs) created with the @udf.function decorator do not allow default values or optional parameters in their definitions. If you try to set a default value for a parameter, such as option: str = "Get watermark", the function will not publish or compile correctly in the Fabric environment.

 

To avoid issues, make sure all parameters are explicitly defined and provided when calling the function. If you need default behavior, handle it inside the function by checking if a parameter is empty or null and then assigning a value as needed. This method works within the platform's current limitations and ensures your UDFs will run as expected.

 

from microsoft.fabric import udf

import microsoft.fabric.functions as fn



@udf.connection(argName="sqlDB", alias="Control")

@udf.function()

def GetWatermark(sqlDB: fn.FabricSqlConnection, ID: int, option: str, description: str) -> list:

   

    if not option:

        option = "get watermark"

    else:

        option = option.lower()



    if option == "get watermark":

        query = f"EXEC [dbo].[Get_ETLSettings] @ID = {ID}, @Option = '{option}';"

    else:

      

        query = f"EXEC [dbo].[Get_ETLSettings] @ID = {ID}, @Option = '{option}';"



    connection = sqlDB.connect()

    cursor = connection.cursor()



    cursor.execute(query)

    results = cursor.fetchall()



    cursor.close()

    connection.close()



    return results

 

Regards,

Karpurapu D,

Microsoft Fabric Community Support Team.

View solution in original post

2 REPLIES 2
v-karpurapud
Community Support
Community Support

Hi @MangoMagic 

Thank you for contacting the Microsoft Fabric Community Forum.

 

Currently, Microsoft Fabric's Python User Defined Functions (UDFs) created with the @udf.function decorator do not allow default values or optional parameters in their definitions. If you try to set a default value for a parameter, such as option: str = "Get watermark", the function will not publish or compile correctly in the Fabric environment.

 

To avoid issues, make sure all parameters are explicitly defined and provided when calling the function. If you need default behavior, handle it inside the function by checking if a parameter is empty or null and then assigning a value as needed. This method works within the platform's current limitations and ensures your UDFs will run as expected.

 

from microsoft.fabric import udf

import microsoft.fabric.functions as fn



@udf.connection(argName="sqlDB", alias="Control")

@udf.function()

def GetWatermark(sqlDB: fn.FabricSqlConnection, ID: int, option: str, description: str) -> list:

   

    if not option:

        option = "get watermark"

    else:

        option = option.lower()



    if option == "get watermark":

        query = f"EXEC [dbo].[Get_ETLSettings] @ID = {ID}, @Option = '{option}';"

    else:

      

        query = f"EXEC [dbo].[Get_ETLSettings] @ID = {ID}, @Option = '{option}';"



    connection = sqlDB.connect()

    cursor = connection.cursor()



    cursor.execute(query)

    results = cursor.fetchall()



    cursor.close()

    connection.close()



    return results

 

Regards,

Karpurapu D,

Microsoft Fabric Community Support Team.

Thank you, that worked 😃

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June FBC25 Carousel

Fabric Monthly Update - June 2025

Check out the June 2025 Fabric update to learn about new features.

June 2025 community update carousel

Fabric Community Update - June 2025

Find out what's new and trending in the Fabric community.

Top Solution Authors