How to create a Power BI custom connector

13 June 2023

Eugene Lebedev

Eugene Lebedev is a Power BI consultant by background. Before founding Vidi Corp in 2021 he created Power BI reporting for Autodesk. His Power Bi reports were used by VPs of Finance and C-suite of Autodesk. As part of Vidi Corp, Eugene created dashboards for Google, Teleperformance, Delta Airlines and 200+ clients worldwide.

Introduction

A custom connector in Power BI is a piece of code written in M which queries data from a data source. This M code usually sends requests to an API and then transforms the data that comes out of the API into a usable format.

Power BI custom connectors also provide a visual interface in order to select the tables you want to import. This would be an interface of a “Get Data” menu. Below is the video demonstration based on the custom connectors that we created for our clients at Vidi Corp.

https://www.youtube.com/watch?v=Tkf6acmmy7U

If you want to create this kind of visual interface, you need to leverage the Power Query SDK inside of the Visual Studio. Here is the official Microsoft documentation on how to do this.

If you want to check whether you can create a custom connector in Power BI for your data source, try googling things like “Xero API documentation” or “Anaplan API documentation”. If you see some results with an official API documentation, it is possible to create a custom connector in Power BI.

The process of creating a custom connector in Power BI

When connecting to APIs in Power BI, we typically need to understand the API's authentication mechanism, request/response structure, and any specific parameters required for data retrieval. Let’s go through this step by step.

1. API (Application Programming Interface): is a set of rules and protocols that allows different software applications to communicate and interact with each other. Every API defines the methods, data formats, and conventions that developers would use when building integrations.

2. JSON: When you send a request to an API, it usually returns data in JSON format. APIs use JSON to transmit and interpret data in a consistent and interoperable manner. JSON represents data as key-value pairs or as arrays of values. In Power BI terms we call it nested data. The basic building blocks in JSON are; Objects, Key and values.

2a. Objects: An object is an unordered collection of key-value pairs enclosed in curly braces {}. Each key is a string, followed by a colon :, and then the corresponding value.

2b. Arrays: An array is an ordered collection of values enclosed in square brackets [].

2c. Values within an array can be of any data type, including objects, arrays, numbers, strings, booleans, or null. Values: Values in JSON can be strings, numbers, booleans (true/false), null, objects, or arrays.

JSON file example

An example of a JSON file

Power BI custom connector rest API

In order to build a Power BI custom connector using a rest API, we should use the option to get data from the Web.

Each data source has its own authentication mechanism to allow users to consume the data. There are 4 common authentication methods:

  • Anonymous (No authentication)
  • Basic (username and password)
  • Bearer tokens
  • Oauth 2.0

Connect to an open API (No Authentication)

Let’s see how to get data from API source which requires no authentication.

Open the Power BI desktop and click on 'Get data'.

Get Data > Web

Select WEB connector from the list and provide API URL.

URL from Web

We are using datausa.io open API to demonstrate this example. There are a lot of sites which provide free API to test and consume data.

OK

  • Power BI will ask for the authentication type for the first time connection. The default options enabled by Power BI are: 
    • Anonymous - This is applicable to Open data source which is accessible to everyone.
    • Windows - This authenticates the data source with logged-in Windows user credentials.
    • Basic - This is applicable when a username and password are given to the users to authenticate the data source.
    • Web API - This authenticates the data source with a secret key or token.
    • Organizational Account - This is applicable for data sources which can be accessible using a Microsoft account (o365).
  • Since we are connecting to an open data source we will select Anonymous authentication.

  • It will take some time to run the API query and then you will see the following data inside of Power Query.

JSON DATA

  • Since data is in JSON format, we need to transform it into table format. Click on the list from the data column.

Edit

  • Power Query will open the list of items. Click on the “Convert to table”  option from the top left to transform it into a table format.

To Table Convert

  • Click on column expand and select the columns required to be expanded. Click ok and data will be available in the required table format.

Convert > Ok

Applied Properties

Connect to an API requiring a Bearer Token

Some APIs rely on authentication using a bearer token. Our Power BI custom connector can send a provided bearer token in the headers to authenticate and to access the API data source. I will demonstrate what a header is later on in this article.

Bearer token should be valid and have access to the data source.There are 2 ways to pass a bearer token to Power BI:

Option 1: Generate token in postman or any other app

This is a good option if your tokens don’t expire often. Some tokens have a lifetime of a year, some only live for 20 minutes. Everything depends on the specific Power BI you are working with. Let me demonstrate how to use a bearer token to authenticate to an API.

Open the Power BI desktop and click on get data. Select the WEB connector and provide your URL.

Select advanced and provide API Url to URL Parts. Add Authorization to the header and provide a token in the format (Bearer Token), click on ok to proceed.

From Web Advanced

If the header is valid and has access to the data source then it will open the list in Power query.

As the token expires after a specific duration, we must generate a token before every call to get the latest data updated in Power BI.

If your token lifetime is short, you can avoid replacing it all the time by writing a function in Power BI to dynamically generate the token on every 'GET call'. Let’s look at a demonstration of how this works below.

Option 2: Generate token dynamically in Power BI

This is applicable when you want to generate the token dynamically on every call instead of passing it manually. The example below covers an API where you have to log in with a username and password. Upon successful login, the API returns a bearer token.

Open Power Query and click on New source -> Blank Query.

Blank Query

Paste below code in the advanced editor and add the details like URL, Username and password. Click on ok to proceed.

let

url = "URL.",

headers = [#"Content-Type" = "application/json"],

postData = Json.FromValue([username="<User Name>",

password= "<Password>"]),

response = Web.Contents(

url,

[

Headers = headers,

Content = postData

]

),

Data = Json.Document(response),

access_token=Data[token]

in

Access_token

Query1

This code will perform the authentication and will generate a token key here.

Now the token key is available we can pass it to the authentication, lets's convert the query into a function.

Add () at the beginning in the advanced editor and click ok. It will convert the query into a function.

Token

Open the dataset and replace the token key with the function name. Make sure we have space between Bearer and the function name.

Data Drilldown 2

Data Drilldown

Now every time this data set is loaded, Power BI will generate a new token using the function and will pass it as a key to the authorization.

Conclusion

This article has provided an overview of some beginner concepts for creating a custom connector in Power BI, creating a power bi customer connector folder and using a power bi custom connector oauth2. I hope that this gives you enough information to understand how the process of creating a custom connector in Power BI is structured.

I will release another article in the future to show how to extract data from an API after you are authenticated. Stay subscribed to our blog in order to not miss it!

If you need specialist help to create a custom connector for you, you can contact us to discuss your project. It usually takes us 1-2 weeks to create a fully functional Power BI custom connector. The prices per custom connector with us are usually in the range of $1,000-2,500 per API.

Microsoft Power Platform

Everything you Need to Know

Of the endless possible ways to try and maximise the value of your data, only one is the very best. We’ll show you exactly what it looks like.

To discuss your project and the many ways we can help bring your data to life please contact:

Call

+44 7846 623693

eugene.lebedev@vidi-corp.com

Or complete the form below