Forum Discussion

apoloduvalis's avatar
apoloduvalis
Regular Visitor
2 years ago
Solved

Error binding a dataset dynamically to a PowerBI embedded report

I have a PowerBI embedded report in my web application. The data for each of my customers is stored in their own dataset (semantic model). Instead of creating a pair of report-dataset for each custom...
  • apoloduvalis's avatar
    2 years ago

    After several days banging my head against a wall, I figured out that I needed to call PowerBI API endpoint get_embed_params_for_multiple_reports instead of get_embed_params_for_single_report (the one in the only complete example provided by Microsoft) to get the token that tells the service that the Javascript is already authenticated and authorized to use both the report and the new dataset.

     

    def getembedinfo_filtered(request):
        '''Returns Embed token and Embed URL for PowerBI reports in the embedded workspace'''
        report_id = request.GET['report']
        dataset_id = request.GET['dataset_id']
    
        pbi_config = set_parameters(report_id)
        service = PbiEmbedService(pbi_config)
    
        try:
            embed_token = service.get_embed_params_for_multiple_reports(
                settings.EMBEDDED_WORKSPACE_ID, [report_id], [dataset_id])
            embedinfo = {
                'accessToken': embed_token.accessToken,
                'tokenExpiry': embed_token.tokenExpiry,
                'embedUrl': embed_token.reportConfig[0]['embedUrl'],
            }
            return JsonResponse(embedinfo, safe=False)
        except Exception as e:
            return JsonResponse({'Error': str(e)}, status=400)

     

    After solving the error with the authorization, then I had to deal with the javascript doing the embedding in the web app. The configuration object was wrong. The actual working code is this:

     

        var reportContainer = $(container_id).get(0);
        var reportId = reportContainer.dataset.reportId;
        var datasetId = reportContainer.dataset.datasetId;
    
        // Initialize iframe for embedding report
        powerbi.bootstrap(reportContainer, { type: "report" });
    
        var models = window["powerbi-client"].models;
    
        $.ajax({
            type: "GET",
            url: "/getembedinfo_filtered?report=" + reportId + "&dataset_id=" + datasetId,
            dataType: "json",
            success: function (data) {
                var reportLoadConfig = {
                    type: "report",
                    tokenType: models.TokenType.Embed,
                    accessToken: data.accessToken,
                    embedUrl: data.embedUrl,
                    datasetBinding: {
                        datasetId: datasetId,
                    },
                    settings: {
                        filterPaneEnabled: false,
                        navContentPaneEnabled: true,
                    }
                };
    
                // Embed Power BI report when Access token and Embed URL are available
                var report = powerbi.embed(reportContainer, reportLoadConfig);
            }
        })