March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Early bird discount ends December 31.
Register NowBe one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now
The purpose of this article is to embed analytics, reports, and visualizations that different users may require in an organization to 3rd party applications or software as a service SAAS and platform as a service PAAS application.
Embedding in Power BI enables ISVs and developers to integrate Power BI content (reports, dashboards, and tiles) directly into an application.
You just acquire and pay for the dedicated capacity required to serve the content and each individual consumer doesn’t require a license. There only needs to be a single “master” Power BI Pro account.
The power BI embedded offering is targeted at ISVs and developers (i.e. building apps for external users).
For embedding Power BI within the enterprise context, for instance, internal portals, and SharePoint, then Microsoft Power BI Premium is better suited as suggested here https://docs.microsoft.com/en-us/power-bi/developer/embedded-faq
Head over to dev.powerbi.com/apps
This also needs to have access to the Azure id tenant.
For app type, generally, a native app is selected as it has more options.
The current URL is taken from documentation; however, this is the URL that it is going to redirect once we successfully authenticate.
It will return a client ID
If you have chosen a web app, the system will generate a client ID and client secret.
There is a couple of permissions that we need to configure. To do that, we head over to the Azure portal.
To click on permissions, we click on settings.
Click on Required permissions in the API access menu and in Windows Azure Active Directory, click on Access the directory as the signed-in user.
Click Save and hit Grant Permissions at the top of the list.
And for Power BI Service, uncheck 2 options;
- Read and write access to all content in the tenant.
Then click Save and Grant Permissions.
This is Power BI embedded dedicated capacity resource.
When the user clicks on the resource, the system takes them to the resource details view within the Azure tenant.
From there, we click on Add to add the Power BI embedded resource.
Data points for creating Power BI Embedded.
This is the administrator that would be linking the app workspace.
The resource would be available in a minute or 2.
If you pause, you won’t be billed.
If you want to add Power BI Master and Pro to others, you can do so.
Now we need to create an app workspace in Power BI service, put up some content in there and link up that resource in azure to the app workspace.
The diamond icon indicates that it is premium now.
You can do so by using add data set the option in the app workspace.
That’s it!! The sample content is ready for embedding in a web app outside of powerbI.com.
What is an embed token? This is something required by the front end typing into that Power BI JavaScript library. The actual embed token comes from Power BI API and whether you want to import a dashboard or report in its entirety, each piece of embed requires a token to embed within the application. This is where Azure functions come in. We are going to have a slab of code that is running as an azure function, that will invoke the Power BI API in order to get that embed token and turn that as a JSON response to the front end.
Give it a valid name, valid subscription and select other necessary details.
Select the function list and click the plus sign and select the HTTP trigger function and the language that you are looking to use.
Name the function and select create.
There is already some sample code there that would obviously be running.
We would need this to be set up for Power BI embedded.
- CLIENT_ID; Value: Azure AD > App Name > Application ID
- GROUP_ID; Value: <Power BI Group ID> (https://powerbi.com/groups/<Group ID>/reports/<Report ID>/ReportSection) we simply need to hardcode this into application.
- REPORT_ID; Value: <Power BI Report ID>
- USERNAME; Value: Your Power BI Pro Account Username
- PASSWORD; Value: Your Power BI Pro Account Password
Now click to app settings,
Then click Save.
Now go back to function, and create new file, filename.json.
Copy and paste this
{
"frameworks": {
"net46":{
"dependencies": {
"Microsoft.IdentityModel.Clients.ActiveDirectory": "3.19.4",
"Microsoft.PowerBI.Api": "2.0.12"
}
}
}
}
And save so that would have installed the dependencies.
Now in the run.csx overwrite the code with this one.
#r "System.Web.Extensions"
using System.Configuration;
using System.Net;
using System.Text;
using System.Web.Script.Serialization;
using Microsoft.IdentityModel.Clients.ActiveDirectory;
using Microsoft.PowerBI.Api.V2;
using Microsoft.PowerBI.Api.V2.Models;
using Microsoft.Rest;
// Static Values
static string authorityUrl = "https://login.windows.net/common/oauth2/authorize/";
static string resourceUrl = "https://analysis.windows.net/powerbi/api";
static string apiUrl = "https://api.powerbi.com/";
static string clientId = ConfigurationManager.AppSettings["PBIE_CLIENT_ID"];
static string username = ConfigurationManager.AppSettings["PBIE_USERNAME"];
static string password = ConfigurationManager.AppSettings["PBIE_PASSWORD"];
static string groupId = ConfigurationManager.AppSettings["PBIE_GROUP_ID"];
static string reportId = ConfigurationManager.AppSettings["PBIE_REPORT_ID"];
public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log)
{
// Authenticate with Azure Ad > Get Access Token > Get Token Credentials
var credential = new UserPasswordCredential(username, password);
var authenticationContext = new AuthenticationContext(authorityUrl);
var authenticationResult = await authenticationContext.AcquireTokenAsync(resourceUrl, clientId, credential);
string accessToken = authenticationResult.AccessToken;
var tokenCredentials = new TokenCredentials(accessToken, "Bearer");
using (var client = new PowerBIClient(new Uri(apiUrl), tokenCredentials))
{
// Embed URL
Report report = client.Reports.GetReportInGroup(groupId, reportId);
string embedUrl = report.EmbedUrl;
// Embed Token
var generateTokenRequestParameters = new GenerateTokenRequest(accessLevel: "view");
EmbedToken embedToken = client.Reports.GenerateTokenInGroup(groupId, reportId, generateTokenRequestParameters);
// JSON Response
EmbedContent data = new EmbedContent();
data.EmbedToken = embedToken.Token;
data.EmbedUrl = embedUrl;
data.ReportId = reportId;
JavaScriptSerializer js = new JavaScriptSerializer();
string jsonp = "callback(" + js.Serialize(data) + ");";
// Return Response
return new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new StringContent(jsonp, Encoding.UTF8, "application/json")
};
}
}
public class EmbedContent
{
public string EmbedToken { get; set; }
public string EmbedUrl { get; set; }
public string ReportId { get; set; }
}
If there are any issues function picking up dependencies, then restart the function app by hit stop, hit start and run the function.
If you are looking to test the function, hit get function URL, copy the function URL and copy the URL in the browser and you will get the response in the form of JSON.
Create an Index.html file and paste the code given below;
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>Power BI Embedded Demo</title>
<link rel="shortcut icon" href="data:image/x-icon;," type="image/x-icon">
<script type="text/javascript" language="javascript" src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script type="text/javascript" language="javascript" src="https://rawgit.com/Microsoft/PowerBI-JavaScript/master/dist/powerbi.min.js"></script> </script>
</head>
<body>
<h1>Power BI Embedded Demo</h1>
<div id="reportContainer" style="width: 80%; height: 600px;"></div>
</body>
<script>
$(document).ready(function () {
var getEmbedToken = "INSERT_YOUR_AZURE_FUNCTION_URL_HERE";
$.ajax({
url: getEmbedToken,
jsonpCallback: 'callback',
contentType: 'application/javascript',
dataType: "jsonp",
success: function (json) {
var models = window['powerbi-client'].models;
var embedConfiguration = {
type: 'report',
id: json.ReportId,
embedUrl: json.EmbedUrl,
tokenType: models.TokenType.Embed,
accessToken: json.EmbedToken
};
var $reportContainer = $('#reportContainer');
var report = powerbi.embed($reportContainer.get(0), embedConfiguration);
},
error: function () {
alert("Error");
}
});
});
</script>
</html>
Now get the function URL that you just got and paste it into the index.html file as the value in the variable getEmbedToken.
Save and open the index.html file in the browser.
That’s It. I hope now you know more about text analytics in Microsoft Azure and Power BI ?. May you know someone who would find this article informative, please share!, follow me on Twitter or visit my website and stay tuned.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.