How to Connect Power BI to Python

Cody Schneider10 min read

Combining the visual storytelling power of Power BI with the advanced analytical strength of Python opens up a world of possibilities for your data. This integration allows you to run complex statistical analyses, implement machine learning models, and create highly customized visuals that go far beyond standard dashboard capabilities. This guide will walk you through, step by step, how to connect Power BI to Python for importing, transforming, and visualizing your data.

Why Connect Power BI and Python?

You might be wondering, "Isn't Power BI powerful enough on its own?" For many scenarios, yes. But when you need to push the boundaries of standard business intelligence, Python is your best friend. Here’s what this pairing allows you to do:

  • Advanced Data Cleaning and Preparation: While Power Query is excellent, Python libraries like Pandas give you granular control for complex data manipulation, such as advanced text processing, imputation of missing values using statistical methods, or complex reshaping of data.
  • Sophisticated Statistical Analysis: Go beyond averages and sums. Use libraries like SciPy and StatsModels to perform regression analysis, hypothesis testing, or clustering directly on your data before you visualize it.
  • Machine Learning Integration: Build and apply predictive models. You can run a script that performs sentiment analysis on customer feedback or predicts future sales using a model you've trained with Scikit-learn, and then feed the results directly into Power BI.
  • Unlimited Custom Visualizations: If you can dream of a chart, you can likely build it with Python libraries like Matplotlib, Seaborn, or Plotly. This is perfect for creating bespoke visuals like heatmaps with customized annotations, detailed scatter plots with regression lines, or specialized scientific charts not available in Power BI’s marketplace.

Essentially, this connection lets you use Power BI for what it does best - creating interactive, shareable dashboards - while leaning on Python for the heavy-duty data science work that happens behind the scenes.

Prerequisites: Getting Your Environment Ready

Before you can bridge the gap between these two tools, you need to make sure your local machine is properly set up. Here are the essentials:

  • Power BI Desktop: You'll need the free Power BI Desktop application installed on your Windows computer. Python integration does not work in the cloud-based Power BI service, only on the desktop client.
  • Python Installation: A local installation of Python is required. If you're new to Python, we recommend using a distribution like Anaconda, as it simplifies the management of environments and packages for data science.
  • Essential Python Libraries: To perform most data tasks, you'll need a few key libraries. The two most critical ones for Power BI integration are:

You can install these libraries using pip, Python's package installer. Open your command prompt or terminal and run the following commands:

pip install pandas pip install matplotlib pip install seaborn

We've included Seaborn here as it's a popular library built on top of Matplotlib that makes creating beautiful statistical charts like heatmaps and regression plots incredibly easy.

Configuring Power BI to Use Python

Once Python and the necessary libraries are installed, you need to tell Power BI where to find them. This is a one-time setup that enables all Python-related features.

Follow these steps:

  1. Open Power BI Desktop.
  2. Navigate to File > Options and settings > Options.
  3. In the Options window that appears, find the Python scripting tab on the left-hand pane.
  4. Power BI will attempt to automatically detect your Python installation. If it finds one (or more), you can select it from the "Detected Python home directories" dropdown.
  5. If Power BI doesn’t find your installation, or if you want to use a specific virtual environment (which is a best practice!), you’ll need to set the path manually. Click “Other” and then browse to the folder where your Python executable is located. If you're using an Anaconda environment, this would be the root folder of that environment (e.g., C:\Users\YourUser\anaconda3\envs\my_pbi_env).
  6. Click OK to save your configuration.

Your Power BI Desktop is now fully configured to execute Python scripts.

How to Import Data Using a Python Script

The first way to use Python is as a data source. This is perfect for pulling data from APIs, performing initial cleaning on a local file, or even just generating a data table from scratch.

Running a Python Script as a Data Source

Let's walk through creating a simple dataset entirely within Python and loading it into Power BI.

  1. In the Home ribbon of Power BI, click Get Data.
  2. In the Get Data window, select Other > Python script and click Connect.
  3. A dialog box will appear with a Python script editor. Here, you can write any Python code you want. The key rule is that your final data must be stored in one or more Pandas DataFrames.
  4. Enter the following script to create a simple DataFrame:

import pandas as pd

Creating a sample dataset

data = {'ProductName': ['Widget A', 'Widget B', 'Gadget C', 'Widget A', 'Gadget C'], 'UnitsSold': [150, 200, 120, 130, 180], 'SaleDate': pd.to_datetime(['2024-01-15', '2024-01-16', '2024-02-10', '2024-02-11', '2024-03-05'])}

The variable below, 'sales_df', is what Power BI will see

sales_df = pd.DataFrame(data)

  1. After entering the script, click OK. Python will execute the code in the background.
  2. Power BI's Navigator window will then pop up, showing you all the DataFrames that were created by your script. In our case, you'll see one named sales_df.
  3. Check the box next to sales_df to preview the data. It looks good, so click Load.

And that's it! The data from your Python script is now loaded into your Power BI data model and is available in the Fields pane, just like any other data source.

How to Transform Data with Python in Power Query

Perhaps the most powerful application of Python is for data transformation within the Power Query Editor. This lets you apply complex, custom logic to an existing table.

Imagine you have product sales data, and you want to create a new column categorizing sales as 'High', 'Medium', or 'Low' based on quantile distribution - a task that's more straightforward in Python than with DAX or M.

  1. Begin by loading your data into Power BI, and then open the Power Query Editor by clicking Transform data.
  2. In the editor, select the query (the table) you want to modify.
  3. From the ribbon, go to the Transform tab and click Run Python script.
  4. The script editor will appear. Here's the important part: Power Query automatically passes the entire table of your selected query into the script as a pre-defined Pandas DataFrame called dataset. All you have to do is manipulate this dataset.
  5. Let's add a script to create our SalesCategory column based on the quantiles of UnitsSold:

'dataset' holds the input table from Power Query

import pandas as pd

Calculate the quantiles for the 'UnitsSold' column

low_threshold = dataset['UnitsSold'].quantile(0.33) high_threshold = dataset['UnitsSold'].quantile(0.66)

Create a function to categorize each sale

def categorize_sales(units): if units >= high_threshold: return 'High' elif units >= low_threshold: return 'Medium' else: return 'Low'

Apply the function to create the new column

dataset['SalesCategory'] = dataset['UnitsSold'].apply(categorize_sales)

  1. Click OK. Power Query will create a new table named dataset that contains the original columns plus your newly created SalesCategory column. You might need to click on "Table" in the output field to expand it and see the results.
  2. Click Close & Apply in the top-left corner to load the changes back into your Power BI report.

How to Create Custom Visuals with Python

When you have a specific visualization in mind that isn't available natively, Python visuals come to the rescue. Let's create a scatter plot with a regression line to analyze the relationship between marketing spend and sales - a task perfect for the Seaborn library.

  1. On the main report canvas in Power BI, find the Visualizations pane and click on the "Py" icon to add a Python visual to your report.
  2. If this is your first time, you may need to click an "Enable" button to allow scripted visuals to run.
  3. With the blank visual selected, drag the fields you want to analyze from the Fields pane into the Values well of the visual. For our example, we'll drag MarketingSpend and Sales.
  4. As you drag fields in, Power BI automatically creates a dataset Pandas DataFrame containing just those columns. You'll see a Python script editor appear at the bottom of the canvas. This is where you write the code to generate your plot.
  5. Enter the following code to create a regression plot using Seaborn:

import matplotlib.pyplot as plt import seaborn as sns

The 'dataset' DataFrame is created automatically by Power BI

with the fields you dragged into the 'Values' well.

sns.set_theme(style="whitegrid") # A nice theme for the plot

Create the regression plot

ax = sns.regplot(x="MarketingSpend", y="Sales", data=dataset, line_kws={"color": "red"})

ax.set_title('Sales vs. Marketing Spend')

This final command renders the plot in the visual

plt.show()

  1. To run the script and generate the visual, click the play icon in the bar at the top of the script editor.

Your beautiful, customized Seaborn regression plot will appear directly on your Power BI report canvas. While this visual itself is a static image, it is fully interactive with the rest of your report - it will update automatically when you apply filters or slicers.

Tips for a Smooth Workflow

To make the most of this integration, keep these best practices and limitations in mind:

Best Practices

  • Develop and Debug Externally: The Power BI script editor is basic. For anything more than a few lines of code, write and test your scripts in a dedicated Python IDE like VS Code or a Jupyter Notebook. Once it's working perfectly, copy and paste it into Power BI.
  • Watch Performance: Running Python scripts, especially during data refresh, adds processing time. Use Python for tasks that are uniquely suited for it. For simpler transformations like removing columns or basic filtering, the native Power Query M language is often much faster.
  • Manage Your Environments: Use a dedicated virtual environment for your Power BI projects. This keeps your dependencies organized and ensures that your Power BI instance is pointing to the correct set of libraries.

Key Limitations

  • Power BI Service and Gateways: To refresh a dataset that uses a Python script in the Power BI Service, you must configure a Personal Mode data gateway. The standard Enterprise gateway does not support Python script refreshes.
  • Privacy Levels: Your Python data sources must be set to Public in the data source settings for them to refresh correctly in the Power BI Service.
  • No In-Visual Interactivity: While Python visuals react to external filters in your report, you cannot interact with the plot itself (e.g., click on a data point to cross-filter other visuals). It is rendered as a static image.

Final Thoughts

Connecting Power BI with Python breathes new life into your reporting, enabling you to layer advanced data science and custom visuals onto your interactive dashboards. By using Python for heavy data shaping, complex modeling, or bespoke chart creation, you can deliver deeper, more meaningful insights that drive better business decisions.

We know that setting up environments and scripting solutions in Python, while powerful, isn't always the fastest path to an answer. Sometimes, you just need a straightforward way to pull all your data together and ask questions. We built Graphed to do exactly that. It's an AI data analyst that lets you connect sources like Google Analytics, Shopify, and Salesforce in a few clicks, and then build real-time dashboards simply by describing what you want in plain English, completely eliminating the need for code.

Related Articles

How to Connect Facebook to Google Data Studio: The Complete Guide for 2026

Connecting Facebook Ads to Google Data Studio (now called Looker Studio) has become essential for digital marketers who want to create comprehensive, visually appealing reports that go beyond the basic analytics provided by Facebook's native Ads Manager. If you're struggling with fragmented reporting across multiple platforms or spending too much time manually exporting data, this guide will show you exactly how to streamline your Facebook advertising analytics.

Appsflyer vs Mixpanel​: Complete 2026 Comparison Guide

The difference between AppsFlyer and Mixpanel isn't just about features—it's about understanding two fundamentally different approaches to data that can make or break your growth strategy. One tracks how users find you, the other reveals what they do once they arrive. Most companies need insights from both worlds, but knowing where to start can save you months of implementation headaches and thousands in wasted budget.