In this tiny tutorial, I’ll be demonstrating how to integrate Python script to get data for transformation and analyses on Power BI. I’ve used an example Python script to scrape web data in this walk through which must provide a well-rounded idea on how Python can be leveraged within PowerBI.

 

– Installing Python and setting it up on Power BI:
    • Python installation file can be downloaded from – Download Python | Python.org
    • Upon installation, to check if Power BI Desktop has detected the installation go to:

File > Options & Settings > Options > Python scripting.

    • You should be able to see a path to the installation folder under ‘Detected Python home directories. Leave the Detected Python IDEs as seen below.

 

Folder path: C:\Users\<username>\AppData\Local\Programs\Python\Python311

NOTE: I could not see the Python directory straight away. I uninstalled and reinstalled the latest Power BI version and that seemed to resolve this issue. I read about this on one of the blogs online.

 

– Writing Python script in Power BI:
    • Click on Get data and type in Python. Click on ‘Python script’ and connect as seen below.

    • This open a Dialogue box with area to type in your script as seen below.
    • I’ve typed in the below Python code to scrape data from Gumtree.com for used cars for sale in Victoria then click ‘OK’. (First 10 pages)

code can be found here: Python-Web-Scrape/python_web_scraping at main · shashankalashnikov/Python-Web-Scrape (github.com)

    • If all goes well you must be able to see the below popup from where data can be transformed for analysis as usual.

    • The last line of code is where a table is assigned to a variable DF. This shows up as the table name.

 

– Caveats and Errors:
    • The below error was the one I encountered. This means that the required libraries aren’t installed.

 

    • This was resolved by installing the libraries on command prompt with the steps below.

Windows key > cmd

Open Command Prompt >  for example, type in ” py -m pip install pandas” without quotes.

 

    • Follow the same steps for all the required libraries.

 

More on resolving issues and errors can be found in below links:

This concludes the tutorial for using Python in Power BI with an example of web scraping using Python.

Shashanka Rao
Author: Shashanka Rao