Saturday, January 13, 2024

Lesson 44 - Python Visual in Power BI Desktop

Power BI is a great visualization tool in which now python is also integrated and now we can extend power BI’s capabilities by using data science and Machine learning libraries in Power BI.

In this blog we going to see how to run basic python scripts directly for importing datasets and to use python visual in power BI for visualizations. Below shows simple scatter chart for showing correlation between fields using Python visual in Power BI


How to Proceed?


Importing dataset using Python script

We can run python script directly in Power BI desktop to import a dataset for analysis. we can share those reports to power BI service
  • Check the python script in local python environment and make sure the code works and gives the required output.
  • Python script with user prompt code stops the code execution.
  • Python script runs more than 30mins will get timed out.
  • Python script for loading a basic data frame works.

In Power BI desktop Home page, click on Get data and choose “Python Script” from multiple data sources and press connect.


Paste the python script and click ok.


Once the code runs successfully, a navigator window appears and ask for load data. Click on load data.


Now u can see the “Sample Python” dataset get loaded into Power BI.















Creating chart using Python visual in Power BI

Step 1

Launch power BI desktop app and open the new report page and import the data required.
Refer Lesson 2 – Introduction to Power BI Desktop 


Step 2

In “visualizations” pane click on “Python Visual” which is highlighted in the given figure.

Prerequisites

You need to install Python on your local machine. 


To integrate python with Power BI you need to install packages “matplotlib” and “pandas”

Pandas – data analysis tool, to work on python visual python data must be in pandas data frame
Matplotlib- Library for creating visualization in python
Install these two packages in console or shell, prefix pip command to install the packages









Enable Python Scripting

To enable python scripting in power BI, click on File->Options and settings->Options->Python Scripting. In the Python scripting option page give local python installation path. Click ok.  

Step 3

Drag the data fields into Values section to analyze.  

Based on the fields selected for analyze python editor by default create a data frame named dataset.
It will remove duplicates. By default, aggregation is don’t summarise.


Step 4 Create visual

Here I created a basic scatter chart using python code which shows correlation between age and weight.
Write python code and click Run to generate the chart.


We cannot format the created Python visual in Power BI. Standard formatting can only be done.


Step 5 Save the visual

Finally, your Python Visual is ready. Click save button to save the visual.










When to use Python Visual?

Python visual in power BI can be used when built in visuals in Power BI does not meet your needs, we can use python visual for creating custom visuals. Also, for doing advance analytics and for predicting using machine learning model.

Pros
  • Python visuals allows high level of customization in visuals.
  • Advance analytics capability
  • Many pre-built python packages and visualizations are available which we can make use for our needs.
  • Can do complex data transformations
Cons
  • Coding knowledge is must to work on python visual.
  • Low performance
  • Requires installation and proper configuration of python is needed


Conclusion

Python visuals in Power BI must be used based on the level of customization user needs and complex level of analysis. It is not suitable for all scenarios where we need to evaluate whether built in power BI visuals are sufficient for our needs.


Tags Power BI
Useful links
MS Learn Modules
Test Your Knowledge Quiz

No comments:

Post a Comment