Embedding Power BI Reports Using Power BI API in React Applications

In today’s data-driven world, the ability to visualize data in real-time is a powerful tool for any business. Microsoft Power BI is a top tool for creating interactive visualizations and gaining business insights. By integrating Power BI with React.js, you can embed these visualizations directly into your web applications. This guide will show you how to embed Power BI reports in your React.js apps using Power BI APIs.

Getting Started

Before starting the integration process, ensure you have the following prerequisites:

  1. A Power BI Account: You’ll need access to the Power BI service. If you don’t have an account then create a new account on the Power BI website.
  2. Power BI Report: Have a published report in your Power BI workspace.
  3. React.js Application: A React application where you will embed the Power BI report.
  4. Power BI API Credentials: These include the application ID and the client secret for authentication.

🔷 Step 1: Power BI Environment Set Up

1.1 Register an App in the Azure Portal

Before using Power BI APIs, you need to register your application in the Azure portal to get the necessary credentials.

  1. First, go to the Azure portal and sign in with your account.
  2. Here select App Registrations and then click on New Registration.
  3. Enter the registered application name and then select the appropriate supported account types.
  4. Click Register to create your application.

1.2 Configure Power BI Permissions

  1. Go to your registered app in Azure Portal.
  2. Select the API permissions option.
  3. Then select Power BI Service > Delegated Permission and choose the permissions needed for your application (e.g., Report.Read.All).
  4. Grant admin consent for the permissions.

1.3 Get all Credentials Needed for Power BI API

Before using API you need the following:

🔹To use API’s you must have tenantId, client_id, client_secret, groupId, reportId all credentials. To get all these credentials you need to follow the following steps-

1. Go to the overview section of Azure registered applications there you will get clientId and tenantId.

2. To get the client_secret you can navigate to the Certificates and Secrets section of the registered application and there you will get the client_secret key.

3. To get the groupId and reportId, go to the Power BI workspace and select the report that you want to show at the front end. There check the URL, you will get both groupId and reportId.

🔹After getting all credentials, go to your Power BI workspace and click on Manage Access. Here add the Azure registered application’s display name. If you will not add your Azure application there and try to use Power BI API then you will get permission issues.

🔷 Step 2: Power BI APIs

2.1 Get an Authorization Token for Power Bi API

To use Power Bi API you need an Authorization token which will be obtained from the following Microsoft login API:

API - https://login.microsoftonline.com/{tenantId}/oauth2/v2.0/token
Type - POST

Body - x-www-form-urlencoded
               grant_type = client_credentials
               client_id = XXXXXXXXXXXXXXXXXX
               client_secret = XXXXXXXXXXXXXXXXXXXXXXXXX
               scope = https://analysis.windows.net/powerbi/api/.default

2.2 Get DatasetId and EmbeddedUrl from the Following API

Following API will return the datasetID and embeddedUrl which will be used in further API to get access token:

API - https://api.powerbi.com/v1.0/myorg/groups/{groupId}/reports/{reportId}
Type - GET
Headers - Authorization: Bearer + <Access token get from first api>

2.3 Get Access Token from the Following API

This API will provide the access token to integrate the Power BI report at the front end.

API - https://api.powerbi.com/v1.0/myorg/GenerateToken
Type - POST
Headers - Authorization: Bearer + <Access token get from first api>
Content - Type: application/json
Body- {
 "datasets": [
    {
         "id": <Datasetid get from api 2.2>
    }
 ],
"reports": [
   {
        "id": <report Id get from api 2.2 url after reports>
   }
 ]
}

These above APIs will provide embeddedUrl, accessToken, and reportId which will be used at the front end to integrate Power BI reports.

Take Your React.js Development to the Next Level with Power BI - Hire Our Developers Now!

🔷Step 3: Install Power BI Client Library in React.js

Open your React.js project and install the Power BI client library using npm:

npm install powerbi-client-react powerbi-client

🔷Step 4: Embed Power BI Reports in React.js

Create a new component in your React application to handle the embedding of Power BI reports:

import { PowerBIEmbed } from 'powerbi-client-react';
import { models } from 'powerbi-client';

const PowerBIReport = () => {
  const embedConfig = {
    type: 'report',
    id: <reportId>,
    embedUrl: <embedUrl>,
    accessToken: <accessToken>,
    tokenType: models.TokenType.Aad,
    settings: {
      panes: {
        filters: {
           expanded: false,
           visible: false,
        },
      },
      navContentPaneEnabled: false,
    },
}

return (
  <div style={{ height: '100vh', width: '100%' }}>
    <PowerBIEmbed embedConfig={embedConfig} />
  </div>
);
};

export default PowerBIReport;

If you want to add filters to the report then you need tableName, columnName, and filter values. Here is the code to embed Power BI reports with a filter:

import { PowerBIEmbed } from 'powerbi-client-react';
import { models } from 'powerbi-client';

const PowerBIReport = () => {
  const embedConfig = {
    type: 'report',
    id: <reportId>,
    embedUrl: <embedUrl>,
    accessToken: <accessToken>,
    tokenType: models.TokenType.Aad,
    filters: [
     {
       $schema: "https://powerbi.com/product/schema#basic",
       target: {
         table: <tableName>,
         column: <columnName>,
    },
    operator: "In",
    values: ["value1", "value2"],
    filterType: models.FilterType.Basic,
    requireSingleSelection: true
   },
  ],
  settings: {
    panes: {
      filters: {
        expanded: false,
        visible: false,
      },
    },
    navContentPaneEnabled: false,
  },
}

return (
  <div style={{ height: '100vh', width: '100%' }}>
    <PowerBIEmbed embedConfig={embedConfig} />
  </div>
 );
};

export default PowerBIReport;
coma

Conclusion

Integrating Power BI with React.js applications opens up a world of opportunities for creating dynamic and data-driven user experiences. By leveraging Power BI APIs and the power-client-react library, you can easily embed interactive reports and dashboards, providing users with real-time insights. With the steps outlined in this guide, you are well on your way to mastering Power BI integration in React.js.

Whether you are developing applications for internal business users or external clients, embedding Power BI reports in your React applications can significantly enhance your data visualization capabilities, leading to more informed decision-making.

Keep Reading

Keep Reading

  • Service
  • Career
  • Let's create something together!

  • We’re looking for the best. Are you in?