Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

The Power BI Data Visualization World Championships is back! It's time to submit your entry. Live now!

Reply
Basanth
Regular Visitor

How to write a PowerBI custom function for retrieving workitem status from AzureDevOps?

Hi,

 

I am trying to write a custom function for retrieing the status of workitem using AzureDevOps REST api query in PowerQuery.

 

This is the query which currently I am using and not working,

 

 

 

// Define a function to retrieve work item status from Azure DevOps
GetADOWorkItemStatus = (Organization as text, Project as text, WorkItemID as number, Token as text) =>
    let
        // Build the REST API URL
        url = "https://dev.azure.com/" & Organization & "/" & Project & "/_apis/wit/workitems/" & Number.ToText(WorkItemID) & "?api-version=7.1",
        
        // Set up the request headers with the Personal Access Token
        headers = [#"Authorization" = "Basic " & Text.ToBinary(Token)],
        
        // Make the API request
        response = Json.Document(Web.Contents(url, [Headers=headers])),
        
        // Extract the status from the response
        status = response[fields][State]
    in
        status

 

 

 

 

With this I am getting an error " Expression.Error: The 'Authorization' header is only supported when connecting anonymously."

Any hint how can I connect to ADO by passing PAT in PoweRQuery?

1 REPLY 1
Basanth
Regular Visitor

Hi,

 

I could connect to ADO using PAT with the help of Python query.

 

https://community.fabric.microsoft.com/t5/Community-Blog/Using-regular-expressions-in-power-bi-deskt...

https://learn.microsoft.com/en-us/power-bi/connect-data/desktop-python-scripts

 

def get_workitem_status(organization, project, work_item_id, personal_access_token):
    authorization = str(base64.b64encode(bytes(':'+ personal_access_token, 'ascii')), 'ascii')
    headers = {
        'Accept': 'application/json',
        'Authorization': 'Basic '+authorization
    }
    try:
        # Make a GET request to the Azure DevOps REST API
         # Azure DevOps REST API base URL
        base_url = f"https://dev.azure.com/{organization}/{project}/_apis/wit"

        # Work item API endpoint
        api_endpoint = f"{base_url}/workitems/{work_item_id}?api-version=7.1"
        response = requests.get(
        url=api_endpoint, headers=headers)

        # Check if the request was successful (status code 200)
        if response.status_code == 200:
            # Parse the JSON response
            work_item_data = response.json()

            # Extract and return the status of the work item
            return work_item_data['fields']['System.State']
        else:
            # Print an error message if the request was not successful
            print(f"Error: {response.status_code} - {response.text}")
            return None

    except Exception as e:
        print(f"Error: {e}")
        return None

For creating a custom column we can use below snippet for creating new column "WorkItemStatus",

organization = "AAA"
project = "BBB"
personal_access_token = "<PAT>"

status_list=[]
for i in range(len(dataset)):
    numberValue=str(dataset.iat[i,1]) //Here 1 is the column number where you have the data
    status = get_workitem_status(organization, project, numberValue, personal_access_token)
    status_list.append(status)
dataset['WorkItemStatus']=status_list

 

Connection to ADO using PAT using PowerBI Query is still failing.

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! It's time to submit your entry.

January Power BI Update Carousel

Power BI Monthly Update - January 2026

Check out the January 2026 Power BI update to learn about new features.

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.