There is always a discussion on how to store back the data from Power BI to local computer or SQL Server Databases, in this short blog, I will show how to do it by writing R scripts inside Power Query.
First how to Store Files to PC
To store data back to a file location in your PC, we need to write one line of code in R.
In my scenario, first, I get some data from https://www.hbo.com/game-of-thrones/cast-and-crew
then I applied some changes to the data using Power query as you have seen in below picture, I applied more than 20 steps to fetch the images
Now I want to store back it into my PC, to do that, you need to navigate to Power Query Editor, then click on Transform, then Run R Scripts
Then in the new R script Editor write the below code, the dataset variable holds all queries,
in R scripts we able to use a function name write.table, you need to pass the dataset and file location, then the separation of CSV file that is in my scenario cama,
# 'dataset' holds the input data for this script write.table(dataset, file="e://Fundataset//game-of-thrones//GOTImageCharacter.csv",sep=",",row.names=F)
Then push the OK bottom, then just check the location of the e://Fundataset//game-of-thrones//GOTImageCharacter.csv
you should see the file there, it was not a big dataset, I applied the same code for 60000 rows of data from AdventureWorks Fact internet sales table, it also took 10 seconds.
Second how to Store Files to SQL Server
In this example, I am going to store the data in SQL Server, the same file into SQL Server 2017. to do that you need to access to the specific package: RODBC
You need to install this package in R Studio.
In below codes write the codes,
access the RODBC package, then you need to use a function odbcDriverConnect, you need to specify the driver, server, database name and so forth.
then you need to use sqlSave, you need to use SQL connection, dataset, and name of the table.
library(RODBC) DBHANDLE<-odbcDriverConnect('driver={SQL Server};server=.\\ML2017;database=Ai;trusted_connection=true') sqlSave(DBHANDLE, data.frame(dataset), tablename = "GOT",rownames=FALSE, append = FALSE)
when you run it, you may receive below error!
R will run twice, so it will store the file twice in the computer, you will get an error that file already exists.
To solve that, we need to put a try keyword in front of the Power Query as you can see in the below code.
just check the SQL Server table
to check if there is a possibility to check the dataset if exist not write the code there
TBLExist="GOTImage" %in% sqlTables(DBHANDLE)$TABLE_NAME if (!TBLExist)
this will help, to check the existence of a table
# 'dataset' holds the input data for this script #write.table(dataset, file="e://Fundataset//game-of-thrones//GOTImageCharacter.csv",sep=",",row.names=F) library(RODBC) outputframe=data.frame(dataset) DBHANDLE<-odbcDriverConnect('driver={SQL Server};server=.\\ML2017;database=Ai;trusted_connection=true') TBLExist="GOTImage" %in% sqlTables(DBHANDLE)$TABLE_NAME if (!TBLExist) sqlSave(DBHANDLE, data.frame(dataset), tablename = "GOTImage",rownames=FALSE, append = FALSE)
by putting this code you did not get any error regarding the existing database.
Thanks! Writing to a database is an amazing possibility. Just want to mention that Imke Feldmann has a Python script that exports queries to csv files. https://www.thebiccountant.com/2018/12/29/export-data-from-power-bi-to-csv-using-python/
Thanks to send the link to her blog, yes that is another way,
When you talk about Power Query, like in this article, can you specify if it works in Excel Power Query?
Good questoin, I think it does not work as we could not run R/ Python in Excel Power Query Yet (at least I am not sure about it yet it availabe or not,) if it is availabe then should work.
Hi Leila,
Great post and something I’ve been waiting for a long time.
Could you add details on achieve the same using Python please?
I read Imke Feldmann’s post but was wondering whether it could be simplified similar to R’s write.table function? I think it will make your post more useful if you could add how to chieve this using Python (and the storing to MS SQL) .
Regards,
Anand
I checked her code this is the code for Python she used
dataset.to_csv(path, mode = ‘w’, index = ” index”, header = “header”, quoting = ” quoting “, chunksize = “chunksize”, decimal= “decimal_as_point”)
just parametise the index and other things that you can specify in the code your self, but she make it more funaciotnal to be use any time by any dataset which is so cool
Great post Leila! Many thanks for sharing!
I was trying to apply that on sample dataset and got an error:
“Working directory does not exists”
Have you experienced such behavior?
I have R installed in Program Files folder on Windows drive and configured like that in Power BI Option. However, this Power Query error points to the location in Users folder. Not sure why.
Thanks so much, could you please send the code so I can check, check file path “//” chec filename with csv …
Is it possible to run the code as a Dataset in the Power BI service with a scheduled refresh?
Will check
Hi,
Is there a possibility to do it via Power BI Service, too, or is this just for PBI desktop?
Thanks and best regards
You are welcome, there is a way to do it by Power Shell scripts, Reza should know about it will ask him
Can we make this R script to run at a scheduled point of time everyday with out manual effort?
Yes,
R scripts can run through a scheduled dataset refresh through a gateway. However, if you don’t have a gateway setup, that might take a bit of time to configure.
Cheers
Reza