Search code examples
javascriptpowerbi-embeddedpowerbi-datasource

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 customer, I would like to have one single report and to dynamically select to with dataset connect in order to load the data.

According to this article it is possible to connect a report to multiple datasets dynamically.

So, I published my "report" and its "dataset" to a premium Workspace in powerbi-online. I tested the embedding and it works perfectly. Then I made a copy of the report and published to the same Workspace, so now I have "report", "dataset", "report (test2)", "dataset (test2)". Note that "dataset" and "dataset (test2)" are identical except for their name. And since all components ("report", "dataset" and "dataset (test2)") are in the same Workspace, they all share the same permissions (right?).

So, following the example in the article I implemented the javascript like this:

var reportLoadConfig = {
       type: "report",        
       tokenType: models.TokenType.Embed,
       filters: [filter_user],
       datasetBinding: {
           sourceDatasetId: "67e095eb-0570-444f-a2c5-0c7b68f9ff85", # dataset
           targetDatasetId: "7a274ca6-7ed0-4bd1-a832-f9732f96164a"  # dataset (test2)
       }
   };
 
   $.ajax({
       type: "GET",
       url: "/getembedinfo_filtered?report=06212621-c8cc-4153-a001-09c98ff2ceb4",
       dataType: "json",
       success: function (data) {
           var embedData = $.parseJSON(data);
           reportLoadConfig.accessToken = embedData.accessToken;
           reportLoadConfig["settings"] = {
               filterPaneEnabled: false,
               navContentPaneEnabled: true
           };
 
           // Embed Power BI report when Access token and Embed URL are available
           var report = powerbi.embed(reportContainer, reportLoadConfig);

But I get an error "This content isn't available."

Embedding Error: {message: 'LoadReportFailed', detailedMessage: 'Get report failed', errorCode: '403', level: 6, technicalDetails: {…}}


Solution

  • 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. As Kotana Sai suggested in his answer, 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);