Machine Learning Community Question

How can I use Power BI and Python together to perform advanced clustering on customer data?

Posted on 2025-11-05 • In Machine Learning • By Robert Wilson

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?

3 Answers 13,817 Views

All answers to this question 3 Answers

Susan Moore Answered on 2025-12-12
Answered

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.

Mark Thompson Answered on 2025-12-15
Answered

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?

Robert Wilson Commented on 2025-12-18
Commented

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.

Barbara Clark Answered on 2025-12-21
Answered

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.

Susan Moore Commented on 2025-12-24
Commented

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.

Write a Comment

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

Join the LearnersEra Community

Ask questions, share knowledge and learn from professionals across technology, project management and certification topics.

Change Country and Nearest City

Select your country and nearest city to see courses available in your region.