Power Query: Python Set
16 October 2019
Welcome to our Power Query blog. Today, I look at how to set up and use Python in Power BI.
Last time, I installed and set up Python ready for Power BI to use. Naturally, I’d also like to be able to use Python in ‘Get & Transform’ from Excel, but for now this is not yet available, so I’ll look at how to use Python in Power BI.
In the ‘Options’ tab, I found the Python sub screen:
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2019/power-query/150/image1.png/e774d10cbbb9450fc45efbe51abdf434.jpg)
I set up the IDE to point to the location of my Visual Studio Code.
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2019/power-query/150/image2.png/f32e5a15e2cf9c3e4d2d058458ce054d.jpg)
I am ready to use Python as my source. Back on the ‘Home’ tab, I look at the options available from the ‘Get Data’ tab.
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2019/power-query/150/image3.png/f1140ff857fc3b6f5f97a6a24f4a6fc7.jpg)
I can limit the amount of data options available by selecting ‘Other’.
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2019/power-query/150/image4.png/72aa864d2854c6fefb1083fba0ab5792.jpg)
I have the option ‘Python Script’, and hovering over this gives me more information ‘Run a Python script on a local Python installation to import data frames’. I choose this option.
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2019/power-query/150/image5.png/36776d1da4d05b45bb5a5d09375f407c.jpg)
I can enter a Python script here – I will use the same standard Microsoft example that I used last time.
import pandas as pd
data = [['Alex',10],['Bob',12],['Clarke',13]]
df = pd.DataFrame(data,columns=['Name','Age'],dtype=float)
print (df)
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2019/power-query/150/image6.png/23912d3b1671861e02bebcd5183f1607.jpg)
I click ‘OK’ to run the script. There is a slight pause whilst the connections are secured and the script is run, and then the ‘Navigator Pane’ appears.
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2019/power-query/150/image7.png/6f49c288a0d88a66b427eaf4ece923d6.jpg)
I can select the table icon to see what data has been imported.
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2019/power-query/150/image8.png/b9ee28d90e6b5bc92ea4aeafdad51628.jpg)
The data appears as expected. I choose to ‘Transform Data’.
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2019/power-query/150/image9.png/0485ccbc83bdeec1d741bad442a1ea5f.jpg)
My data is in the Power Query editor ready to be transformed. Once I am happy, I use ‘Close & Apply’ to make my data available.
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2019/power-query/150/image10.png/daf8c4f0259ce428269c0d3d4badd32b.jpg)
I can now go back to my Power BI Desktop report canvas and select this data.
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2019/power-query/150/image11.png/22c6daeb82d7d69ac88f878227e04b28.jpg)
Next time, I will look at how to use Python to filter some of the data provided by my imaginary salespeople.
Come back next time for more ways to use Power Query!