How to Install Python in Power BI
Pairing Python's data wrangling power with Power BI's interactive reporting capabilities is a true game-changer for any analyst. This combination lets you perform complex data transformations, run statistical models, and create custom visualizations that go far beyond Power BI’s built-in options. This guide will walk you through the entire process, from installing Python on your machine to configuring Power BI and running your very first script.
Why Use Python in Power BI?
Before we get into the setup, let's quickly cover why this is worth your time. Integrating Python isn't just for data scientists, it gives any Power BI user a significant advantage. The three main benefits are:
- Advanced Data Cleaning and Shaping: While Power Query is incredibly powerful, Python libraries like Pandas allow for more complex and nuanced data manipulation, making it easier to handle messy datasets, perform complex calculations, and prepare your data with surgical precision.
- Custom Visualizations: Power BI has great visuals, but what if you need something highly specific, like a specialized scientific chart or a unique statistical plot? With libraries like Matplotlib and Seaborn, you can generate nearly any type of static visualization you can imagine, directly within your report.
- Statistical Analysis and Machine Learning: This is where the integration truly shines. You can run predictive models (like regressions or classifications) using libraries like scikit-learn and display the results in Power BI, turning your descriptive reports into prescriptive analytics dashboards.
Step 1: Install Python on Your Computer
The first step happens outside of Power BI. You need to install Python locally on your machine. If you already have Python installed, you can skim this section, but it’s a good idea to ensure it's configured correctly.
Downloading Python
It's best to install Python directly from the source.
- Navigate to the official Python website at python.org/downloads.
- Click the button to download the latest version for Windows. The site usually detects your operating system automatically.
- Once the installer file (.exe) has been downloaded, run it.
The Most Important Step During Installation
When the installation wizard appears, you’ll see two options at the bottom of the first screen: "Install launcher for all users (recommended)" and "Add python.exe to PATH."
This is the most important part of the process: make sure you check the box that says "Add python.exe to PATH."
Why is this so important? "PATH" is an environment variable on your computer that tells the command line (and other applications, like Power BI) where to find executable files. By adding Python to your PATH, you’re making it possible for Power BI to find and run your Python installation without any extra configuration headaches later.
After ticking that box, click "Install Now" and let the wizard complete the installation.
Free PDF · the crash course
AI Agents for Marketing Crash Course
Learn how to deploy AI marketing agents across your go-to-market — the best tools, prompts, and workflows to turn your data into autonomous execution without writing code.
Verifying the Installation
Once the installation is complete, it's a good habit to verify that everything is working. You can do this through the Command Prompt:
- Press the Windows key, type
cmd, and press Enter to open the Command Prompt. - Type the following command and press Enter:
python --versionIf the installation was successful and you added Python to your PATH, the Command Prompt will display the version of Python you just installed (e.g., "Python 3.12.3"). If you get an error that the command is not recognized, it likely means the "Add to PATH" step was missed. In that case, the easiest solution is to uninstall and reinstall Python, making sure to check the box this time.
Step 2: Install Essential Python Libraries
A fresh Python installation is a blank slate. To do anything useful for data analysis in Power BI, you need to install some key third-party libraries. The two absolutely essential libraries for getting started are Pandas and Matplotlib.
- Pandas: This is the backbone of data analysis in Python. It provides the DataFrame structure - essentially a table with rows and columns, similar to a spreadsheet - that Power BI will interact with.
- Matplotlib: This is the foundational library for creating plots and charts in Python. Power BI uses it under the hood to render Python visuals.
You’ll use pip, Python’s built-in package manager, to install them. Open the Command Prompt again and run the following commands one by one, pressing Enter after each:
pip install pandas
pip install matplotlibYou can also install them both in one line if you prefer:
pip install pandas matplotlibpip will download and install the packages for you. You are now ready to connect Python with Power BI.
Step 3: Enable Python Scripting in Power BI
With Python and the necessary libraries installed, the final step is to tell Power BI where to find them. This is a one-time setup within Power BI Desktop's options.
- Open Power BI Desktop.
- Go to File > Options and settings > Options.
- In the Options window, scroll down the Global options on the left and select Python scripting.
- Power BI is usually good at auto-detecting your Python installation, especially if you added it to PATH. You should see the Python home directory already populated under "Detected Python home directories." If so, ensure that option is selected and click OK.
- If the Detected path is empty or incorrect, you’ll have to set it manually. Under "Python home directory," select "Other" and then browse to the folder where Python was installed. It's typically in a location like:
C:\Users\{Your_Username}\AppData\Local\Programs\Python\{Python_Version}.
Once you’ve done this, click "OK." You are now all set to start using Python in your reports!
How to Use Python in Power BI: Two examples
There are two primary ways to run Python scripts in Power BI: as a data source and as a method for creating visualizations. Let's look at a simple example of each.
Example 1: Using a Python Script as a Data Source
You can create a Pandas DataFrame directly in a script and load it into Power BI as a new table. This is great for procedurally generating data or pulling from a source Power BI doesn't have a native connector for.
- In Power BI's Home ribbon, click on Get data.
- In the search box, type "Python script" and select it from the list.
- A new window will open for you to write your script. Copy and paste the following code:
import pandas as pd
# Create a simple dataset
data = {'Category': ['A', 'B', 'C', 'D'],
'Value': [10, 25, 15, 30]}
# Convert the dataset into a pandas DataFrame
df = pd.DataFrame(data)
# Power BI looks for DataFrame objects at the end of the script,
# so we simply leave df here
dfClick OK. Power BI will execute the script using your Python engine. A Navigator window will then appear showing the DataFrame, df, that was generated. Check the box next to it and click "Load" to import it into your data model, just like any other data source.
Free PDF · the crash course
AI Agents for Marketing Crash Course
Learn how to deploy AI marketing agents across your go-to-market — the best tools, prompts, and workflows to turn your data into autonomous execution without writing code.
Example 2: Creating a Python Visual
This is where things get fun. Let’s say you have some data loaded and want to create a Matplotlib bar chart.
First, ensure you have some data loaded. You can use the data from the previous example or any other table with a categorical column and a numeric column.
- In the Visualizations pane, click the Python script visual icon (it looks like
Py). - Power BI will create an empty visual placeholder and open a Python script editor section at the bottom. You will see a comment that says: # Pandas DataFrame 'dataset' holds your data.
- Select the data fields you want to work with. For our example, drag the 'Category' field and the 'Value' field into the "Values" well of the visual.
- This action automatically creates a Pandas DataFrame called
datasetin the script editor's memory containing those two columns. - Now, in the script editor, paste the following code:
import matplotlib.pyplot as plt
# The 'dataset' DataFrame is created for you by Power BI
# containing the columns you dropped into the 'Values' well.
# Create a bar chart using the dataset
plt.bar(dataset['Category'], dataset['Value'])
# Display the plot
plt.show()Click the "Run" icon (a play button) at the top of the script editor. After a brief moment, your Matplotlib bar chart will appear in the visual placeholder on the report canvas!
Final Thoughts
You've successfully installed Python, configured Power BI to use it, and executed scripts for both data sourcing and visualization. This setup unlocks a world of powerful data manipulation and advanced visual capabilities, allowing you to build richer and more insightful reports.
While this integration is incredibly effective for custom projects, we know that sometimes you need answers without writing code or configuring environments. At Graphed, we’ve built our platform to eliminate this setup process entirely. You can connect sources like Google Analytics, Shopify, and Salesforce in a few clicks and build entire dashboards just by describing what you want to see - no scripts, no installations, just plain English and instant answers.
Related Articles
How to Create Facebook Ads API for Claude Code
Create a Meta Marketing API access token that never expires, using a system user, so Claude Code can pull Facebook ad reports and manage campaigns.
ClickHouse vs Postgres: When to Switch to Columnar
Comparing ClickHouse vs PostgreSQL means matching storage architecture to workload. Row storage wins for transactions. Columnar wins for scan-heavy analytics.
Google Ads for Orthodontists: Get More Booked Consults
Most orthodontic Google Ads campaigns waste money on low-intent searches. The problem is structure, not the platform.