Power Automate – Create the Power BI Workspace

We’ve setup the service principal, saved the details securely in Azure Key Vault and used Power Automate to fetch them securely and finally setup the permissions to allow that service principal to use the rest api. This post will finally create the flow to automatically create the Power BI workspace making use of all the preparation.

This post is part of Power Automate and Power BI Rest API series

Start the flow and get service principal details

The will be an instant flow using a manual trigger. Once the flow editor appears, expand the trigger and add a text input for the Workspace Name.

Build an instant cloud flow and then the  trigger expanded to show the Workspace name input

The first action will be to call the child flow that fetches the tenantid, clientid and secret. Select Get Client Tenant and Secret flow from the dropdown and then use PBI-Workspace as the prefix value.

Run a child flow action

HTTP Call to automatically create the Power BI Workspace

FInally we can do the action to create the workspace, for this we add a HTTP action. I’m going to break this down into 3 parts, the URI, the body and the authentication.

HTTP action, the details t

From https://learn.microsoft.com/en-us/rest/api/power-bi/groups/create-group#examples we can see the HTTP starts with POST so we pick that as the method. It also gives us the URI. To save you going to fetch it here it is

https://api.powerbi.com/v1.0/myorg/groups?workspaceV2=True

The next part is the body. This is a very simple piece of JSON with only one field name. Get the value of name to come from the trigger input Workspace name.

{
  "name": "<Workspace Name>"
}

The final part is the authentication. Show the Advanced Options to see the boxes to fill in. Firstly Authentication is Active Directory OAuth. Then the Authority can be left blank. Next the Tenant, Client ID and Secret are dynamic values from running the child flow action. Finally the Audience should be the following

https://analysis.windows.net/powerbi/api

Your flow is now ready to test. Hopefully when you do this it all works and you get green ticks. But when you go to Power BI the workspace is not listed in workspaces you can see. This is due to the workspace was created by the service account, so the service account is the admin, not you. So the next step is to add a workspace admin.

Add Workspace Admin

This is going to require another HTTP call. So if we look at the second example show at https://learn.microsoft.com/en-us/rest/api/power-bi/groups/update-group-user#examples we can see we need 2 pieces of information, the workspace ID and the email address of the workspace admin.

screen grab from the rest api documentation

The workspace ID must have been returned when we create the workspace. If you look in the dynamic content it won’t be listed though so we need either code by hand or we use my favourite trick of Parse JSON.

Showing the successful HTTP step with output body, and the Parse JSON action with the generate from sample button hghlighted

Get the Workspace ID

Go to the test run you did and expand the HTTP call that created the workspace. In the Outputs section there is a Body section which will contain the JSON that was returned. Copy this JSON as this will help complete the Parse JSON.

Then return to editing the flow and add a Parse JSON action. For the content use the Body returned by the HTTP action. JSON schemas are tricky to write by hand so you can generate it from what we copied. Click on the Generate from sample to open a dialog. Paste the output body we copied earlier and click Done. The Parse JSON step will now have a schema.

Request the Admin Email

The flow might not be run by the requester of the workspace, so it makes sense to ask for the admin’s email. Add this as an extra input in the trigger.

New HTTP Request to add User

HTTP step filled in as the instructions below

Add a new HTTP action, and I recommend renaming it. (Yes that should happen to the first one as well!). Again its a POST method and the URI needs id from the Parse JSON step slotted in. The Body is a simple JSON again. Code for both of these can be copied from below. The authentication is exactly the same as the previous HTTP step.

URI

https://api.powerbi.com/v1.0/myorg/groups/<Workspace ID>/users

Body

{
  "emailAddress": "<Admin Email>",
  "groupUserAccessRight": "Admin"
}

Conclusion

You should now have a complete flow that when run asks for Workspace Name and Admin Email and creates a workspace with the admin added.

Flow run box with parameters populated and the created workspace showing the Manage Access pane

This is the MVP of the solution. There are obvious additions such as approvals, notifying the new admin and other various extras that should be included. They may get blogged, lets see.

Over 20 year experience at being passionate about training, solving problems and loving a new challenge especially in the Microsoft's Power Platform suite.

3 Responses

  1. Hi,
    Is there any possibility to check before creating Workspace if the name of Worskapce already exists?

Leave a Reply

Your email address will not be published. Required fields are marked *

Post comment