Skip to content

Use Graph API data in Power BI using Logic Apps

Some things in the modern connected world seem so common that you just assume it’s possible by nature. Getting your Microsoft Graph API data into Microsoft Power BI for example. That must be easy peasy right? Well….

When I start looking for ways to do this, I assumed there was a builtin connector available in Power BI that I could use. Guess what? There is not (yet). There is a connector for the Microsoft Security Graph, but that one “only” gives back the data from the security products. Just good to know that it’s out there, but that’s not what we’re looking for.

When you look for solutions on the internet I read about all these methods that work for a while and then breaks. An example of that is the use of the OData feed connector in Power BI. This used to work for a while, but all of the sudden users could not authenticate any longer. Turn out, that OData is not supporting OAth authentication (anymore).

So, I found this article from Yannick that’s a great workaround. It uses an Azure Function that acts as a proxy, so you have to re-write your queries to match your function URL. As this would work for sure, I was looking for an easier approach (no offense Yannick 😉 )

The next option is a custom power Query Connector. There is a good example of how to build this, but when I saw the Github issues related to this, I saw that this method also broke.

So I could not find any working solution for this. I discussed this with my colleagues and we came up with a way to get this done. I’m happy to share this with you guys (and girls). Enough with the chit-chat, on with the show!

Logic Apps to the rescue!

In this demo, I am going to show you how you can use Logic apps (or Power Automate if you prefer) to pull the data out of the Microsoft Graph API. Take a look at the overview of the solution.

  1. Power Query in Power BI sends a POST HTTPS request to kick off the Logic App.
  2. The Logic App uses an HTTP connector with Oath authentication to get the data out of Graph API.
  3. The JSON data is sent back to Power BI.

App registration

Earlier, I wrote a blog about Power Automate where I showed all the steps to create an app registration. I don’t go over all the steps here. You can use the previous blog as a reference if you’re not familiar with app registrations, API permissions, and secrets.

We need an app registration in Azure AD to authenticate to Graph API. Create the app registration and add the right permissions. To see what permissions you’ll need, head over to the Graph API reference. In this example we are getting the Sign-in logs as an example, so we’ll need to add the AuditLog.Read.All and Directory.Read.All permissions. Don’t forget to grant admin consent after you applied the permissions.

When the app registration is created, create a secret. When done, write down the properties of the app registration for later use.

  • Application (client) ID
  • Directory (tenant) ID
  • Secret

Logic App

Next, we are going to create the Logic App. Create a new Logic App and pick the trigger: When a HTTP request is received

Use the following JSON body Schema:

{
    "properties": {},
    "type": "object"
}

When you save the trigger, the HTTPS POST URL will be created. We are going to use this later.

Our next step in the Log App is the HTTP request to call the Graph API. For this example, I calling for the Sign-in Logs using: https://graph.microsoft.com/beta/auditLogs/signIns. For authentication, pick Active Directory OAuth and fill in the client ID, Tenant ID and secret from app registration.

Don’t forget to enable pagination if you request large amounts of data.

Our final step is to create a response. Just pick the body content from the HTTP request.

Your final Logic App will look like this:

Power Query

Head over to Power BI Desktop to create our Power Query.

Switch over to the Advanced Editor and create the query.

let
    url = "https://your_logic_app_endpoint_post_url",
    body  = "{}",
    Parsed_JSON = Json.Document(body),
    BuildQueryString = Uri.BuildQueryString(Parsed_JSON),
    Source = Json.Document(Web.Contents(url,[Headers = [#"Content-Type"="application/json"], Content = Text.ToBinary(BuildQueryString) ] ))
in
    Source

Replace the URL with the URL from your Logic App trigger.

When the query asks for credentials, just pick an anonymous connection.

Next, we need to parse the data and convert it into a table.

You can then expand the data to new rows and expand the columns. Depending on your data, multiple values need to be expanded.

You can expand all the columns and only select the data that you care about. The steps will look like this, depending on what data you are working with:

You can now go on and take care of your dataset to use it in your Power BI reports. Rename the query to a recognizable phrase to keep the overview.

When done, click Close & Apply.

You can now use the data to build your reports. I suck at Power BI, but I’m sure someone can build some nice reports with this data 😉

Wrap up

Using Graph API data in your Power BI reports can give some great insights. The sky is the limit here. I assume, since you stumbled upon this blog, you got your own use-case. To give you some ideas:

TIP: you can clone your Logic App. I won’t recommend building more than one data request behind each trigger.

I bet you can do this better!

I’m sure there are more ways to do this. I’d like to hear back from you. Do you think you have something way better? Let me know!

I’ve also found this workaround. With this solution you’ll have to install the Power BI Gateway, but auto refresh will work.


Update 10-05-2020 ( Azure Blob Storage)

You can also use an Azure Blob Storage Container (or basically any data source) to store the JSON data. This way you can handle much bigger chunks of data. Now, I am not going trough all the steps here, but to give you an idea of how this would work, I created this overview:

Just create a new storage account in Azure and build your Logic App like this:

It will drop the raw JSON data output of the HTTP request into a new blob. Whenever you re-run this Logic App, the blob is replaced by default. In Power BI, use the Azure Blob Storage connector to connect to your data source.

Fill in the path (minus the / at the end).

Next, enter the access key.

Repeat the earlier steps to parse the JSON into tables.

17 thoughts on “Use Graph API data in Power BI using Logic Apps”

    1. Hi Revathi, I assume a (scheduled) refresh should work, but I did not test that. Refresh will trigger the HTTP request from the Logic App. Please let me know if you run into a problem with refresh.

  1. Hi Jan, The schedule refresh fails with this error:
    Failed to update data source credentials: The credentials provided for the Web source are invalid. Status code: 400.
    I tried with Anonymous as well as Basic. Both fails with same error.

      1. I was able to get a schedule refresh set up, you just have to “skip” all the test connections and it does in fact refresh in schedule with established gateway

  2. This works, but needs to make a POST request instead of GET. Change source to

    Source = Json.Document(Web.Contents(url, [Headers=[#”Content-Type”=”application/json”], Content=Text.ToBinary(body)]))

  3. I get this error, I have pagination turned on.

    The action ‘HTTP’ has an aggregated page results size of more than ‘210088831’ bytes. This exceeded the maximum size ‘209715200’ bytes allowed.

  4. Hey, thanks for that. The concept here is fantastic, making the power automate or logic apps call the graph API then return the response to powerBI. however I’m having issues with, I get an error:

    DataSource.Error: Web.Contents failed to get contents from ‘https://prod-07.canadacentral.logic.azure.com/workflows/myappid/triggers/manual/paths/invoke?api-version=2016-06-01&sp=%2Ftriggers%2Fmanual%2Frun&sv=1.0&sig=o4fY-O9SVP22hyC1QFto894EaRrPhnIaL3Gs6nH3Ex0’ (502): Bad Gateway
    Details:
    DataSourceKind=Web
    DataSourcePath=https://prod-07.canadacentral.logic.azure.com/workflows/myappid/triggers/manual/paths/invoke
    Url=https://prod-07.canadacentral.logic.azure.com/workflows/myappid/triggers/manual/paths/invoke?api-version=2016-06-01&sp=%2Ftriggers%2Fmanual%2Frun&sv=1.0&sig=o4fY-O9SVP22hyC1QFto894EaRrPhnIaL3Gs6nH3Ex0

    Can you help me with that?

    Thanks in advance

  5. hello

    i’m trying to use this, but running into some issues. the logic app has some parameters it has to inject, which it reads using outputs.queries step. how can I inject these parameters into the power bi query?

    tks.

  6. This is great! Has anyone managed to get pagination to work? Following all these steps and turning pagination on, I still only get the default number of records returned (1000)

  7. Hi,

    I run into an issue when querying the data from graph for say users I get a timeout error on the http response. Does anyone know how to fix this?

    Thanks

  8. Great share – thanks a lot!

    Were you meanwhile able to use SAS in Power BI to authenticate to the storage account? This seems to be a more secure option than relying on access keys.

Leave a Reply

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