Skip to Content
Course content

202: Connecting R to Power BI

Click on the "Edit" button in the top corner of the screen to edit your slide content.

Look, Power BI is an absolute beast when it comes to standard bar charts and slicers. But the second you want to do something a bit more "statistical"—like a correlation matrix or a complex forecast—you'll find that the built-in visuals start to feel restrictive. That's where we bring in R. Instead of fighting with Power Query's GUI, we can just drop a script right into the middle of our report.

For this lesson, let's imagine we have a dataset of store performance metrics: sales, foot traffic, marketing spend, and average customer spend. We want to see which of these variables actually move together. A correlation heatmap is the perfect tool for this, and it's significantly faster to write in R than to build manually in Power BI.

Feeding the R engine with Power BI data

First, I'm going to load my dataset into Power BI. Once the data is there, I'll click on the "R script visual" icon in the Visualizations pane. Power BI does something helpful here: it automatically creates a dataframe called dataset containing the columns I've dragged into the "Values" bucket. I'm dragging in Sales, Traffic, MarketingSpend, and AvgSpend.

Now, here is the logic I'm following: I need to calculate the correlation matrix first, and then pass that matrix into a visualization. I'm opting for ggplot2 because it gives me the most control over the aesthetics.

Sketching out the correlation heatmap

I'll jump into the R script editor and start typing. I want to melt the correlation matrix into a "long" format so ggplot2 can handle it. Here is the code I'm aiming for:

# Calculate correlation
cor_matrix <- cor(dataset)

# Convert to long format for ggplot
library(reshape2)
melted_cor <- melt(cor_matrix)

# Plot it
ggplot(melted_cor, aes(Var1, Var2, fill=value)) + 
  geom_tile() + 
  scale_fill_gradient2(low="blue", high="red", mid="white", midpoint=0)

Dealing with the "Function Not Found" headache

I hit "Run," and... nothing. Power BI throws a generic error saying it can't find the function ggplot. I stared at my code for a full minute before I realized I'd made the classic rookie mistake: I loaded reshape2, but I completely forgot to load ggplot2.

In a standard RStudio session, I probably have these libraries loaded in my global environment from an hour ago. But Power BI starts a fresh, isolated R session every time the visual refreshes. If it's not in the script, it doesn't exist. I'll add library(ggplot2) to the top and try again.

library(ggplot2)
library(reshape2)

cor_matrix <- cor(dataset)
melted_cor <- melt(cor_matrix)

ggplot(melted_cor, aes(Var1, Var2, fill=value)) + 
  geom_tile() + 
  scale_fill_gradient2(low="blue", high="red", mid="white", midpoint=0) +
  labs(title="Metric Correlations", x="", y="")

Polishing the visual for the dashboard

Now that it's actually rendering, it looks a bit cramped. The axis labels are overlapping. To fix this, I'll add a quick theme adjustment. I'm adding theme_minimal() and rotating the x-axis text. It's a small tweak, but when this ends up on a corporate dashboard, those little details prevent the stakeholders from asking "What does this axis even say?"

The final result is a dynamic heatmap that updates automatically whenever the user filters the data using other Power BI slicers. We've essentially used Power BI as the data pipeline and R as the rendering engine.




📋 Practical Task

Building a Regional Order Distribution Boxplot

You have a dataset in Power BI containing Region (a category) and OrderValue (a numeric value). Your goal is to create an R visual that displays a boxplot of order values grouped by region to identify outliers.

Requirements:

  • Use the ggplot2 library.
  • Map Region to the X-axis and OrderValue to the Y-axis.
  • Use geom_boxplot() to create the visual.
  • Apply theme_bw() to ensure the visual blends well with a professional Power BI report background.
  • Ensure you include all necessary library() calls within the script to avoid execution errors.
Rating
0 0

There are no comments for now.

to be the first to leave a comment.