Using the Python Visualization for Power BI

Leave a comment

In our previous posts, we installed and set up Python for Power BI and used Python as a datasource in a Power BI Desktop report. We also used Python to filter data in Power Query. In this post, we will look at the Power BI Python Visualization.

In Power BI Desktop, select the Python visualization:

You may see the message below. Click Enable:

We will add a couple of fields from our dataset, AccountName and Revenue to Values:

We can see the following Python code is already added:

# The following code to create a dataframe and remove duplicated rows is always executed and acts as a preamble for your script:
# dataset = pandas.DataFrame(AccountName, Revenue)
# dataset = dataset.drop_duplicates()
# Paste or type your script code here:
Let’s add to this code. We will display a bar chart using matplotlib. We have 2 fields in our data, Account Name and Revenue that we will display:
[sourcecode language=””] # The following code to create a dataframe and remove duplicated rows is always executed and acts as a preamble for your script:

# dataset = pandas.DataFrame(AccountName, Revenue)
# dataset = dataset.drop_duplicates()

# Paste or type your script code here:
import matplotlib.pyplot as plt

plt.xlabel(‘Account Name’)
plt.xticks(rotation=’vertical’)
plt.ylabel(‘Revenue’)
plt.title(‘Account Name vs Revenue’)

plt.bar(dataset.AccountName, dataset.Revenue)
plt.show()
[/sourcecode]

This displays our chart in Power BI. Note we are using plt.xticks(rotation=’vertical’) to display the x-axis fields vertically, otherwise they will bunch together:

 

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

Leave a Reply

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