Power BI

{Microsoft Dataverse} Call Custom Action from Power BI

Recently we had a requirement to call Custom Action from Power BI to get the data which can be shown on reports and dashboards.

We have used Web.Contents from Power BI M query to execute Dataverse Custom action from Power BI. We first created a custom action in Dataverse which has 2 input and 2 output parameters. Plugin steps registered on this custom action to return the data in Json format as output.

Once you have activated the custom action, make sure you test this from Rest builder to see if it works as expected. Now open Power BI Desktop->Get Data->Blank query->Righ click on query and select Advance Editor then paste below code.

let
    TokenUrl = "https://login.microsoftonline.com/" & TokenRequestParam & "/oauth2/token",
    body = "{""client_id"":""" & TokenClient_id & """,""client_secret"":""8yk6t1FG5gXHuJ.~ENgn.0~-O5S1A4mLwP"",""grant_type"":""client_credentials"",""resource"":""" & TokenResource & """}",
    Parsed_JSON = Json.Document(body),
    BuildQueryString = Uri.BuildQueryString(Parsed_JSON),
    Source = Json.Document(Web.Contents(TokenUrl,[Content = Text.ToBinary(BuildQueryString)])),
    #"Converted to Table" = Table.FromRecords({Source}),
    #"Access Token" = Table.SelectColumns( #"Converted to Table", "access_token"),
    AccessToken = #"Access Token"{0}[access_token],

 

    PowerBIRestCall = "https://powerappsinfylearn.api.crm8.dynamics.com/api/data/v9.1/new_PowerBIRestCall",
    jsonbody = "{""datarequest"":""datarequestsampletext"",""executionrequest"":""executionrequestsampletext""}",
    
    headers = [Headers = [#"Authorization"="Bearer "&AccessToken, #"Content-Type"="application/json",#"ODATA-MaxCVersion"="4.0",#"OData-Version"="4.0",#"Cache-control"="no-cache"], Content=Text.ToBinary(jsonbody)],
    ActionResponse = Json.Document(Web.Contents(PowerBIRestCall,headers))

 

in
    ActionResponse

You will have to create three parameters as shown in above image. You can create new parameter by clicking on Manage Parameters button from the ribbon.

In Code we first get the access token by passing the Client ID, Secrete and resource which is CRM URL. Once we get access token we are executing our custom action and passing the access token as bearer token.

We are converting the reponse in table and which can later be used for reporting. Once you click on Okay on Advanced editor you will the response as table as shown below.

Hope this helps!

One thought on “{Microsoft Dataverse} Call Custom Action from Power BI

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