
Extending Power BI with Python and R
By :

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.
One of the first steps required to work with data in Power BI Desktop is to import it from external sources:
Figure 1.1 – Browse more connectors to load your data
script
into the search text 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
a) A local installation of Python is required.
b) 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.
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 see how you can use your favorite IDE to develop your own scripts.
Microsoft suggests installing the base Python distribution, but in order to follow some best practices on environments, we will install the Miniconda
distribution. The details of how to do this and why will be covered in Chapter 3.
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 that assigns a pandas DataFrame or an R dataframe to a variable, respectively. You will see concrete examples throughout this book.
Next, let's look at transforming data.
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:
Figure 1.5 – Select the Web connector to import data from a web page
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.
Figure 1.7 – Imported data preview
Figure 1.8 – R and Python script tools into Power Query Editor
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.
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.
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:
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.12 – The R and Python script visuals
Figure 1.13 – Enable the script code execution
Figure 1.14 – The Python visual layout
You can now write your own custom code in the Python editor and run it via the Run script icon highlighted in Figure 1.14 to generate a Python visualization.
A pretty much identical layout occurs when you select R script visual.