Forum Discussion
Error binding a dataset dynamically to a PowerBI embedded report
- 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); } })
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”.
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.