Update 2021 March:
Update 2019 April:
If you want to export the data model from either Power BI Desktop or Power BI Service to CSV or SQL Server check this out.
Hans Peter Pfister has already explained how to export data from Power BI Desktop to SQL Server using R scripts. Although Hans has done a brilliant job, it might be a bit hard to make it work if you don’t have any R experience and you don’t even know how to install and call R libraries. That’s so true about me, I’m NOT an R guy, but, who knows, maybe I will be.
But, what if you don’t want to go with R? If you are more involved with BI than analytics, then using R might not really be your cup of tea. Luckily, there is another way to export your Power BI data to SQL Server which is more BI friendly. You can export Power BI data to SQL Server using SSIS (SQL Server Integration Services). So if you are familiar with SSIS, then it might be your preferred choice.
With respect to Hans, in this post, I explain his method of exporting data from Power BI Desktop to SQL Server more in detail so that anyone who is not that familiar with R can make it work. I also explain how to export data from Power BI Desktop to SQL Server using SSIS. If there is any other methods you’re aware of please let me know in the comment section below.
Exporting Data from Power BI Desktop to SQL Server with R
As stated before, Hans has already explained this method here. So I don’t explain exactly what he did, but, I use his method to export data from the existing Power BI Desktop model to SQL Server and I explain it step-by-step.
To make this method work you need to:
- The latest version of Power BI Desktop, you can download it from here
- Have access to an instance of SQL Server, either on your own machine or on a server in your local network to export the data to
- Either install R for Windows, you can download it from here OR using an existing R-Server OR install SQL Server 2016 R Services
- Install RODBC library for R, you can download the library from here
Note: I haven’t installed R Studio and nothing went wrong.
Installing RODBC Library for R and SQL Server R Services
As mentioned earlier, you can install R OR SQL Server R Services OR R-Server, but, as I haven’t tried R-Server myself I just explain how to install RODBC in R and SQL Server R Services.
You have to download the library from the link provided above, then extract the contents of the zip file which contains an “RODBC” folder. Then all you really need to do is to copy the “RODBC” to the “library” folder that exists in either R or SQL Server 2016 folders in your “Program Files” folder.
How Does It Work?
Open an existing Power BI Desktop model that you’re willing to export its data to a SQL Server table and follow the steps below: (I use the “Internet Sales” model created on top of AdventureWorksDW. You can download my Power BI Desktop model at the end of this post.)
- Open the Power BI Desktop model
- Click “Edit Queries”
- Right-click on any desired Table you want to export to SQL Server and click “Reference”
- Rename the duplicated table to “Internet Sales Export”
- Now click on “Run R Script” from the “Transform” tab from the ribbon
- Copy and paste the R script below:
library(RODBC) conn <- odbcDriverConnect("driver=SQL Server; server=SQL_SERVER_INSTANCE; Database=SQLDB") odbcClearError(conn) sqlSave(conn, dataset, tablename="TABLE_NAME",rownames=FALSE, safer=FALSE, append=TRUE) close(conn)
- Replace the highlighted parts with your object names then click OK
Note: If you have more than one SQL Server instance then you need to add an extra “\” (backslash) in the server name. The first backslash is considered as an escape character. So your script should look like the below figure.
If everything is fine then you’ll see an empty table in Query Editor without any error messages. Now open SSMS and query the “Exported_From_PowerBI” table to see the exported data.
Note: As you see in the “Run R Script” window, there is a description after the script block showing your current R home directory.
You can change it from Power BI desktop Options as below:
- Click “File” menu
- Click “Options and settings”
- Click “Options”
- Click “R Scripting”
- Select a “Detected R home directory” from the dropdown list then click OK
Note: As you see we’re modifying the query. This is why I created a duplicate from the base query. You may want to put the R script to the base query then remove it after the data is exported to SQL Server.
Hans briefly explained the R script, so I encourage you to have a look at his post to get a better understanding of the R script. For more detailed information check this out.
I tried the above method on a bigger table containing more than 11 million rows and I should say that the performance was not too good. It took more than 15 minutes to export about 1.5 million rows which is not that impressive.
Exporting Power BI Desktop Data to SQL Server Using SSIS
In this method, I connect to Power BI Desktop from SSIS to export data to SQL Server. I explained how to connect to Power BI Desktop more in detail here.
- The latest version of Power BI Desktop
- Accessing an instance of SQL Server
- Accessing SSIS (SQL Server Integration Services)
- SSDT (SQL Server Data Tools), you can download here. I use SSDT 2015
How Does It Work?
As stated before we’ll connect to a Power BI Desktop using its random local port from the SSIS package then we export data from the desired table to SQL Server.
Note: Keep in mind that the random port number will change if you close your Power BI Desktop file and reopen it. Therefore, this method is a temporary solution for exporting a larger amount of data from Power BI Desktop to SQL Server.
First of all, we need to find the port number of the local SSAS instance created by the Power BI Desktop. To learn more about the Power BI Desktop port number check this out.
To quickly find the port number:
- Navigate to the directory below:
%LocalAppData%\Microsoft\Power BI Desktop\AnalysisServicesWorkspaces
- Open “Data” folder
- Find “msmdsrv.port.txt” in the folder and open it. It contains the random local SSAS instance port number used by Power BI Desktop
Now we need to create an SSIS project in SSDT.
- Open SSDT
- Create a new “Integration Services” project
- Put a “Data Flow Task” on Control Flow
- In the data flow add an “OLEDB Source”
- Double click OLEDB Source to open “OLEDB Source Editor”
- Click “New” to create a new OLEDB connection manager
- Click “New” one more time
- Set “Provider” to: “Native OLEDB\Microsoft OLEDB Provider for Analysis Services 13.0”
- Set “Location:” to “localhost:XXXXX” which XXXXX is your local Power BI port number
- Select “Initial catalog” from the dropdown list
- Click “Test Connection” to make sure the connection is successful
- You have to click OK several times to get back to “OLEDB Source Editor”
- So far we created a connection manager that you can see in the “OLEDB Source Editor”
- You can see all tables exist in the Power BI Desktop model by clicking on “Name of the table or the view” dropdown list
- As you see there is a bunch of “LocalDate Table” in the model that you cannot see in Power BI Desktop. They are hidden Date tables created by Power BI Desktop automatically to support time intelligence. We are NOT going to select a table from the list as we’ll get an error message at the end.
- Set “Data access mode” to “SQL Command” then write the following simple DAX code in the “SQL command text” box. I know, it says SQL command, but, we put DAX query. Believe me, it works
- Click “Columns” to see the table’s columns then click OK
- Put a “SQL Server Destination” on the data flow
- Connect the “OLE DB Source” to “SQL Server Destination”
- Double click “SQL Server Destination”
- Click “New” to create a new connection manager to our SQL database
- Click “New” again
- The provider should be “Native OLE DB\SQL Server Native Client”
- Enter the “Server name” that you desire to export data to
- Select or enter the database name
- Test connection
- You then need to click OK several times to get back to “SQL Destination Editor”
- Click “New” to create a new table to land our data
- Looking at generated T-SQL you’ll quickly understand that the create table statement won’t work. Copy the code and paste into a text editor OR in SSMS and tidy it up. You may also want to use a meaningful name for the new table.
- Copy/paste the code back to the “Create Table” window then click OK
- Click “Mappings” then map all “Input Columns” to “Destination Columns” then click OK
- That’s it. You just need to run the package now
This method works well even when exporting a larger number of rows.
If you have any other ideas, feedback, etc. I’d love to know about it. So please leave your thoughts in the comment section below.