Building Advanced Reporting and Dashboards in .NET with Microsoft Power BI
Why Advanced Reporting Matters in Modern .NET Applications
First of all, we must understand why developing advanced reporting is important for modern .NET applications, and why using Power BI for this is crucial. So, to begin with, using normal solutions will usually give you static reports; this means that your data will be on the poorer side, as your user can’t interact with it, see details, or understand information deeply. Dynamic reports, on the other hand, offer a richer user experience, as data is interactive and your users can explore it, understand differences, modify time spans and filters, etc.
So, if your data will be used to assist in decision-making or data governance, and in a general way focus on operational efficiency, then using dynamic and interactive dashboards is mandatory for you. And, to do so, Microsoft Power BI is, with no doubt, the most robust, complete, and easy-to-use solution. It offers a complete suite to let you transform your data, perform complex calculations, and very easily render interactive, beautiful, and modern reports and graphics to let your users get much richer information out of their data.
Furthermore, you can automate your application to fetch live data from the cloud by using Power BI integration with your back-end and front-end services. That’s very useful, as it allows you to implement automated reports and real-time dashboards with your .NET app, distribute them across different front-end screens, select what each user can have access to or not, and manage permissions, automatically filter data, etc. It opens up a plethora of different ways to use your different dashboards, avoiding business failures while promoting a data-driven culture to help your company scale faster and better. So, if you are looking for reporting and dashboard development services, you should definitely check this very powerful stack out and explore how it can modernize and power up your software products!
Power BI Integration with .NET Applications
Now that we understand how important it is to use Microsoft Power BI to create dynamic dashboards and reports for your application, most of all if it follows a data-driven idea, we must understand how to integrate it with .NET. At the beginning, you must understand that Power BI provides a REST API for managing and reorganizing your dashboards; it is a platform for .NET dashboard development. To authenticate in your front-end app, use a Service Principal and embed it in the HTML using Power BI’s official libraries and development kits.
The great advantage, then, of embedding Power BI in .NET applications is that you can see and interact with a dashboard without needing to leave for another page. This means you won’t have to open a presentation on the platform’s website or embed it in PowerPoint, or anything like that; it can be viewed and read directly in your web app. It is much more convenient for your end user, and also makes your application feel much more modern and interactive, prompting it feel more premium, and adds more value if it is under a paid subscription.
Given this, you must take great care with security and authentication rules to prevent data leaks, unauthorized disclosure of client information, and potentially major legal problems for your company. Taking into account previously written, you should be very mindful about how to add authentication, how to allow a user to see a graph, how to separate graphs between clients if that’s the case, and how to prevent licensing breaches. To do so, you must use a Service Principal to authenticate, and then on your back-end, you must separate what each client will see and how to gain access to each graph. That’s absolutely crucial for enterprise reporting with .NET and Power BI done responsibly and with security.
Real-Time Dashboards in .NET Applications
Now, there are different ways to update your dashboard’s data. It is an important business decision, as there are several different time points to do so, and it changes very drastically the data your user can see; and, in this context, a real-time dashboard is marketed as the best option. But it is not always the best option: actually, it rarely is something you should implement into your application, as we will discuss further. Very frequently, you only need your data to be updated once a day, or sometimes only once a week, depending on the subject of your graph.
The issue is that, in practice, absolute real-time dashboards don’t exist nowadays. They require an absurd amount of data processing, with too many database connections simultaneously, performing read and write operations constantly, putting too much strain on the processing and storage units of your server. Because of this, even real-time dashboards have a small delay of around 5 to 10 seconds in their refreshes. Now, even so, we must understand the use cases in which your dashboard really needs constant updates. For instance, a graph of your monthly revenue across a year doesn’t need to be updated every 5 seconds, nor even every minute: once a month is just fine. If, on the other hand, you need to monitor logistic systems, production monitoring, queueing controls, etc, then you really need your dashboard to be live and your data to be as updated as possible. Still, we need to make it as clear as possible: only use this solution for business intelligence in .NET applications if it is absolutely required, as data piles up, and it will put a severe burden on your servers and systems.
Finally, to integrate everything into your .NET data visualization tools, there are some different tools you can use, and sometimes you will implement many of them at the same time. To update your front-end app in real time when the data is transformed, you should use SignalR; it is used to notify your front-end when new metrics are available, and can also send data to the dataset via API. Also, to force an update in your dataset whenever something happens when creating interactive reports in .NET, you should implement an event-based architecture by implementing queues with RabbitMQ or Kafka. And finally, if your app really needs to use real-time data, then you probably should use data streams to minimize CPU overhead with very few data transformations and continuous delivery to the dataset. That will help with the throttles and high CPU consumption this model of dashboard requires.
Sample Code: Embedding a Power BI Report in a .NET Application
So, to better illustrate everything we have cited, here’s a short example on integration between Power BI and an ASP.NET Core application. In this instance, we are going to use Power BI Embedded to generate an Embed Token on the back-end to render it in the front-end.
NuGet Packages
First, we will install the official .NET packages to add support for Power BI and its configurations:
dotnet add package Microsoft.PowerBI.Api
dotnet add package Microsoft.Identity.Client
Power BI Token Service (Back-end)
Now, for the core of the integration. We must authenticate with Azure AD and generate a valid token for a specific report. To do so, we must read our credentials from the config file (with _configuration), create a client, then create a Power BI client, and finally, we can generate the embed token and send it to the front-end.
PowerBIEmbeddingService.cs
using Microsoft.Identity.Client;
using Microsoft.PowerBI.Api;
using Microsoft.PowerBI.Api.Models;
using Microsoft.Rest;
public class PowerBIEmbeddingService
{
private readonly IConfiguration _configuration;
public PowerBIEmbeddingService(IConfiguration configuration)
{
_configuration = configuration;
}
public async Task<EmbedToken> GenerateEmbedTokenAsync(
Guid workspaceId,
Guid reportId)
{
var tenantId = _configuration["PowerBI:TenantId"];
var clientId = _configuration["PowerBI:ClientId"];
var clientSecret = _configuration["PowerBI:ClientSecret"];
var authority = $"https://login.microsoftonline.com/{tenantId}";
var scopes = new[] { "https://analysis.windows.net/powerbi/api/.default" };
var confidentialClient = ConfidentialClientApplicationBuilder
.Create(clientId)
.WithClientSecret(clientSecret)
.WithAuthority(authority)
.Build();
var authResult = await confidentialClient
.AcquireTokenForClient(scopes)
.ExecuteAsync();
var tokenCredentials = new TokenCredentials(authResult.AccessToken, "Bearer");
using var powerBiClient = new PowerBIClient(
new Uri("https://api.powerbi.com/"),
tokenCredentials);
var generateTokenRequest = new GenerateTokenRequest(
accessLevel: "view",
allowSaveAs: false);
return await powerBiClient.Reports
.GenerateTokenAsync(workspaceId, reportId, generateTokenRequest);
}
}
API Controller Exposing Embed Info
This creates the controller to expose an endpoint to our front-end. It can be protected using JWT, Cookies, and IdentityServer, without ever exposing our secrets.
PowerBIController.ts
using Microsoft.AspNetCore.Mvc;
[ApiController]
[Route("api/powerbi")]
public class PowerBIController: ControllerBase
{
private readonly PowerBIEmbeddingService _embeddingService;
private readonly IConfiguration _configuration;
public PowerBIController(
PowerBIEmbeddingService embeddingService,
IConfiguration configuration)
{
_embeddingService = embeddingService;
_configuration = configuration;
}
[HttpGet("embed-info")]
public async Task<IActionResult> GetEmbedInfo()
{
var workspaceId = Guid.Parse(_configuration["PowerBI:WorkspaceId"]);
var reportId = Guid.Parse(_configuration["PowerBI:ReportId"]);
var embedUrl = _configuration["PowerBI:EmbedUrl"];
var embedToken = await _embeddingService
.GenerateEmbedTokenAsync(workspaceId, reportId);
return Ok(new
{
reportId,
embedUrl,
embedToken = embedToken.Token
});
}
}
Front-end (JavaScript – Power BI Client)
This adds a script to our HTML to call the official Power BI SDK, being responsible for rendering it in the front-end by fetching data from the back-end.
<script src="https://cdn.jsdelivr.net/npm/powerbi-client/dist/powerbi.min.js"></script>
<div id="reportContainer" style="height: 800px"></div>
<script>
async function loadReport() {
const response = await fetch("/api/powerbi/embed-info");
const data = await response.json();
const models = window["powerbi-client"].models;
const config = {
type: "report",
tokenType: models.TokenType.Embed,
accessToken: data.embedToken,
embedUrl: data.embedUrl,
id: data.reportId,
permissions: models.Permissions.Read,
settings: {
panes: {
filters: { visible: false },
pageNavigation: { visible: true },
},
},
};
const container = document.getElementById("reportContainer");
powerbi.embed(container, config);
}
loadReport();
</script>
appsettings.json (Example)
That’s the config file, which you must have in your local setup to store your credentials. In a production environment, these credentials must come from Azure Key Vault or from environment variables in order to guarantee that your data cannot be leaked.
{
"PowerBI": {
"TenantId": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
"ClientId": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
"ClientSecret": "YOUR_SECRET",
"WorkspaceId": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
"ReportId": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
"EmbedUrl": "https://app.powerbi.com/reportEmbed"
}
}
So, that’s the basics for .NET reporting and analytics, and how to implement it in an existing application, both front-end and back-end. From here, you can expand your idea and automate it further by creating more endpoints, more sheets on your front-end, or whatever your use case requires.
Final Considerations for Advanced Reporting in .NET Applications
Given all of this, we can understand how enterprise data visualization .NET is very important for a modern web product, most of all if it is a more premium one. The Microsoft Power BI consulting services niche is, then, very important for developing modern data-driven software solutions and certainly a paradigm that’s here to stay, not merely a fad. And, in this context, .NET is the default go-to technology to develop integration with Power BI: they both are in the Microsoft development environment, so there are several libraries and dev kits to integrate their different tools very easily.
Now, as we have discussed previously, you must think very well about the refresh timespan of your dashboard. Data processing and transformation take a heavy toll on your physical servers and on your services, and if you have many clients and have to store data for several different platforms, then it will add up quickly. This will make your service slow and make your user experience very unpleasant, as bugs and stutters will happen whenever trying to access a dashboard. So you have to plan: if your data isn’t so critical and doesn’t actually require to be real-time, then you should add a time range of a day or even a week, if you can, to make your graphs safer and more reliable as well.
And, in case your company needs assistance in integrating your .NET services with Power BI dashboards, creating dynamic graphs to make your product much more modern and up to date with modern data practices, contact us at Chudovo to help you! We are a tech consultancy with over 15 years of market experience and have completed more than 190 projects with several different stacks and in many different areas, including data, and are masters at .NET. Feel free to ask us for a quotation!