How to Create a Report in Embed View via Power BI API: A Step-by-Step Guide
Creating dynamic and insightful reports is pivotal in today's data-driven environment. Microsoft Power BI stands out as a robust tool that allows organizations to visualize their data in comprehensive reports. However, the true power of Power BI becomes evident when you unlock its capabilities via the Power BI API, particularly when embedding these reports directly into your applications. This guide will walk you through creating a report in an embed view using the Power BI API's GenerateTokenForCreateInGroup
function, ensuring you can seamlessly integrate dynamic reporting into your application.
Understanding the Process
To start, it's critical to understand that embedding a Power BI report involves generating an embed token that dictates what resources the embedded component can access. In this case, we'll focus on creating a new report tied to a specific dataset within your Power BI workspace. Remember, Row-Level Security is not supported for this specific API function, meaning that no effective identity can be defined in GenerateTokenRequest
.
Step 1: Set Up Your Environment
Before diving into the code, make sure you have the Power BI JavaScript client library installed in your project. This library makes it easier to work with Power BI embedding features. You can include it in your project by adding a reference to the Power BI JavaScript library in your HTML:
<script src="https://cdn.jsdelivr.net/npm/powerbi-client/dist/powerbi.min.js"></script>
Next, gather your dataset ID and workspace ID from your Power BI environment as you will need these identifiers to generate the embed token and configure the embed parameters.
Step 2: Server-side Configuration
Your server-side code will primarily deal with generating the embed token needed for creating the report. Here's a basic outline of what that looks like in C#:
// Initialize Power BI Client
var client = new PowerBIClient(new Uri(ApiUrl), new TokenCredentials(YourAccessToken, "Bearer"));
// Generate an embed token to create
var generateTokenRequestParameters = new GenerateTokenRequest(
TokenAccessLevel.Create,
“your_dataset_id”,
allowSaveAs: true
);
var tokenResponse = client.Reports.GenerateTokenForCreateInGroup(“your_workspace_id”, generateTokenRequestParameters);
// Populate embed variables (to be passed client-side)
var embedToken = tokenResponse.Token;
var embedUrl = “https://app.powerbi.com/reportEmbed?groupId=” + “your_workspace_id”;
var datasetId = “your_dataset_id”;
Replace your_dataset_id
and your_workspace_id
with your actual dataset ID and workspace ID.
Step 3: Client-side Configuration
On the client side, you'll use the token, URL, and dataset you obtained from the server to configure and embed the report. Here's how:
// Read embed token and other parameters
var embedToken = "<%= this.embedToken %>";
var embedUrl = "<%= this.embedUrl %>";
var datasetId = "<%= this.datasetId %>";
// Get models (models contain enums)
var models = window['powerbi-client'].models;
// Embed configuration
var embedCreateConfiguration = {
type: ‘report’,
tokenType: models.TokenType.Embed,
accessToken: embedToken,
embedUrl: embedUrl,
datasetId: datasetId,
};
// Embed the report within the div element
var report = powerbi.createReport(embedDiv, embedCreateConfiguration);
// Optional: SaveAs report
var saveAsParameters = {
name: “newReportName”
};
report.saveAs(saveAsParameters);
Ensure that your HTML includes a div
element with an appropriate ID that matches embedDiv
in your script for the report to be embedded into.
Final Thoughts
Embedding Power BI reports into your application opens a plethora of opportunities for delivering tailored data insights directly to your users. By following the steps outlined above, development teams can integrate interactive and dynamic reporting capabilities, enhancing the overall data experience within their applications.
Moreover, integration is just the beginning. Analytical insights gained from embedded Power BI reports can further be augmented with platforms like Flowpoint.ai. Tools such as Flowpoint can help you identify all the technical errors impacting your site's conversion rates and offer direct recommendations to fix them, ensuring your embedded reports always serve their purpose efficiently.
Remember, the goal isn't just to display data but to make it actionable. By embedding Power BI reports within your applications, you're one step closer to achieving that objective, offering your users not just data, but insights.
Get a Free AI Website Audit
Automatically identify UX and content issues affecting your conversion rates with Flowpoint's comprehensive AI-driven website audit.