Using R and Python to interact with your data
In the previous section, you saw all the ways you can interact with your data in Power BI via R or Python scripts. Beyond knowing how and where to inject your code into Power BI, it is very important to know how your code will interact with that data. It’s here that we see a big difference between the effect of scripts injected via Power Query Editor and scripts used in visuals:
- Scripts via Power Query Editor: This type of script will transform the data and persist transformations in the model. This means that it will always be possible to retrieve the transformed data from any object within Power BI. Also, once the scripts have been executed and have taken effect, they will not be re-executed unless the data is refreshed. Therefore, it is recommended to inject code in R or Python via Power Query Editor when you intend to use the resulting insights in other visuals, or in the data model.
- Scripts in visuals: The scripts used within the R and Python script visuals extract particular insights from the data and only make them evident to the user through visualization. Like all the other visuals on a report page, the R and Python script visuals are also interconnected with the other visuals. This means that the script visuals are subject to cross-filtering and therefore, they are refreshed every time you interact with other visuals in the report. That said, it is not possible to persist the results obtained from the script visuals in the data model.
TIP
Thanks to the interactive nature of R and Python script visuals due to cross-filtering, it is possible to inject code that is useful for extracting real-time insights from data. The important thing to keep in mind is that, as previously stated, it is then only possible to visualize such information, or at the most, to write it to external repositories (as you will see in Chapter 8, Logging Data from Power BI to External Sources). Also, although it is possible to access resources on the internet from a visual script when developing in Power BI Desktop, it is no longer possible to do so when the report is published to the Power BIs Service (you will see what this is about in the next section) due to security issues. This restriction doesn’t exist for scripts used in Power Query.
In the final section of this chapter, let’s look at the limitations of using R and Python when it comes to various Power BI products.