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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

v-lionel-msft

Selectively Importing Python Tables and Auto Exporting with Python

Python and Power BI are directly passed by DataFrame. Since Python itself does not support dataframe, python calls the pandas library automatically. M transfers its data of table type to python, and python will automatically convert table to dataframe; Python's processing results are output in the form of dataframe, and m will automatically convert dataframe to table format.

 

Importing Multiple Tables and Filter with Parameter
Instead of using the “Python Script” connector to import data, I need to use Python.Execute() function in power query to retrieve multiple query tables. Please refer to the steps:

 

1. In power query editor, create a blank query.


2. Create Python.Execute() function to run Python script in the bracket.

v-lionel-msft_0-1618989508516.png


3. I create 3 tables with duplicate values just to demonstrate on how to filtered tables.

v-lionel-msft_1-1618989516255.pngv-lionel-msft_2-1618989519294.png

 

4. Python.Execute() allows you to create an aggregated query include multiple tables. With this query, we should be able to create a parameter to filter the table we want to display in the Desktop visual interface.

 

5. It is recommended to create an index column since the table name could be complex in real case, we just need to add an index number before the table name (e.g. 1. Product table, 2.Income table, etc.). Then we can right click index column, add as new query to create a query parameter and use the parameter to filter the index column. Finally, we can just expand the table before Close and Apply.

v-lionel-msft_3-1618989550254.png

v-lionel-msft_4-1618989553185.png


Exporting Data with Schedules

Here is a very common scenario, if we would like to export data every time the dataset refreshes we could use Python script to achieve this. By executing the following python code, we would create a variable (data) that duplicates the original table (dataset), then use the data.to_csv function to export the data to a specified location.

v-lionel-msft_5-1618989571887.png

 

Once this steps is done, we can create the report and publish it to the service as usual to service like we usually do. However, it is important to know that Python/R scripts are only available for Personal mode Gateway.

v-lionel-msft_6-1618989577027.png


After configuring the personal gateway properly, we can then set up schedule refreshes. With the above configuration, the script allows you export and update the csv file every time the dataset refreshes.

v-lionel-msft_7-1618989597569.pngv-lionel-msft_8-1618989603756.png

Comments

Hi All,

 

Sample Files: Pbix Files

Blog Author: @V-pazhen-msft

 

 

Best Regards,
Lionel Chen