Advance your Data & AI career with 50 days of live learning, dataviz contests, hands-on challenges, study groups & certifications and more!
Get registeredJoin us at FabCon Atlanta from March 16 - 20, 2026, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM. Register now.
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
Solved! Go to Solution.
@Anonymous You're running into a common issue with the Power BI Admin API's Get Activity Events endpoint. The key problem is that this API only supports querying one day at a time—not a range of multiple days.
What’s Causing the 400 Error
How to Fix It
Update your code to loop through each day individually. Here’s a quick fix:
from datetime import datetime, timedelta import requests if "access_token" in token: access_token = token["access_token"] headers = { "Authorization": f"Bearer {access_token}" } for i in range(2): # Loop through past 2 days day = datetime.utcnow() - timedelta(days=i) start_str = day.strftime('%Y-%m-%dT00:00:00.000Z') end_str = day.strftime('%Y-%m-%dT23:59:59.000Z') url = f"https://api.powerbi.com/v1.0/myorg/admin/activityevents?startDateTime={start_str}&endDateTime={end_str}" response = requests.get(url, headers=headers) print(f"Day {i+1}: {response.status_code}") if response.status_code == 200: print("Success") else: print("Failed") print(response.text)
Pro Tips
You can find more details in this Microsoft Fabric Community thread and Stack Overflow discussion.
Both the startDateTime and endDateTime query parameters must both be in the same UTC day and also be enclosed in single quotes.
May I ask if you have resolved this issue? We would like to confirm if our community member @anilgavhane answer resolves your query or if you need further help
If we don’t hear back, we’ll go ahead and close this thread. For any further discussions or questions, please start a new thread in the Microsoft Fabric Community Forum we’ll be happy to assist.
Thank you for being part of the Microsoft Fabric Community.
@Anonymous You're running into a common issue with the Power BI Admin API's Get Activity Events endpoint. The key problem is that this API only supports querying one day at a time—not a range of multiple days.
What’s Causing the 400 Error
How to Fix It
Update your code to loop through each day individually. Here’s a quick fix:
from datetime import datetime, timedelta import requests if "access_token" in token: access_token = token["access_token"] headers = { "Authorization": f"Bearer {access_token}" } for i in range(2): # Loop through past 2 days day = datetime.utcnow() - timedelta(days=i) start_str = day.strftime('%Y-%m-%dT00:00:00.000Z') end_str = day.strftime('%Y-%m-%dT23:59:59.000Z') url = f"https://api.powerbi.com/v1.0/myorg/admin/activityevents?startDateTime={start_str}&endDateTime={end_str}" response = requests.get(url, headers=headers) print(f"Day {i+1}: {response.status_code}") if response.status_code == 200: print("Success") else: print("Failed") print(response.text)
Pro Tips
You can find more details in this Microsoft Fabric Community thread and Stack Overflow discussion.
May I ask if you have resolved this issue? If so, Can you please share the resolution steps here. This will be helpful for other community members who have similar problems to solve it faster.
If we don’t hear back, we’ll go ahead and close this thread. For any further discussions or questions, please start a new thread in the Microsoft Fabric Community Forum we’ll be happy to assist.
Thank you for being part of the Microsoft Fabric Community.
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
Hi @Anonymous,
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
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.
Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!
| User | Count |
|---|---|
| 13 | |
| 6 | |
| 2 | |
| 2 | |
| 2 |