Skip to content

Python in Power BI: Enabling Python Visualizations

  • September 24, 2020

Return to Resources

One of the nicer features of tools like Power BI and Tableau is that you can use a plethora of connection types to get both the data and visualizations that you need. Power BI allows users to leverage their Python knowledge in a manner that can be very easily shared with others once promoted to the server. If you are not familiar with Python, I highly recommend practicing in a Python IDE first before using Power BI and Python together.

Python visualizations are great if you have a very particular chart to make, as the standard PBI graphs don’t always have a lot of options for customizations. If you are fairly advanced in data and visualizations but not familiar with DAX, Python scripts in PBI is another way around the standard visualization options. While there are also additional visualization options in the PBI marketplace as well, these are not always free, but Python in PBI can help here as well.

If you are using an Anaconda environment for Python, please see the setup section in our previous blog post, Python for Power BI: Enabling Python Scripts with Anaconda Environment to learn how to run a Python script in Anaconda.

Once you have PBI open and have loaded your dataset (any data source can be used for Python visualizations), under the Visualizations menu, find the Python icon and click or drag it onto the workspace. If you get a message about enabling script visuals, click “Yes.”

Python in Power BI_Enabling Python Visualizations_1_200924

 

Your dashboard will look something like this:

Python in Power BI_Enabling Python Visualizations_2_200924

 

Drag all the fields you need to use in the plotting script you have under Values. If they are not under values, you will not be able to access them as part of your visualization script. If you are using numerical values, most likely you will want to remember to select “Don’t Summarize” by clicking on the down arrow on the box in the values menu.

Python in Power BI_Enabling Python Visualizations_3_200924

You can then start writing your script in either the Python Editor window, or your selected Python Editor by selecting the arrow in the black band at the top of the Python Editor window.

Python in Power BI_Enabling Python Visualizations_4_200924

 

Keep in mind that PBI automatically starts off your script like below, so ensure those libraries are already installed/updated even if you don’t use them in your own script.

Python in Power BI_Enabling Python Visualizations_5_200924

Unless you decide to rename it, “dataset” is the pandas dataframe of the measures you’ve dragged under values. Here’s a quick script as an example, but if you write one of your own, be sure to end with plt.show() or a similar command to generate an image.

Python in Power BI_Enabling Python Visualizations_6_200924

 

The results will look like this:

Python in Power BI_Enabling Python Visualizations_7_200924

 

You can set figsize in the plt.figure() command, but the size is still bound by the manually sized box on the workspace. As a result, you might need to set the margins in your scripts, and unfortunately if you have a larger sized visualization, it will resize to fit and you cannot have a scroll bar. On the bright side, you can add slicers to the workspace, and the visualizations will change as the slicers do. Once you’ve uploaded the workbook to the server, any workbook viewers do not need to have Python installed; they can still view and interact with your custom visualizations!

Data Governance: A Necessity in Modern Asset Management

Twenty-five years ago, data governance was not something top of mind for financial firms. The world of data was much...

by Colleen Avallone