How To Embed Power BI Report In Angular 6

In this article, you're going to learn how to embed the Power BI report in Angular after AD authentication.

Introduction

 
Lately, analytics and visualization play a significant role in boosting up businesses.  To that end, PowerBi is one of the most powerful visualization tools in the present market. On the other hand, Angular SPA has become more popular. So today you're going to learn how to embed PowerBi reports in angular applications.
 
Prerequisite
  • Angular App.
  • Sample PowerBi report.
  • power-client. 
Step 1 - Create Angular App using Angular CLI.
 
We are going to create a sample Angular application to embed PowerBi reports using Angular CLI. Use the below command to generate the Angular application.
 
ng new angular-powerbi
 
The generated code structure looks like this.
 
How To Embed Power BI Report In Angular 6
 
Step 2 - Install powerbi-client npm.
 
powerbi-client is an official plugin from powerbi community. Reference link. Use below command to install.
 
npm install --save powerbi-client
 
Step 3 - Code Configuration.
 
After AD authentication you'll get the access token.(If AD is not integrated, we need to write an API to get access token). PowerBi has two categories of reports.
  1. Application Own App(Single license).
  2. User Own App(AD).
After getting access token, you need to configure in your component and html as follow.
 
View
  1. <div style=" width: 100%;" [ngStyle]="{ 'height': (screenHeight-150)+ 'px' }" #reportContainer></div>  
Component
 
Import powerbi-client first like below. 
  1. import * as pbi from 'powerbi-client';  
Later add element reference.
  1. public screenHeight:number;  
  2.  @ViewChild('reportContainer') reportContainer: ElementRef;  
Write an function "ShowReport" which takes access token as parameter.
  1. showReport(accessToken) {  
  2.     // Embed URL    
  3.     let embedUrl = POWERBI.BASE_URL + POWERBI.REPORT_ID + "&groupId=" + POWERBI.GROUP_ID;  
  4.     let embedReportId = POWERBI.REPORT_ID;  
  5.     let config = {  
  6.         type: 'report',  
  7.         accessToken: accessToken,  
  8.         embedUrl: embedUrl,  
  9.         id: embedReportId,  
  10.         settings: {}  
  11.     };  
  12.     let reportContainer = this.reportContainer.nativeElement;  
  13.     let powerbi = new pbi.service.Service(pbi.factories.hpmFactory, pbi.factories.wpmpFactory, pbi.factories.routerFactory);  
  14.     let report = powerbi.embed(reportContainer, config);  
  15.     report.off("loaded");  
  16.     report.on("loaded", () => {  
  17.         console.log("Loaded");  
  18.     });  
  19.     report.on("error", () => {  
  20.         this.getToken();  
  21.     });  
  22. }  
Replace you're  POWERBI.REPORT_ID, POWERBI.GROUP_ID.
 
Call show report method in page load.
  1. ngOnInit() {  
  2.        this.screenHeight = (window.screen.height);  
  3.        var token =this.getToken();  
  4.        this.showReport(token);  
  5.    }  
That's it. Pwerbi report is embedded and starts rendering in your Angular application.
 
Step 4 - Settings 
 
Remove filter panel from the report. Change your embedded url like this:
  1. let embedUrl = POWERBI.BASE_URL + POWERBI.REPORT_ID + "&groupId=" + POWERBI.GROUP_ID + "&filterPaneEnabled=false";  
Remove Navigation pane from the report. Change your embedded url like this:
  1. let embedUrl = POWERBI.BASE_URL + POWERBI.REPORT_ID + "&groupId=" + POWERBI.GROUP_ID + "&navContentPaneEnabled=false";  
To render a specifice page, Replace POWERBI.DASHBOARD_REPSECTION with your page sectionId
  1. let embedUrl = POWERBI.BASE_URL + POWERBI.REPORT_ID + "&groupId=" + POWERBI.GROUP_ID + "&pageName=" + POWERBI.DASHBOARD_REPSECTION";  
  2.    
Filters
  1. let embedUrl = this.appConstants.powerBiBaseUrl + this.appConstants.pbiReportId + "&groupId=" + this.appConstants.pbiGroupId + "&pageName=" + this.appConstants.pbiOrgListingRepSection + "&$filter=" + this.appConstants.pbiTransactionRepFilter + "'" + yourfilterName+ "'";  

Summary

 
In this article, I discussed how to embed a PowerBI report in an Angular application and how to configure settings like remove filter & nav pane and how to render specific pages, and also how to add filters to it. I hope this article is useful.