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...:
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 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:
- A local installation of Python is required.
- 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
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
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:
- Simply click on Get data and then Web to import data directly from a web page:
Figure 1.5: Select the Web connector to import data from a web page
- You can now enter the previously mentioned URL in the window that pops up:
Figure 1.6: Import the Iris data from the web
- 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:
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:
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:
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:
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:
- 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
- After the data import is complete, you can select either the R visual or Python visual option in the Visualizations pane of Power BI:
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
- 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:
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.