cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
HamletDRC
Microsoft
Microsoft

Loading data via REST API - query dependent API (parent/child calls)

I am using Power BI to read data from the Azure DevOps REST API. 

 

I want to use PowerQuery to get the pull requests in the ADO instance, and the comment threads associated with this pull requests. Doing this in a traditional programming language would be a 2 step process: 

  1. Get the list of pull requests using HTTP GET: https://docs.microsoft.com/en-us/rest/api/azure/devops/git/pull%20requests/get%20pull%20requests?vie...
  2. For-each item in the result set, get the comment threads using the HTTP GET: https://docs.microsoft.com/en-us/rest/api/azure/devops/git/pull%20request%20threads/list?view=azure-...

The URL to load the comment threads is: 
https://dev.azure.com/{organization}/{project}/_apis/git/repositories/{repositoryId}/pullRequests/{p...

 

I need to somehow query the /threads endpoint and fill in a {pullRequestId} parameter. 

 

Ideally I'd like two takes to show up in Power BI: one for the pull requests and one for the threads. 

1 ACCEPTED SOLUTION
HamletDRC
Microsoft
Microsoft

I found a solution. At a high level: 

  1. Define a "Pull Requests" table that queries the parent REST API. Put the pull request ID in a column named pullRequestId
  2. Define a function that queries the child REST API taking the pullRequestId as a parameter, returning a single List value
  3. Create a table filled with the primary key value and a blank column to hold the list
  4. Fill the blank column with the results of the function call. 

 

Specifically, my function definition is: 

 

let
Source = (pullRequestId) => let
Source2 = Json.Document(Web.Contents(Text.Combine({"https://dev.azure.com/{organization}/{project}/_apis/git/repositories/{repoName}/pullRequests/", Text.From(pullRequestId), "/threads?api-version=6.1-preview.1"}))),
#"Converted to Table" = Table.FromRecords({Source2}),
#"Removed Columns" = Table.RemoveColumns(#"Converted to Table",{"count"})
in
#"Removed Columns"
in
Source

And my table definition is: 

let
Source = Table.SelectColumns( #"Pull Requests" , "value.pullRequestId"),
#"Renamed Columns" = Table.RenameColumns(Source,{{"value.pullRequestId", "pullRequestId"}}),
#"Added Custom" = Table.AddColumn(#"Renamed Columns", "value", each GetPrThread([pullRequestId]))
in
#"Added Custom"

From there you just use Power BI to expand columns as you need. 

View solution in original post

2 REPLIES 2
HamletDRC
Microsoft
Microsoft

I found a solution. At a high level: 

  1. Define a "Pull Requests" table that queries the parent REST API. Put the pull request ID in a column named pullRequestId
  2. Define a function that queries the child REST API taking the pullRequestId as a parameter, returning a single List value
  3. Create a table filled with the primary key value and a blank column to hold the list
  4. Fill the blank column with the results of the function call. 

 

Specifically, my function definition is: 

 

let
Source = (pullRequestId) => let
Source2 = Json.Document(Web.Contents(Text.Combine({"https://dev.azure.com/{organization}/{project}/_apis/git/repositories/{repoName}/pullRequests/", Text.From(pullRequestId), "/threads?api-version=6.1-preview.1"}))),
#"Converted to Table" = Table.FromRecords({Source2}),
#"Removed Columns" = Table.RemoveColumns(#"Converted to Table",{"count"})
in
#"Removed Columns"
in
Source

And my table definition is: 

let
Source = Table.SelectColumns( #"Pull Requests" , "value.pullRequestId"),
#"Renamed Columns" = Table.RenameColumns(Source,{{"value.pullRequestId", "pullRequestId"}}),
#"Added Custom" = Table.AddColumn(#"Renamed Columns", "value", each GetPrThread([pullRequestId]))
in
#"Added Custom"

From there you just use Power BI to expand columns as you need. 

View solution in original post

v-stephen-msft
Community Support
Community Support

Hi @HamletDRC ,

 

I found these similar posts which might be helpful:

Using a REST API as a data source

How to Easily Get Data from Web Source Using Power BI Rest API Calls

Importing data from JSON files and Power BI Rest APIs into Power BI

 

 

 

Best Regards,

Stephen Tao

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Helpful resources

Announcements
MBAS on Demand

2021 Release Wave 2 Plan

Power Platform release plan for the 2021 release wave 2 describes all new features releasing from October 2021 through March 2022.

July 2021 Update 768x460.png

Check it out!

Click here to read more about the July 2021 Updates

Power Query PA Forum 768x460.png

Check it out!

Did you know that you can visit the Power Query Forum in Power BI and now Power Apps

Top Solution Authors
Top Kudoed Authors