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

Score big with last-minute savings on the final tickets to FabCon Vienna. Secure your discount

Reply
luko3
New Member

Issue with Power BI Admin API 'Get Activity Events' Call using Python F-String and Token Handling

Hi everyone,

I'm trying to use the Power BI Admin API to retrieve activity events from the past two days using Python. I'm dynamically generating the startDateTime and endDateTime parameters using datetime.utcnow() and formatting them into the required ISO 8601 format. Here is the link to the documentation: Get Activity Events Within Window.

 

import requests
from datetime import datetime, timedelta

if "access_token" in token:
    access_token = token["access_token"]
    end_time = datetime.utcnow()
    start_time = end_time - timedelta(days=2)

    start_str = start_time.strftime('%Y-%m-%dT%H:%M:%S.000Z')
    end_str = end_time.strftime('%Y-%m-%dT%H:%M:%S.000Z')

    url = f"https://api.powerbi.com/v1.0/myorg/admin/activityevents?startDateTime={start_str}&endDateTime={end_str}"

    headers = {
        "Authorization": f"Bearer {access_token}"
    }

    response = requests.get(url, headers=headers)

    if response.status_code == 200:
        print("it worked")
    else:
        print("it didn't work")
        print(response.status_code)
        print(response.text)
        print(start_str)
        print(end_str)

For some reason it continues to give me a status code as 400 or it tells me that the startDateTime and the endDateTime are invalid. Any guidance or examples of working implementations would be greatly appreaciated!

 

Thanks in advanced

3 REPLIES 3
v-prasare
Community Support
Community Support

We would like to confirm if our community members answer resolves your query or if you need further help. If you still have any questions or need more support, please feel free to let us know. We are happy to help you.

 

Thank you for your patience and look forward to hearing from you.
Best Regards,
Prashanth Are

v-prasare
Community Support
Community Support

Hi @luko3,

We would like to confirm if our community members answer resolves your query or if you need further help. If you still have any questions or need more support, please feel free to let us know. We are happy to help you.

 

 

Thanks,
Prashanth Are

Thomaslleblanc
Super User
Super User

The format of the dates is probably incorrect. Try using Powershell and see if that works. Then, work on formatting the dates correctly in notebook.

Helpful resources

Announcements
August 2025 community update carousel

Fabric Community Update - August 2025

Find out what's new and trending in the Fabric community.