Write back to a Fabric SQL database: the user data function

A Fabric user data function can save a value entered in Power BI to a SQL database. Connect the function to the database, validate the input, and pass values separately to a parameterised SQL statement. Commit the transaction and return a text message that Power BI can display.

Analysts · ·

The problem

Your report shows data from a Fabric SQL database and people want to correct or add a value without leaving the report. The button side is easy to click together. The function behind it is the part that can corrupt data or fail with a message nobody understands. This Pattern is for analysts and engineers who write that function.

What is generally available and what is not

  • Translytical task flows: generally available since the March 2026 Power BI update, per Microsoft’s update archive. The Cosmos DB in Fabric guide still says public preview; that page differs.
  • Fabric user data functions: the overview and service limits pages carry no preview label (checked 2026-11-29). Their integration with Data Warehouse T-SQL is a separate feature labelled preview.
  • SQL database in Fabric: no preview label on its overview page. Its limitations page marks only the ALTER DATABASE SET options as preview.

Steps

Five steps of a write-back function: validate the inputs, connect through the alias, run parameterised SQL, commit, return a text. A dashed box says bad input raises UserThrownError and a database failure is caught, rolled back and reported.

What the function does, in order, and how failures reach the report.

The steps follow Microsoft’s tutorial, which uses the AdventureWorksLT sample in a SQL database.

  1. Create the SQL database and load the sample data. In the Fabric portal create a SQL database, then choose the Sample data option to load AdventureWorksLT. Note the database name.

  2. Create the user data functions item. In a workspace select New item, then User data functions under Develop data. Give it a name, for example sqlwriteback, then select New function.

  3. Add the connection. Select Manage connections, Add data connection, pick the SQL database and connect. Fabric generates an alias for the connection. Copy it. You need access permissions on the database for this step.

  4. Paste the function and set the alias. Adapted from the tutorial: it adds an empty-input check and rolls back on failure, and it raises an error instead of returning a success text when the write fails.

    import fabric.functions as fn
    import uuid
    
    udf = fn.UserDataFunctions()
    
    @udf.connection(argName="sqlDB", alias="<connection-alias>")
    @udf.function()
    def write_one_to_sql_db(sqlDB: fn.FabricSqlConnection, productDescription: str, productModelId: int) -> str:
        # Validate before touching the database
        if not productDescription.strip():
            raise fn.UserThrownError("The description cannot be empty.")
        if len(productDescription) > 200:
            raise fn.UserThrownError("Descriptions have a 200 character limit. Please shorten your description.")
    
        connection = sqlDB.connect()
        cursor = connection.cursor()
        try:
            # ? placeholders: the values never become part of the SQL text
            cursor.execute(
                "INSERT INTO [SalesLT].[ProductDescription] (Description) "
                "OUTPUT INSERTED.ProductDescriptionID VALUES (?)",
                productDescription,
            )
            new_id = cursor.fetchall()[0][0]
    
            # The tutorial generates a placeholder culture value. Use a real culture code in your own data.
            culture = str(uuid.uuid4())[:6]
            cursor.execute(
                "INSERT INTO [SalesLT].[ProductModelProductDescription] "
                "(ProductModelID, ProductDescriptionID, Culture) VALUES (?, ?, ?)",
                (productModelId, new_id, culture),
            )
            connection.commit()
        except Exception:
            connection.rollback()
            raise fn.UserThrownError("The description could not be saved. Try again later.")
        finally:
            cursor.close()
            connection.close()
    
        return "Product description was added"
    

    Each ? stands for a value. In the second cursor.execute call, the three placeholders correspond, in order, to the model ID, the description ID and the culture code.

    Two rules from the programming model: parameter names must be camelCase without underscores, and every parameter needs a type annotation.

  5. Publish and run once. Select Publish. In the Functions explorer hover over the function and select Run. Enter a description and a product model id between 1 and 127, then run. Read the output and the logs. If it fails, check your access to the database first.

  6. Share it with the report readers. On the function item select Share, open Additional permissions, pick Execute Functions and View Functions Logs, add the users or groups and send.

Next, Pattern 15 connects a report to this function.

Copy this prompt

To adapt the Python function to your Fabric database, paste this into your assistant’s chat with the action, table, parameters and connection alias. Ask for a draft that validates input, performs the change with parameterized SQL and returns a message the report can display.

Write a Fabric user data function in Python that <action> in <schema>.<table>
of a Fabric SQL database. Parameters: <name: type> (camelCase, type-annotated).
Use @udf.connection with the alias <connection-alias>. Validate every input and
raise fn.UserThrownError with a readable message. Use “?” placeholders for values. Avoid string
concatenation. Commit on success, roll back and raise on failure.
Return a short success message as str.

Check that the return type is str, that no SQL is built by joining the input into a string, and that a failed write cannot return a success text.

Limits

  • A function runs for at most 240 seconds, accepts 4 MB of request parameters and returns at most 30 MB. Calls through a public endpoint stop at 100 seconds.
  • Published functions run on Python 3.11. The test capability in Develop mode uses 3.12.
  • The Culture value in the second insert is a placeholder, as in the tutorial. Do not copy it into real data.
  • Both inserts commit together, but the article does not test what happens when two people write at once.
  • Sample table and column names come from AdventureWorksLT. Check yours against your own database.
  • A SQL database on a trial capacity is limited to three databases.

Sources

Author

Rodrigo Lombana Reina, data engineer. Personal opinions. Checked against the documentation on 2026-11-29.

Questions

What is a Fabric user data function?

A reusable Python function that runs serverless inside Fabric and can be called from pipelines, notebooks, Activator rules, Power BI translytical task flows or any system through its own REST endpoint. Microsoft documents a Python 3.11.9 runtime, PyPI libraries and managed connections to Fabric data sources. The function code is the same whoever calls it.

How do I connect a user data function to a Fabric SQL database?

In the function item select Manage connections, then Add data connection, pick the SQL database and connect. Fabric creates an alias for the connection. In the code, decorate the function with @udf.connection(argName="sqlDB", alias="<alias>") and call sqlDB.connect() to get a pyodbc connection, which is what Microsoft's tutorial recommends for SQL.

Why does my user data function not show up in the Power BI button?

Because it does not return a string. Both the tutorial and the button page say that data function buttons require the function to return a str, and that missing functions are usually a return type problem. Change the return annotation to str, return a message, publish again and reopen the function picker in the button settings.

How do I show an error message to the report reader?

Raise fn.UserThrownError with the text you want shown, for example when a description is longer than 200 characters. The report then shows Something went wrong and the reader can select View details to read your message. Microsoft recommends validating each parameter this way and writing a friendly success message too.

How do I avoid SQL injection in a user data function?

In this example, each question mark marks where a value goes in the statement. Pass the values to cursor.execute separately, in the same order. Validate their length and range before writing to the database. This keeps user input out of the SQL text.

Who needs permission to run the function from a report?

Everyone who presses the button. On the function item select Share, open Additional permissions and pick Execute Functions and View Functions Logs, then add the users or groups. People without it get a Something went wrong response. You also need access to the SQL database yourself to test the function.

What are the limits of a user data function, and is SQL database in Fabric generally available?

A function can run for 240 seconds, take 4 MB of request parameters and return 30 MB. Logs are kept 30 days. The SQL database overview page carries no preview label, while its limitations page marks only the ALTER DATABASE SET options as preview, and a trial capacity allows three databases. Checked 2026-11-29.

Portrait of Rodrigo Lombana Reina

Rodrigo Lombana Reina, data engineer. Data problems solved, step by step.

About Rodrigo · The opinions and texts on this site are my own.

My LinkedIn profile