Power Query vs. Power BI Rest API

I presented this topic to the Iowa Power BI User Group (Their Meetup), and the entire recording can be viewed here:

Power Query codes from video

Get Token:

let
    output =
        (AzureADTenantID as text, AzureApplicationClientSecret as text, AzureApplicationClientID as text) as text =>
            let
                resource = "https://analysis.windows.net/powerbi/api",
                tokenResponse =
                    Json.Document(
                        Web.Contents(
                            "https://login.windows.net",
                            [
                                RelativePath = AzureADTenantID & "/oauth2/token",
                                Content =
                                    Text.ToBinary(
                                        Uri.BuildQueryString(
                                            [
                                                client_id = AzureApplicationClientID,
                                                resource = resource,
                                                grant_type = "client_credentials",
                                                client_secret = AzureApplicationClientSecret
                                            ]
                                        )
                                    ),
                                Headers = [
                                    Accept = "application/json"
                                ],
                                ManualStatusHandling = {
                                    400
                                }
                            ]
                        )
                    ),
                token_output =
                    tokenResponse[token_type]
                    & " "
                    & tokenResponse[access_token]
            in
                token_output,
    documentation = [
        Documentation.Name = " get-BearerToken.pq ",
        Documentation.Description = " Get Bearer Token needed for Power BI REST API calls ",
        Documentation.Source = "https://www.datameerkat.com",
        Documentation.Version = " 1.0 ",
        Documentation.Author = " Štěpán Rešl "
    ]
in
    Value.ReplaceType(
        output,
        Value.ReplaceMetadata(
            Value.Type(output),
            documentation
        )
    )

Datasets from selected Group:

let
    apiCall =
        Json.Document(
            Web.Contents(
                "https://api.powerbi.com/v1.0/myorg",
                [
                    RelativePath = "admin/groups/"& groupsId &"/datasets",
                    Headers = [
                        #"Content-Type" = "application/json",
                        Authorization = generatedToken
                    ]
                ]
            )
        )
in
    apiCall[value]

Datasets from selected Group - Table generation:

let
    apiCall =
        Json.Document(
            Web.Contents(
                "https://api.powerbi.com/v1.0/myorg",
                [
                    RelativePath = "admin/groups/"& groupsId &"/datasets",
                    Headers = [
                        #"Content-Type" = "application/json",
                        Authorization = generatedToken
                    ]
                ]
            )
        )
in
    #table(
        type table [
            id = text,
            name = text,
            addRowsAPIEnabled = logical,
            configuredBy = text,
            isRefreshable = logical,
            isEffectiveIdentityRequired = logical,
            isEffectiveIdentityRolesRequired = logical,
            isOnPremGatewayRequired = logical,
            webUrl = text,
            targetStorageMode = text,
            createdDate = datetime,
            createReportEmbedURL = text,
            qnaEmbedURL = text,
            upstreamDatasets = list
        ],
        List.Transform(
            apiCall[value],
            each
                {
                    _[id]?,
                    _[name]?,
                    _[addRowsAPIEnabled]?,
                    _[configuredBy]?,
                    _[isRefreshable]?,
                    _[isEffectiveIdentityRequired]?,
                    _[isEffectiveIdentityRolesRequired]?,
                    _[isOnPremGatewayRequired]?,
                    _[webUrl]?,
                    _[targetStorageMode]?,
                    DateTime.From(_[createdDate]?),
                    _[createReportEmbedURL]?,
                    _[qnaEmbedURL]?,
                    _[upstreamDatasets]?
                }
        )
    )
Power Query vs. Power BI Rest API
Older post

Multi-Color Gradient with DAX in Power BI

The Power BI Service hides data that is not visible at first peek but can help us control the Service and properly check whether there are any irregularities. It may sound a bit recursive, but let's get this data into Power BI using Power Query so we can start creating reports from it. Why to leave your favorite tool when there is no need for that at all.

Newer post

Restrict Power BI Integration Button in Microsoft Lists

The Power BI Service hides data that is not visible at first peek but can help us control the Service and properly check whether there are any irregularities. It may sound a bit recursive, but let's get this data into Power BI using Power Query so we can start creating reports from it. Why to leave your favorite tool when there is no need for that at all.

Power Query vs. Power BI Rest API