Using Python to Filter Data in Power Query

1 Comment

In our previous post, we installed and set up Python to use with Power BIĀ and used Python as a data source. Let’s look at how Python can be used to filter data inside Power Query.

Let’s filter records where the number of Employees is greater than 5000. We will use the query:

[sourcecode language=”Python”] # ‘dataset’ holds the input data for this script

import pandas as pd

dataset_filtered = dataset.query(‘Employees > 5000’)
[/sourcecode]

It should look like below. Click OK:

You may get this message regarding privacy levels. Per the Power BI documentation, “For the Python scripts to work properly in the Power BI service, all data sources need to be set toĀ public“. Click Save:

This will output below. Click on dataset_filtered:

Your dataset will now be filtered accordingly:

In the next post, we will look at using the Python visualization in Power BI.

Carl de Souza
Keep learning. Keep building.

Explore AI, agents & Microsoft technology.

I share practical ideas, tutorials, and videos about AI, AI agents, Microsoft technologies, and the Power Platform.

Subscribe on YouTube →
Carl de Souza Enterprise Architect at Microsoft Ā· AI Technology Expert

One Response to Using Python to Filter Data in Power Query

  1. Thanks a lot for sharing this, it has been really helpful! I’ve followed these steps and it works for most of my dashboards, however for one of them whenever I try to apply the changes from the query editor, I always get the follow error:

    DataSource.Error: ADO.NET: A problem occurred while processing your Python script.
    Here are the technical details: Process must exit before requested information can be determined.
    Details:
    DataSourceKind=Python
    DataSourcePath=Python
    Message=A problem occurred while processing your Python script.
    Here are the technical details: Process must exit before requested information can be determined.
    ErrorCode=-2147467259
    ExceptionType=Microsoft.PowerBI.Scripting.Python.Exceptions.PythonUnexpectedException

    I have made sure all my data source privacy levels are ‘Public’. Do you know what might be causing this error? Thanks a lot.

Leave a Reply

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