Forum Discussion
Issue with Power BI Admin API 'Get Activity Events' Call using Python F-String and Token Handling
- 11 months ago
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
- The API expects:
- startDateTime and endDateTime to be on the same calendar day
- Both in UTC ISO 8601 format
- You’re passing a 2-day range, which violates the API’s constraints
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
- Make sure your app has AuditLog.Read.All permission
- Use UTC time and avoid local timezone offsets
- Consider paginating results if the volume is high
You can find more details in this Microsoft Fabric Community thread and Stack Overflow discussion.
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
- The API expects:
- startDateTime and endDateTime to be on the same calendar day
- Both in UTC ISO 8601 format
- You’re passing a 2-day range, which violates the API’s constraints
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
- Make sure your app has AuditLog.Read.All permission
- Use UTC time and avoid local timezone offsets
- Consider paginating results if the volume is high
You can find more details in this Microsoft Fabric Community thread and Stack Overflow discussion.