Have you ever wondered how to efficiently write back multiple records to a SQL database using Power BI? In this tutorial, I’ll guide you through the steps of connecting to data and inserting it into a SQL database table in bulk. This method is particularly useful for scenarios where you need to handle large datasets without the hassle of inserting records one at a time.
Why Use Power BI for SQL Write-Back?
Power BI may not be the first tool that comes to mind when thinking about writing back data to SQL, especially since there are alternatives like Azure Data Factory or SSIS that can handle this task more efficiently. However, Power BI offers a user-friendly interface and powerful features such as various data connectors and Power Query, which makes it an attractive choice for many users. With these tools, you can easily transform and match up data, streamlining the process of writing back to SQL.
Setting Up Your Environment
Before we dive into the actual writing process, it’s essential to set up our environment correctly. I will be using Microsoft Forms data as an example, which I’ll connect to Power BI. If you’re interested in learning how to connect Microsoft Forms data directly to Power BI, be sure to check out the link in the description.

Creating the SQL Table
First, we need to create a table in SQL Server to store our data. I’ll set up a table called survey_responses with three columns: id (int), start_time (datetime), and feedback (varchar(max)). This structure mirrors the data we pulled into Power BI from Microsoft Forms.

Preparing the Insert Statement
Now that we have our SQL table ready, it’s time to create a custom column in Power BI that will generate the SQL insert statements. To do this, we go to the Add Column tab and select Custom Column. I’ll name this new column sql_insert.
The insert statement will be a long string that concatenates the values from our columns to create a valid SQL command. Here’s the basic structure:
INSERT INTO survey_responses (id, start_time, feedback) VALUES (value1, value2, value3);
To handle any potential issues with single quotes in the feedback column, we need to replace those with two single quotes using the text.replace function. This step is crucial to avoid errors during the insertion process.

Connecting Power BI to SQL Server
Next, we need to connect Power BI to our SQL Server database. This is done by selecting Get Data and choosing SQL Server. Enter your server and database names, and establish a connection. Once connected, we can start pulling data from our SQL table.

Creating a Function to Execute Insert Statements
With our connection in place, we will create a function that will execute our insert statements against the SQL database. This function will take our sql_insert column as a parameter and use the value.nativequery function to run the SQL command.

Inserting Data into SQL
Now it’s time to push our insert statements into the database. We will add a new column in Power BI where we invoke the custom function we just created. This step will execute the insert statements and write the data back to SQL.
One important thing to note is that you need to adjust your settings to allow native queries to run without requiring user approval for each one. Go to File > Options and settings > Options and uncheck the option for user approval.

Checking the Results
After running the inserts, we can check our SQL database to see if the records have been written successfully. You should see all the records that were inserted, although you might also notice some duplicates. Don’t worry; we’ll address that shortly.

Handling Duplicates
To prevent duplicates in our SQL table, we need to modify our insert statement to ensure that the id values are unique. We can do this by adding a TRY…CATCH block around our insert logic. This way, if an insert fails due to a duplicate key, the process won’t break, and we can handle the error gracefully.

Finalizing the Setup
Once we’ve implemented the duplicate handling, it’s time to finalize our setup. We’ll remove any unnecessary steps and ensure that our Power BI report can refresh without issues. After applying these changes, we should be able to write back multiple records without running into problems.

Testing the Write-Back Functionality
Now, let’s test our setup by submitting a new response through Microsoft Forms. Once we submit the form, we can refresh our Power BI report to see if the new record appears in our SQL table.

After refreshing, you should see the new response added to your SQL database, confirming that our bulk write-back functionality is working smoothly.
Conclusion
In this tutorial, we explored how to write back multiple records to a SQL database using Power BI. By leveraging Power Query and SQL insert statements, we can efficiently handle bulk data updates directly from Power BI. This method not only simplifies the process but also enhances the interactivity and usability of your reports.
If you found this tutorial helpful, check out my training courses for in-depth learning!


