Using Python to Filter Data in Power Query

Leave a 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:

# 'dataset' holds the input data for this script

import pandas as pd

dataset_filtered = dataset.query('Employees > 5000')

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.

ABOUT CARL DE SOUZA

Carl de Souza is a developer and architect focusing on Microsoft Dynamics 365, Power BI, Azure, and AI.

carldesouza.comLinkedIn Twitter | YouTube

 

Leave a Reply

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