Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Extending Power BI with Python and R

You're reading from   Extending Power BI with Python and R Perform advanced analysis using the power of analytical languages

Arrow left icon
Product type Paperback
Published in Mar 2024
Publisher Packt
ISBN-13 9781837639533
Length 814 pages
Edition 2nd Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Luca Zavarella Luca Zavarella
Author Profile Icon Luca Zavarella
Luca Zavarella
Arrow right icon
View More author details
Toc

Table of Contents (27) Chapters Close

Preface 1. Where and How to Use R and Python Scripts in Power BI FREE CHAPTER 2. Configuring R with Power BI 3. Configuring Python with Power BI 4. Solving Common Issues When Using Python and R in Power BI 5. Importing Unhandled Data Objects 6. Using Regular Expressions in Power BI 7. Anonymizing and Pseudonymizing Your Data in Power BI 8. Logging Data from Power BI to External Sources 9. Loading Large Datasets Beyond the Available RAM in Power BI 10. Boosting Data Loading Speed in Power BI with Parquet Format 11. Calling External APIs to Enrich Your Data 12. Calculating Columns Using Complex Algorithms: Distances 13. Calculating Columns Using Complex Algorithms: Fuzzy Matching 14. Calculating Columns Using Complex Algorithms: Optimization Problems 15. Adding Statistical Insights: Associations 16. Adding Statistical Insights: Outliers and Missing Values 17. Using Machine Learning without Premium or Embedded Capacity 18. Using SQL Server External Languages for Advanced Analytics and ML Integration in Power BI 19. Exploratory Data Analysis 20. Using the Grammar of Graphics in Python with plotnine 21. Advanced Visualizations 22. Interactive R Custom Visuals 23. Other Books You May Enjoy
24. Index
Appendix 1: Answers
1. Appendix 2: Glossary

Injecting R or Python scripts into Power BI

In this first section, Power BI Desktop tools that allow you to use Python or R scripts will be presented and described in detail. Specifically, you will see how to add your own code during the data loading, data transforming, and data viewing phases.

Data loading

One of the first steps required to work with data in Power BI Desktop is to import it from external sources:

There are many connectors that allow you to do this, depending on the respective data sources, but you can also do it via scripts in Python and R. In fact, if you click on the Get data icon in the ribbon, not only are the most commonly used connectors shown but you can also select other ones from a more complete list by clicking on More...:

Graphical user interface, text, application  Description automatically generated

Figure 1.1: Browse more connectors to load your data

In the new Get Data window that pops up, simply start typing the word script into the search box, and immediately the two options for importing data via Python or R appear:

Figure 1.2 – Showing R script and Python script into the Get Data window

Figure 1.2: Showing R script and Python script in the Get Data window

Reading the contents of the tooltip, obtained by hovering the mouse over the Python script option, two things should immediately jump out at you:

  1. A local installation of Python is required.
  2. What can be imported through Python is a data frame.

The same two observations also apply when selecting R script. The only difference is that it is possible to import a pandas DataFrame when using Python (a DataFrame is a data structure provided by the pandas package), whereas R employs the two-dimensional array-like data structure called an R data frame, which is provided by default by the language.

After clicking on the Python script option, a new window will be shown containing a text box for writing the Python code:

Figure 1.3 – Window showing the Python script editor

Figure 1.3: Window showing the Python script editor

As you can see, it’s definitely a very skimpy editor, but in Chapter 3, Configuring Python with Power BI, you’ll discover how you can utilize your preferred IDE to create your scripts within a more comprehensive and feature-rich editor.

Taking a look at the warning message, Power BI reminds you that no Python engine has been detected, so it must be installed. If you already have Python installed and configured, you will not see this message. Clicking on the How to install Python link will cause a Microsoft Docs web page to open, explaining the steps to install Python.

Microsoft suggests installing the base Python distribution, but in order to follow some best practices on environments (self-contained spaces that allow developers to manage dependencies, libraries, and configurations specific to individual projects), we will install the Miniconda distribution. The details of how to do this and why will be covered in Chapter 3.

If you had clicked on R script instead, a window for entering code in R, similar to the one shown in Figure 1.4, would have appeared:

Figure 1.4 – Window showing the R script editor

Figure 1.4: Window showing the R script editor

As with Python, in order to run code in R, you need to install the R engine on your machine. Clicking on the How to install R link will open a Docs page where Microsoft suggests installing either Microsoft R Open or the classic CRAN R. Chapter 2, Configuring R with Power BI, will show you which engine to choose and how to configure your favorite IDE to write code in R.

In order to import data using Python or R, you need to write code in the editors shown in Figure 1.3 and Figure 1.4, which assign a pandas DataFrame or an R data frame to a variable, respectively. You will see concrete examples throughout this book.

Next, let’s look at transforming data.

Data transformation

It is possible to apply a transformation to data already imported or being imported, using scripts in R or Python. Should you want to test this on the fly, you can import the following CSV file directly from the web: http://bit.ly/iriscsv. Follow these steps:

  1. Simply click on Get data and then Web to import data directly from a web page:
Graphical user interface, application  Description automatically generated

Figure 1.5: Select the Web connector to import data from a web page

  1. You can now enter the previously mentioned URL in the window that pops up:
Figure 1.6 – Import the Iris data from the web

Figure 1.6: Import the Iris data from the web

  1. Right after clicking OK, a window will pop up with a preview of the data to be imported.

    In this case, instead of importing the data as is, click on Transform Data in order to access the Power Query data transformation window:

    Table  Description automatically generated

    Figure 1.7: Imported data preview

    It is at this point that you can add a transformation step using a Python or R script by selecting the Transform tab in Power Query Editor:

    Graphical user interface, table, Excel  Description automatically generated

    Figure 1.8: R and Python script tools in Power Query Editor

    By clicking on Run Python script, you’ll cause a window, similar to the one you’ve already seen in the data import phase, to pop up:

    Graphical user interface, text, application, email  Description automatically generated

    Figure 1.9: The Run Python script editor

    If you carefully read the comment in the text box, you will see that the dataset variable is already initialized and contains the data present at that moment in Power Query Editor, including any transformations already applied. At this point, you can insert your Python code in the text box to transform the data into the desired form.

    A similar window will open if you click on Run R script:

    Graphical user interface, text, application, email  Description automatically generated

    Figure 1.10: The Run R script editor

Also, in this case, the dataset variable is already initialized and contains the data present at that moment in Power Query Editor. You can then add your own R code and reference the dataset variable to transform your data in the most appropriate way.

Next, let’s look at visualizing data.

Data visualization

Finally, your own Python or R scripts can be added to Power BI to create new visualizations, in addition to those already present in the tool out of the box:

  1. Assuming we resume the data import activity started in the previous section, once the Iris dataset is loaded, simply click Cancel in the Run R script window, and then click Close & Apply in the Home tab of Power Query Editor:
Figure 1.11 – Click Close & Apply to import the Iris data

Figure 1.11: Click Close & Apply to import the Iris data

  1. After the data import is complete, you can select either the R visual or Python visual option in the Visualizations pane of Power BI:
    Diagram  Description automatically generated

    Figure 1.12: The R and Python script visuals

    If you click on Python visual, a window pops up asking for permission to enable script code execution, as there may be security or privacy risks:

    Figure 1.13 – Enable the script code execution

    Figure 1.13: Enable the script code execution

  1. After enabling code execution, in Power BI Desktop, you can see a placeholder for the Python visual image on the report canvas and a Python script editor pane at the bottom:
Graphical user interface, application  Description automatically generated

Figure 1.14: The Python visual layout

Once you drag the fields you want to use in your Python script into the Values area, you can write your own custom code into the Python script editor and run it to generate a Python visualization.

A pretty much identical layout occurs when you select R visual.

You have been reading a chapter from
Extending Power BI with Python and R - Second Edition
Published in: Mar 2024
Publisher: Packt
ISBN-13: 9781837639533
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image