Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Find everything you need to get certified on Fabric—skills challenges, live sessions, exam prep, role guidance, and more. Get started

Reply
apoloduvalis
Regular Visitor

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: {…}}



1 ACCEPTED SOLUTION
apoloduvalis
Regular Visitor

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);
        }
    })

 

View solution in original post

3 REPLIES 3
apoloduvalis
Regular Visitor

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);
        }
    })

 

AmosHersch
Microsoft Employee
Microsoft Employee

How are you generating the embed token? Are you using this API Embed Token - Generate Token - REST API (Power BI Power BI REST APIs) | Microsoft Learn ? It's the newer way to generate a token, and it should be used when you intend to embed with dynamic binding.

Please make sure you specify the report ID, and relevant dataset IDs in the generate token request

v-yangliu-msft
Community Support
Community Support

Hi  @apoloduvalis ,

 

You can check the official documentation below, which describes an error message similar to yours, which might be “Report ID doesn't match token”.

vyangliumsft_0-1717666045354.png

Refer to:

Troubleshoot Power BI embedded analytics application - Power BI | Microsoft Learn

As far as I know, the token is generated based on the DatasetID. When you use this token, it must match the DatasetID that was used to generate it. However, in your scenario, the DatasetID is dynamically switching. You need to ensure that the token you obtain matches the DatasetID you are currently using. If they do not match, it will result in an incorrect credential, leading to an error.

You might consider creating two Configs. Use the corresponding Config for whichever report you need to use.

 

This is the related document, you can view this content:

How to troubleshoot and debug your embedding code in Power BI embedded analytics | Microsoft Learn

Solved: Token expiring in Power BI embedded (error 403) - Microsoft Fabric Community

 

Best Regards,

Liu Yang

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Helpful resources

Announcements
July 2024 Power BI Update

Power BI Monthly Update - July 2024

Check out the July 2024 Power BI update to learn about new features.

July Newsletter

Fabric Community Update - July 2024

Find out what's new and trending in the Fabric Community.