Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.
Disclaimer: I'm very new to Power BI and PowerShell!
I have been trying to import a report .pbix file to a workspace, by following the very detailed tutorial given here by Sirui Sun. To do so, I've been attempting to understand the following piece of code. The .pbix file has to be encoded as form data, as explained in the reference for the create import API.
try { "== Importing $report_name to target workspace" $uri = "https://api.powerbi.com/v1.0/$target_group_path/imports/?datasetDisplayName=$report_name.pbix&nameConflict=Abort" # Here we switch to HttpClient class to help POST the form data for importing PBIX $httpClient = New-Object System.Net.Http.Httpclient $httpClientHandler $httpClient.DefaultRequestHeaders.Authorization = New-Object System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", $token.AccessToken); $packageFileStream = New-Object System.IO.FileStream @($temp_path, [System.IO.FileMode]::Open) $contentDispositionHeaderValue = New-Object System.Net.Http.Headers.ContentDispositionHeaderValue "form-data" $contentDispositionHeaderValue.Name = "file0" $contentDispositionHeaderValue.FileName = $file_name $streamContent = New-Object System.Net.Http.StreamContent $packageFileStream $streamContent.Headers.ContentDisposition = $contentDispositionHeaderValue $content = New-Object System.Net.Http.MultipartFormDataContent $content.Add($streamContent) $response = $httpClient.PostAsync($Uri, $content).Result if (!$response.IsSuccessStatusCode) { $responseBody = $response.Content.ReadAsStringAsync().Result "= This report cannot be imported to target workspace. Skipping..." $errorMessage = "Status code {0}. Reason {1}. Server reported the following message: {2}." -f $response.StatusCode, $response.ReasonPhrase, $responseBody throw [System.Net.Http.HttpRequestException] $errorMessage } # save the import IDs $import_job_id = (ConvertFrom-JSON($response.Content.ReadAsStringAsync().Result)).id # wait for import to complete $upload_in_progress = $true while($upload_in_progress) { $uri = "https://api.powerbi.com/v1.0/$target_group_path/imports/$import_job_id" $response = Invoke-RestMethod -Uri $uri –Headers $auth_header –Method GET if ($response.importState -eq "Succeeded") { "Publish succeeded!" # update the report and dataset mappings $report_id_mapping[$report_id] = $response.reports[0].id $dataset_id_mapping[$dataset_id] = $response.datasets[0].id break } if ($response.importState -ne "Publishing") { "Error: publishing failed, skipping this. More details: " $response break } Write-Host -NoNewLine "." Start-Sleep -s 5 } } catch [Exception] { Write-Host $_.Exception Write-Host "== Error: failed to import PBIX" Write-Host "= HTTP Status Code:" $_.Exception.Response.StatusCode.value__ Write-Host "= HTTP Status Description:" $_.Exception.Response.StatusDescription continue }
However, my pbix file is not bound to a dataset (I have multiple reports bound to one dataset as described here), and I constantly get a 400 Bad Request error, along with the error "PowerBIModelNotFoundException". As the dataset is already available in the workspace, I'm looking for a means to bind the report to the data set during the import. Is that possible?
Thank you in advance!
Solved! Go to Solution.
I cracked it by finally figuring out where I had to look on this board, also taking my inspiration from here. As some of you might know by now, the problem was the Connections file in the pbix zip archive that had to be altered, so that it would containsinformation about the dataset that the report is supposed to be connected to.
Looking into the structure of the connections file, it is nothing more than a hashtable converted to JSON, and if one wishes, the connection can be easily modified by renaming the pbix file to a zip file, unzipping, rewriting the connections file, zipping and renaming again.
I cracked it by finally figuring out where I had to look on this board, also taking my inspiration from here. As some of you might know by now, the problem was the Connections file in the pbix zip archive that had to be altered, so that it would containsinformation about the dataset that the report is supposed to be connected to.
Looking into the structure of the connections file, it is nothing more than a hashtable converted to JSON, and if one wishes, the connection can be easily modified by renaming the pbix file to a zip file, unzipping, rewriting the connections file, zipping and renaming again.
Im having a sililar issue. What did you have to change in the connection file?
@Anonymous wrote:
I figured it out by finally figuring out what I had to look for on this board, taking my inspiration from here. As some of you might know by now, the problem is the Connections file in the pbix zip archive that has to be altered, so that it contains information about the dataset that the report is connected to.
Looking into the structure of the connections file, it is nothing more than a hashtable converted to JSON, and if one wishes, the connection can be easily modified by renaming the pbix file to a zip file, unzipping, rewriting the connections file, zipping and renaming again.
@Anonymous
So you mean someone has unzipped the pbix file and changed the connection file? By no means it is supported offcially, I'd suggest avoiding such operations.
@Eric_Zhang That is exactly it, and you're right - this is also mentioned in the blog post that I referred to.
I posed the question originally, because the clone API did not seem to do what I wanted. When cloning a report from one workspace to another (as described in Sirui Sun's tutorial), the clone API would incorporate the original dataset in each of the multiple reports. I can not make sense of why it does so, and I have been using the exact same code that Sirui used for that purpose.
Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City
Check out the April 2024 Power BI update to learn about new features.