Embedding in Power BI

The topic of “embedding” in Power BI comes up in almost every conversation I have around Power BI. There is a lot of confusion around the topic and the term “embed” is used much too liberally and has created a lot of confusion. I am working on a post that I hope will shed some light on this topic and provide clarity and direction.

However, in the meantime I wanted to share something very cool that Adam Saxton (Guy in a Cube) posted a couple of weeks ago. Adam posted a video that showed how to easily use Power BI embedding APIs to embed content from your tenant into the Power BI Embedded Playground. If you haven’t try the Playground, it is well worth a couple of minutes to take a look. It is a tool that allows you to quickly see what you can do with the Power BI API. The Playground uses sample content, but many have asked me how to get the information they need to make the Playground work with their content.

Up until now the process for getting your embed token and other required information was a bit arduous. Adam shows us how to use the Power BI PowerShell “Invoke-PowerBIRestMethod” cmdlet to easily accomplish this. However, the steps for those of us in GCC (Microsoft’s Government Community Cloud) are a bit different.

First, when use the Power BI PowerShell cmdlet, you must use the “Environment” parameter. In Adam’s example (and what you will likely find in most documentation) you authenticate to the service as follows:


However, if your Power BI tenant is in GCC, you need to use the following:

Connect-PowerBIServiceAccount -Environment usgov 

Accepted values for the “Environment” parameter include:
Public, Germany, USGov, China, USGovHigh, and USGovMil .

Second, Adam uses the “Invoke-PowerBIRestMethod” cmdlet to generate the Embed token using the Service’s REST API.

# Regular Report

$url = "https://api.powerbi.com/v1.0/myorg/groups/e5a6343a-4491-4ae9-9d94-3091b859e0c8/reports/9476c6ef-8092-47ea-8d3d-cd2af4c49c6f/GenerateToken"

$body = "{ 'accessLevel': 'View' }"

$response = Invoke-PowerBIRestMethod -Url $url -Body $body -Method Post

For this to work in GCC, the url needs to be updated to:

$url = "https://api.powerbigov.us/v1.0/myorg/groups/641fb41a-c797-473e-a15c-71315fdc5c12/reports/7e987fdf-9537-4a61-9f15-7714a2ae2b6c/GenerateToken"

NOTE: The Report ID and Group ID will be unique to your environment. The key difference from the original instruction is that the url is https://api.powerbigov.us.

Running this cmdlet will return the required embed token. Adam shows you how to use the embed token along with the Group ID and Report ID to display your report in the Playground. However, there is one final change that GCC users need to make for this to work.

You need to update the “Embed Url” prefex from https://app.powerbi.com/ to https://app.powerbigov.us/

If you have followed Adam’s instructions (with the minor tweets above) you should now see your report in the Playground. Pretty cool!

I modified Adam’s script to accept a report url as a runtime input. Now all you need to do is go to the report you want to embed (from the Power BI UI) and then copy the url from the browser window. When the script runs it will ask you for that url. Simply paste the url in to the PowerShell window and hit <ENTER>. The script will then cleanly output the 3 fields you need for the Playground: the embed token, the full embed url, and the report ID.

Here is the updated script:

# Power BI Embedded Playground
# https://microsoft.github.io/PowerBI-JavaScript/demo/
# Invoke-PowerBIRestMethod Documentation
# https://docs.microsoft.com/powershell/module/microsoftpowerbimgmt.profile/invoke-powerbirestmethod?view=powerbi-ps
# GenerateToken for Reports in a Group
# https://docs.microsoft.com/rest/api/power-bi/embedtoken/reports_generatetokeningroup
# Sign in with a user that has admin rights to App Workspace
Connect-PowerBIServiceAccount -Environment usgov 
# Regular Report
$reportURL = Read-Host "Please enter the url for the Power BI report you would like to embed"
    #example:  https://app.powerbigov.us/groups/641fb41a-c797-473e-a15c-71315fdc5c12/reports/4a6a8ec6-e2cc-4c10-9014-9b9b59526e48/ReportSection50059e7d7b8900b48305
$reportURL -match "/groups/(?<group>.+)/reports/(?<report>.+)/"
$ReportID= $Matches.report
$GroupID= $Matches.group
$url= "https://api.powerbigov.us/v1.0/myorg/groups/$GroupID/reports/$ReportID/GenerateToken"
$EmbedURL = "https://app.powerbigov.us/reportEmbed?reportId=$ReportID&groupId=$GroupID"
$body = "{ 'accessLevel': 'View' }"
$response = Invoke-PowerBIRestMethod -Url $url -Body $body -Method Post
$json = $response | ConvertFrom-Json
$EmbedToken = $json.token

echo "Token:  $EmbedToken `n" 
echo "Embed URL: $EmbedURL `n"
echo "Report ID: $ReportID `n"
#echo "Report URL: $reportURL"
#echo "Report ID: $ReportID"
#echo "Group ID: $GroupID"

This makes it VERY easy for GCC users to use the Embed Playground to generate their embed code and play around with the Power BI Embed APIs.

1 Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s