How to Run Python Scripts in Power BI Desktop

Introduction

How to Run Python Scripts in Power BI Desktop

Power BI is a powerful data visualization tool, but sometimes, you need advanced analytics, automation, or custom data transformation. This is where Python scripting in Power BI comes in!

With Python, you can:
✅ Perform complex data transformations
✅ Fetch external data (e.g., stock market, web scraping, APIs)
✅ Apply machine learning models
✅ Generate advanced statistical visualizations

In this tutorial, we’ll go through the step-by-step process of installing Python, configuring it in Power BI, and running Python scripts inside Power BI Desktop.

Step 1: Install Python on Your Computer

Power BI requires Python to be installed on your system before you can run Python scripts.

Install Python

  1. Download Python from the official website:
    👉 https://www.python.org/downloads/
  2. Run the installer and select:
    ✅ “Add Python to PATH” before clicking Install.
  3. Wait for the installation to complete.
  4. Restart your computer after installation.

Step 2: Install Required Python Libraries

For Power BI to process data, you need the necessary Python libraries.

Install Required Libraries

  1. Open Command Prompt (CMD).
  2. Run the following command to install commonly used libraries:
pip install pandas numpy matplotlib seaborn

We can install required library in a similar way; pandas is required for Power BI!

Step 3: Configure Python in Power BI

  1. Open Power BI Desktop.
  2. Go to File → Options & settings → Options.
  3. Scroll down to Python scripting (under “Global” settings).
  4. Click “Detect” or manually select your Python installation path.
    • If unsure, check Python’s path by running:
where python

5. Click OK and restart Power BI.

python scripts in power bi
File → Options & settings → Options.
run python script in power bi
python scripts in power bi
Select Python scripting

Step 4: Run a Python Script in Power BI

1. Open Power BI Desktop.

2. Click Home → Transform Data.

3. In Power Query Editor, click Run Python Script.

python scripts in power bi

4. Enter this basic Python script to test:

import pandas as pd

# Create a simple DataFrame
df = pd.DataFrame({'Numbers': [1, 2, 3, 4, 5]})
df
python scripts in power bi

5. Click OK, then Load Data to see the results.

python scripts in power bi

Extract Real Data Using Python

Now, let’s fetch Yahoo Finance stock data and load it into Power BI.

import yfinance as yf
import pandas as pd

# Define stock symbol and date range
stock_symbol = "AAPL"
start_date = "2024-01-01"
end_date = "2025-01-01"

# Fetch data from Yahoo Finance
df = yf.download(stock_symbol, start=start_date, end=end_date)

# Reset index and select columns
df.reset_index(inplace=True)
df = df[['Date', 'Open', 'High', 'Low', 'Close', 'Volume']]

df
python scripts in power bi

✅ Click OKLoad DataYour stock data will now appear in Power BI! 🎉

python scripts in power bi
python scripts in power bi
power bi

Common Errors & Fixes

Error: “ModuleNotFoundError: No module named ‘pandas'”

🔹 Fix: Install the missing library using CMD

pip install pandas

Error: “Python script error: ADO.NET”

🔹 Fix: Ensure Power BI is using the correct Python path in File → Options → Python scripting.

Final Notes

✔ Python is required for Power BI scripting
✔ Install necessary libraries using pip install
✔ Set the correct Python path in Power BI settings
✔ Restart Power BI after making changes

Now, you can integrate Python + Power BI for advanced analytics! 🚀

Power BI Basic Course

Please follow and like us:
error
fb-share-icon

Leave a Comment

Your email address will not be published. Required fields are marked *