Skip to main content

Guide: How to connect to Barentswatch API with Power BI

Step 1: Gather Prerequisites

  • Ensure you have Power BI Desktop installed on your computer.
  • Register a client and obtain the required credentials (Client ID and Client Secret) from the Barentswatch API. Follow the instructions in the API documentation to create the client: Registering a Client.

Step 2: Open Power BI Desktop and Create a New Report

Launch Power BI Desktop. Click on "File" in the top-left corner, select "New," and choose "Blank Report."

Step 3: Connect to Data and Create M-Language Code

  • In the "Home" tab, click on "Get Data."
  • Choose "Blank Query" under "Other."
  • In the "Home" tab, click on "Advanced Editor" in the "Query" section and paste the code below. This code will involve sending a POST request to the token endpoint with your Client ID, Client Secret, and other required parameters, and then use that token to make API requests.
  • Remember to replace placeholders with your actual values. Don't delete the quotation marks. The placeholders are:
    • YOUR_CLIENT_ID: Your registered client ID from Barentswatch.
    • YOUR_CLIENT_SECRET: Your registered client secret from Barentswatch.
    • /YOUR_ENDPOINT: The specific API endpoint you are accessing.
let
clientId = "YOUR_CLIENT_ID",
clientSecret = "YOUR_CLIENT_SECRET",
scope = "api",
tokenUrl = "https://id.barentswatch.no/connect/token",
baseApiUrl = "https://www.barentswatch.no/bwapi",
endpointUrl = "/YOUR_ENDPOINT", // Replace with your specific API endpoint
apiUrl = baseApiUrl & endpointUrl,
body = "grant_type=client_credentials" & "&client_id=" & clientId & "&client_secret=" & clientSecret & "&scope=" & scope,

tokenResponse = Text.FromBinary(Web.Contents(tokenUrl, [Content = Text.ToBinary(body), Headers = [#"Content-Type"="application/x-www-form-urlencoded"]])),
tokenJson = Json.Document(tokenResponse),
accessToken = tokenJson[access_token],

headers = [#"Authorization" = "Bearer " & accessToken],
apiResponse = Web.Contents(apiUrl, [Headers = headers]),
jsonResponse = Json.Document(apiResponse)
in
jsonResponse

Step 4: Specify Data Source Connection

  • After pasting the M-Language code in the Advanced Editor, you might see a warning asking you to specify data source connection.
  • Click on the "Edit credentials" button.
  • In the "Access Web Content" dialog, select "Anonymous."
  • Click "Connect" to proceed.

Step 5: Specify Privacy Levels

  • You might see a warning that "Information is required about data privacy". Press "Continue".
  • In the subsequent modal window that appears, you'll see a list of datasources (queries) on the left side.
  • For each datasource, select either the "Organizational" or "Public" privacy level from the dropdown menu. You'll need to select the same option for both datasources.
  • Click "OK" or "Apply" to confirm the privacy levels.

Step 6: Transform and Load Data (Optional)

  • If needed, perform data transformation in the Power Query Editor.
  • Filter, transform, and shape the data to fit your reporting needs.