An Introduction to Python in Power BI
Table of Contents
Introduction
Power BI has always been a go-to tool for every data enthusiast, whereas Python has established itself as the language of choice for data science. These felt like two separate worlds: Power BI for reporting, Python for deeper analysis, with data often bouncing between the two in the form of CSV exports and copy-pasted results.
However, Power BI now supports Python directly, enabling you to bring Python’s data manipulation, statistics, and visualization capabilities right into your reports and data model. This post is a gentle introduction to what that integration looks like, why you might want to use it, and how the pieces fit together.
Want to learn more about Python Scripting? Check out our blog: Getting Started with Python Scripting in Power BI
Where Python Fits in Power BI
Python can be used in two main places within Power BI:
- Python Scripts in Power Query (Data Transformation): You can use Python as a data source or transformation step during the data-loading process. This is useful for cleaning, reshaping, or enriching data before it lands in your data model.
- Python Visuals (Data Visualization): You can create a Python visual on your report canvas, write a script using libraries like Matplotlib or Seaborn, and Power BI will render the output as a static image within your report.
Getting Your Environment Ready
Before diving in, it helps to understand what’s happening under the hood: Power BI doesn’t run Python itself. Instead, it calls a Python installation on your machine, runs your script, and brings the results back in. That means a bit of setup is needed before your first script will work.
1. Open Power BI desktop.
2. Go to Options and settings and select Options
3. Select Python Scripting and click on the hyperlink to install python.
This will open a Microsoft Learn page Create Power BI visuals with Python.
4. Here under prerequisites click on the hyperlink for downloading python.
5. On Python Website, download exe file compatible with your system.
You may enable the option ‘Add Python 3.7 to PATH’ to add this into the environment, or you may add it manually. After installing Python, you need to restart your Power BI Desktop app.
6. Power BI will now recognize your Python environment and allow you to run scripts.
Python Scripts in Power Query
To transform data using Python during the load process:
7. Go to Home > Get Data > More.
8. Search for and select Python script.
9. Write your transformation code in the script editor. For example:
import pandas as pd df = pd.read_csv(“C:/data/sales_data.csv”) df[‘Total’] = df[‘Quantity’] * df[‘UnitPrice’] df = df.dropna()
10. Click OK. Power BI will run the script and let you select which resulting dataframe to load into your model.
You can also apply a Python script to an existing query by selecting Transform > Run Python script in the Power Query Editor. Any dataframe available in that step can be manipulated further with Python before continuing your transformations.
Creating Python Visuals
Python visuals let you bring custom charts into your report:
11. In the Visualizations pane, select the Python visual
12. A placeholder Python visual image appears on the report canvas.
13. Drag the fields you want to use into the Values.
14. A Python script editor will appear at the bottom of the screen, with a dataset dataframe automatically populated from your selected fields.
15. Write your plotting code.When the script is complete, select the Run icon from the Python script editor title bar to run the script and generate the visual.
Note: Keep in mind that Python visuals are rendered as static images, so they won’t support the same interactivity (like tooltips or cross-filtering) as native Power BI visuals.
Best Practices and Limitations
- Performance: Python scripts can slow down refresh times, especially with large datasets. Use Python selectively for tasks that Power Query or DAX genuinely can’t handle well.
- Row limits: Python visuals currently support a maximum of 150,000 rows of input data.
- Static output: Python visuals aren’t interactive, they’re images, so they can’t be cross filtered by other visuals on the page.
- Security and privacy: Since scripts run locally in Power BI Desktop, be mindful of data privacy settings when combining sources, and be cautious about running scripts from untrusted sources.
- Publishing:To refresh Python-based reports in the Power BI Service, you’ll need to configure a personal gateway with Python installed, since cloud-based refreshes don’t have a native Python runtime.
Common Use Cases
- Outlier detection and cleaning: Use statistical methods to flag and remove anomalies before analysis.
- Forecasting: Apply time series models (like ARIMA or Prophet) to generate forecasts and visualize them next to historical trends.
- Clustering and segmentation: Use scikit-learn to segment customers or products, then display the groupings visually.
- Sentiment analysis: Process text data with NLP libraries and surface sentiment scores in your dashboards.
- Custom statistical plots: Build correlation heatmaps, violin plots, or other visuals not natively available in Power BI.
Conclusion
Python in Power BI isn’t meant to replace Power Query or DAX, it’s meant to fill in the gaps where those tools reach their limits. Think of it as another tool in the toolbox: reach for it when you need statistical rigor, machine learning, or a visualization that Power BI doesn’t offer natively.
If you’re just getting started, pick one small task, clean up a messy column, or build a single custom chart, and get a feel for the workflow. From there, it becomes much easier to see where Python can add real value to your existing reports.
FAQs
Yes, at least the basics. Power BI doesn’t teach you Python it just gives you a place to run scripts you write yourself. If you’re comfortable with Pandas and basic scripting, you’re ready to start.
You need to install Python separately on your machine. Power BI Desktop calls that local installation to run your scripts; it doesn’t come bundled with its own Python engine.
Yes. Python is typically used for data preparation (in Power Query) or for a specific visual, while DAX continues to handle measures, calculated columns, and the rest of your data model as usual.
Further Learning and Resources
To learn more about Power BI, check out our courses here:
- Power BI Essentials
- Power BI Report Design
- Power BI Advanced
- Power BI DAX Essentials
- Power BI Service Course
- Chat GPT Essentials
Want to learn how Microsoft 365 Copilot transforms everyday work with AI? Check out our Course:
For end-to-end solutions customized to your problems, Get in touch with our Consultants:
Want to learn more about Power BI and how to make reports more efficiently?
Power BI training in Australia: We offer comprehensive training sessions across various cities, including :
Ready to enhance your Power BI skills? Sign up for our training courses and explore our wide range of resources designed to elevate your data analytics capabilities.
Want end-to-end solutions for your Power BI Reports? Get in touch with our consultants at Power BI Consulting & Training Services in Australia
Make an enquiry about our Power BI courses and find the proper training for you.








Really helpful introduction to Python in Power BI! I like how you explained its role in data analysis. For someone who already knows Power BI but is new to Python, how useful would you say it is to learn?