Introduction

In this article, we are going to explore how to execute python scripts from Power BI. There are multiple in-built visualizations available in Power BI but sometimes you want to use advanced visualization to work on the requirement. We will explore this.

Python Script in Power BI Desktop

We can execute the python scripts on the Power BI desktop. We will now explore step by step process to execute a python script.

Step 1 - Install Python

In order to run the python script on the desktop, we have to first install python on the local machine. In order to execute the python script from Power BI, we need the below python packages and they should be installed.

Python Packges

We can use the pip command to install python packages.

Step 2 - Enable Python Scripting in Power BI Desktop

From the power BI desktop, Go to File -> Options & Settings -> Options -> Python Scripting

Execute Python Script From Power BI

Once, we specify and enable python in power bi desktop, we can execute the python script from Power BI.

Step 3 - Prepare a python script to execute it from Power BI

# importing pandas package
import pandas as pd
  
# making data frame from csv file 
data = pd.read_csv("emp.csv")
  
# generating one row 
row1 = data.sample(n = 1)
  

Now the python script is ready. But before we execute the python script, there are few limitations to execute python scripts in Power BI.Limitations for python script in Power BI is as below :

Step 4 - Execute Python Script from Power BI

Now, the python script is ready so we can use it to execute it from Power BI.

Go to the Home page of the Power BI desktop, select Get Data -> Other -> Python Script -> Connect.

Execute Python Script From Power BI

The installed python version which is mentioned in the Power BI desktop will be used as a python engine to execute the python script.

Execute Python Script from Power BI

After you select OK and python script will execute in the Power BI Desktop.

Other ways to Use Python in Power BI

Using Power BI Query Editor

Using External IDEs

Conclusion

So, in this article, we explored how to execute the python script from Power BI. We also explored the multiple ways to configure and execute python scripts from Power BI.