How to Use Python Script In Power BI

by | Power BI

Power BI is hands down one of the best platforms you can use to analyze, transform and visualize data. Its power is compounded even more when you learn how to use Python script in Power BI.

How does the use of Python script and Power BI together benefit data professionals?

Python is one of the most commonly used programming languages in the world. The sheer number of programmers using the language alone is testament to how convenient it is to use especially when it comes to data analysis.

So imagine the impact if Python is integrated with Power BI. Users can start creating data analysis scripts and level up visualization techniques beyond what Power BI can currently do.

What You Need To Get Started

The first thing that you need to do is download the right Python installer and have it installed on your computer. From there, you can download and install the required Python packages next.

There are two Python packages that we need to install before we can start using Python scripts in Power BI — Pandas and Matplotlib.

Pandas is a library that can be used for data manipulation and analysis. It’s especially useful if you’re working with time series and numerical tables.

On the other hand, Matplotlib serves as a plotting library where users can create animated, interactive and static visualizations using Python.

To install these two packages, you need to use the pip command line tool. This usually comes with your Python installer download.

Next, you need to enable Python scripting on your Power BI desktop. To do this, just go to File, then click on Options and Settings. Under Options, you’ll find Python Scripting.

how to use python script in power bi

Always make sure that you set the right installation path for Python. This is one area that causes issues for other people who are also figuring out how to use Python script in Power BI. Once the right path has been set, you can start running Python scripts on your Power BI desktop.

How to Use Python Script in Power BI

Now that you’re ready to run Python script in Power BI, there are a few things that you need to remember first.

Although using Python in Power BI has a number of benefits, there are also some limitations:

  • Make sure that the data you’re importing into Power BI comes in a data frame because only Panda data frames are recognized.
  • Always check how much time it takes to run your script because Python scripts will time out once they go beyond 30 minutes.
  • Any interactive calls used within the Python script will interrupt its execution.
  • Always use a full path when setting up the directory in your Python script, and not just a relative path.
  • There is no support yet for nested tables when using Python script in Power BI.

Prepare your script in your local Python development environment first before running it through Power BI. Make sure it runs smoothly so that you won’t have to deal with issues once you move to the Power BI desktop.

Once you have all of these things covered, you can start running your script.

Under the Home ribbon, click on Get data, then choose Other. You’ll find Python Script under Other. Click Connect.

how to use python script in power bi

A Python script dialog box will appear. Copy your own script into the box, then click OK.

You’ll know that your script ran successfully if the Navigator box opens. From here, you can select which data to load.

how to use python script in power bi

Note that you might encounter issues if you have extra steps in the distribution process. It’s always best to get the official distribution from the Python website itself to avoid any issues.

***** Related Links *****
Power BI With Python Scripting To Create Date Tables
Python Scripting In Power BI Data Reports
Python I for Power BI Users – New Course in the Enterprise DNA Education Platform

Conclusion

Using Python in Power BI will improve Microssoft’s analytical tool and enhance the way Power BI works. You can perform analysis using frameworks like Pandas and run external Python scripts from Power BI, and a lot more. With this tutorial, I hope you can learn how to use both tools in your report development.

All the best,

Sam

Related Posts