How can I use Power BI and Python together to perform advanced clustering on customer data?
I have a clean dataset of customer purchase history, and I want to run a K-means clustering algorithm to segment them. I know Power BI has some native clustering, but I want the flexibility of Python's Scikit-learn. How do I integrate the Python script so the clusters update when I filter the data by region or date, and are there any gateway limitations I should know about?
2025-11-05 in Machine Learning by Robert Wilson
| 13798 Views
All answers to this question.
You can use the "Python script" transformation in Power Query to run your clustering. The script will take the input 'dataset' and return a new data frame with the 'Cluster' ID assigned to each row. One big catch is the Personal Gateway requirement. For the Power BI Service to refresh a dataset containing Python scripts, you must have a Personal Gateway running on a machine that has the same Python environment and libraries (like pandas and scikit-learn) installed. Also, remember that Python scripts in Power BI have a limit on the number of rows they can process (usually 150,000) and execution time.
Answered 2025-12-12 by Susan Moore
If I run the script in Power Query, will the clustering re-calculate every time a user clicks a slicer in the actual report, or only during a full data refresh?
Answered 2025-12-15 by Mark Thompson
-
Good question, Mark. If you put the script in Power Query, it only runs during the data refresh. If you need it to be truly dynamic based on report slicers, you have to use a "Python Visual" instead. The visual reruns the script every time the filtered data changes. However, Python visuals are much slower to render than native visuals and have strict privacy settings. For customer segmentation, usually, a daily refresh via Power Query is enough to satisfy the business requirements without hurting the user experience.
Commented 2025-12-18 by Robert Wilson
Make sure you handle null values in your Python script before running the K-means, as scikit-learn will throw an error if it encounters missing data in your features.
Answered 2025-12-21 by Barbara Clark
-
Definitely! I always include a simple dataset.fillna() or a dropna at the start of my Power BI Python scripts to avoid those frustrating refresh failures.
Commented 2025-12-24 by Susan Moore
Write a Comment
Your email address will not be published. Required fields are marked (*)

