Python and R visuals in Power BI

How Python and R visuals in Power BI draw charts with matplotlib, seaborn or ggplot2 from a dataset dataframe, the 150,000-row, 72 DPI and five-minute limits, supported packages in the service, licensing, and when a custom visual is better.

Short answer: a Python visual or R visual runs a script on the fields you give it and shows the picture the script draws. The fields arrive as a dataframe called dataset, grouped like a table visual (duplicate rows appear once unless you add an index). Use them for charts Power BI does not have: a box plot, a regression with confidence bands, a correlation matrix. They are images: no hover, no click to cross-filter. The limits are 150,000 rows, 72 DPI and five minutes; in the service only supported packages run.

A violin plot of delivery times per carrier, or a correlation heatmap of twenty measures, are three lines of seaborn or ggplot2. In Power BI they are a script visual, and they still react to slicers.

What does a script visual do?

  1. 1
    Fieldsthe columns and measures dragged into the visual
  2. 2
    dataseta pandas or R data frame, grouped, duplicates removed
  3. 3
    Scriptyour Python or R code draws a plot
  4. 4
    ImagePower BI shows the plot; filters re-run the script
From fields to a picture

How do you make one?

  1. In Power BI Desktop, set the Python or R installation in Options, Python scripting or R scripting. Install the packages you need (pandas and matplotlib for Python).
  2. Select Python visual (or R visual) in Visualizations and enable script visuals when asked.
  3. Drag fields into Values. The editor shows the generated code that builds dataset.
  4. Write the script under it and run it.

A box plot of delivery days by carrier in Python:

import matplotlib.pyplot as plt

dataset.boxplot(column="Delivery Days", by="Carrier", grid=False)
plt.suptitle("")
plt.title("Delivery days by carrier")
plt.ylabel("Days")
plt.show()

The same in R with ggplot2:

library(ggplot2)
ggplot(dataset, aes(x = Carrier, y = `Delivery Days`)) +
  geom_boxplot() +
  labs(title = "Delivery days by carrier", y = "Days")

Because fields are grouped, two orders with the same carrier and the same delivery days become one row. Add Orders[Order ID] to the fields so every order is a row.

What are the limits?

Microsoft lists for both languages:

  • 150,000 rows: more are cut to the top 150,000 and a message appears on the image.
  • 72 DPI: the picture is a raster image at 72 DPI.
  • Five minutes: a longer calculation times out with an error.
  • Service packages: Power BI Desktop uses any package you install; the service runs only supported packages (for R, open-source packages from CRAN; no private packages).
  • Licensing: viewing script visuals in the service needs a Pro or Premium Per User license, or a workspace on a Fabric capacity; script visuals in the service work in Fabric regions.
  • Interaction: a script visual is filtered by others but cannot filter them.

When is something else better?

NeedBetter choice
A chart Power BI has (bar, line, scatter)the built-in visual: interactive, fast, themeable
A common statistical chart (box plot, violin)a certified custom visual from AppSource
A one-off analysis with a special libraryPython or R visual, or a Fabric notebook
Results needed as numbers in other visualscompute them in Power Query or a notebook, not in a visual

What goes wrong?

  • Grouped rows. Statistics on deduplicated data are wrong; add a unique ID field.
  • A package missing in the service. The visual works in Desktop and shows an error after publishing; use supported packages.
  • Slow pages. Each script runs on every filter change; keep data small and scripts fast.
  • Expecting interactivity. No tooltips, no selection; put a table next to it for details.

Where can you check this?

Where do you start?

Use a script visual for the one chart Power BI cannot draw, add a unique ID field so no rows are merged, and check the packages are supported in the service before you publish. Custom visuals are the interactive alternative for common special charts.

Keep reading

More from the blog.

Every post

Next

Building this? Say hello.

A question about this post, a pattern you would build differently, or the next thing you want written out: Behnia reads every message.