How To Embed Microsoft Power BI Charts Into Your React Application
Embedding Microsoft Power BI charts into your React Application can be breeze with the right tools. Be it Report, Dashboard or a Tile.
This article assumes you have a fairly decent idea about Microsoft Power BI and few related terminologies.
Note: This article is updated for the latest release of the package ≥2.0.0
TL;DR
If you're using npm
or yarn
Basic usage for Report, Dashboard or Tile.
Note: I’d like to have some of you as collaborators to improve and maintain it.Raise a PR anytime.
Now, for the those who stayed :)
Embedding a Power BI report into your React Application can be a pain for a beginner. Even though the Microsoft’s documentation on the topic is pretty neat.
I’m just gonna tell you what’s need from a React developer’s perspective and not go into the details of implementation or architecture.
There are two approaches that you can take to embed your Power BI Report:
- User Owns Data
- App Owns Data
The main difference is:
- User Owns Data sample is for Embedding for your organisation, the embedded reports readers can be Power BI Pro or free users in your organisation. Users have to log-in with their PBI account. Power BI Premium license is required. (i.e, Users who are part of your AD)
- App Owns Data sample is for Embedding for your customers, the embedded report readers can be your own customers(say you’re an ISV application provider). So no log-in. Power BI Embedded license is required.(i.e, Users outside your AD)
Source: App-vs-User-Owns-Data
You have to choose who will be using your application that’s gonna contain your embedded report i.e, Internal users(AD User) vs. External users(Non AD).
Now that you’ve decided on which approach you’re gonna choose.
Hopefully, You have your report ready and are able to view it in https://powerbi.microsoft.com.
Now, navigate to your report under your workspaces your URL should look something like this:
https://app.powerbi.com/groups/workspace-id/reports/report-id
for dashboards you might have something like this:
https://app.powerbi.com/groups/workspace-iddashboards/dashboard-id
Report ID: from the URL. 👆
Workspace ID: also, from the URL. 👆
Token: AAD or Embed Token (To authenticate the person accessing the report)
Note: AAD token is used while taking the User Owns Data Approach and Embed token is used when taking App Owns Data Approach.
I'll talk about ways to generate AAD and Embed token in my next article.
Embed Url: (Will consist of Report ID and Workspace ID)
https://app.powerbi.com/reportEmbed?reportId=report-id&groupId=group-id
There are mainly two ways you can proceed with embedding the report into your react application.
- Using an iframe
- Using Power BI JS API provided by Microsoft
Using an iframe is pretty straightforward but, this doesn’t provide the flexibility that a React component would provide, like callbacks or event triggers.
Embedding iframe would something like this:
But, naturally as Javascript developers we tend to go with the more flexible JS API.
You can take a look at Microsoft’s official page:
https://microsoft.github.io/PowerBI-JavaScript/demo/v2-demo/index.html#
They have a very clear explanation and demo in Vanilla JS which uses their Javascript API.
https://microsoft.github.io/PowerBI-JavaScript/demo/v2-demo/index.html#
You might be wondering, the demo is in Vanilla JS so if I have to make it a React Component and use it in my app.hmm, that might take a while 🤔
powerbi-report-component to the rescue! ✨
Check it out https://www.npmjs.com/package/powerbi-report-component. (It’s fairly easy to use :) )
Install the package using:
Usage for Report,
Currently supported features:
- Custom styling by passing style to your embedded report component.
- The component also lets you pass callbacks to trigger on events like:
- Page Change
- Load
- Data Element Clicked
3. Use ‘report’ object returned to parent component for:
- Fullscreen
- Print Report
- Change Mode
- Show or Hide Visual Headers
- Set Filters
- Get Filters
- Remove Filters
- Save edited report when in “edit” or “create” mode.
Usage for Dashboard,
Usage for Tile,
Please go over to README in Github for more information.
Links:
Demo site: http://akshay5995.github.io/powerbi-report-component
GitHub: https://github.com/akshay5995/powerbi-report-component
Sources I found very helpful:
Microsoft/PowerBI-JavaScript
https://microsoft.github.io/PowerBI-JavaScript/demo/v2-demo/index.html#
I would love to hear your thoughts and feedback (also any request for new features at https://github.com/akshay5995/powerbi-report-component/issues).