|
| 1 | +# Demostration: How to Use Power BI REST API |
| 2 | + |
| 3 | +Costa Rica |
| 4 | + |
| 5 | +[](https://github.com/) |
| 6 | +[brown9804](https://github.com/brown9804) |
| 7 | + |
| 8 | +Last updated: 2025-01-17 |
| 9 | + |
| 10 | +---------- |
| 11 | + |
| 12 | +> The Power BI REST API provides programmatic access to several Power BI resources, enabling automation and embedding of analytics. |
| 13 | +
|
| 14 | + |
| 15 | +<details> |
| 16 | +<summary><b>List of References </b> (Click to expand)</summary> |
| 17 | + |
| 18 | +- [Using the Power BI REST APIs](https://learn.microsoft.com/en-us/rest/api/power-bi/) |
| 19 | +- [Reports - Get Report](https://learn.microsoft.com/en-us/rest/api/power-bi/reports/get-report) |
| 20 | +- [Reports Operations](https://learn.microsoft.com/en-us/rest/api/power-bi/reports) |
| 21 | +- [Datasets Operations](https://learn.microsoft.com/en-us/rest/api/power-bi/datasets) |
| 22 | +- [Get-PowerBIWorkspace](https://learn.microsoft.com/en-us/powershell/module/microsoftpowerbimgmt.workspaces/get-powerbiworkspace?view=powerbi-ps) |
| 23 | +- [Admin - Groups GetGroupsAsAdmin](https://learn.microsoft.com/en-us/rest/api/power-bi/admin/groups-get-groups-as-admin) |
| 24 | +- [Admin - WorkspaceInfo GetScanResult](https://learn.microsoft.com/en-us/rest/api/power-bi/admin/workspace-info-get-scan-result) |
| 25 | +- [Push semantic model limitations](https://learn.microsoft.com/en-us/power-bi/developer/embedded/push-datasets-limitations) |
| 26 | +- [Enhanced refresh with the Power BI REST API](https://learn.microsoft.com/en-us/power-bi/connect-data/asynchronous-refresh) |
| 27 | + |
| 28 | +</details> |
| 29 | + |
| 30 | + |
| 31 | +<details> |
| 32 | +<summary><b>Table of Contents</b> (Click to expand)</summary> |
| 33 | + |
| 34 | +- [Overview](#overview) |
| 35 | +- [How to work around the rate limits](#how-to-work-around-the-rate-limits) |
| 36 | + - [Batch Request](#batch-request) |
| 37 | + - [Example Implementation in Python](#example-implementation-in-python) |
| 38 | + |
| 39 | +</details> |
| 40 | + |
| 41 | +## Overview |
| 42 | + |
| 43 | + |
| 44 | +> [!IMPORTANT] |
| 45 | +> There are rate limits for Power BI REST API endpoints. |
| 46 | +> These limits can vary depending on the specific API you're using. |
| 47 | +> Make sure to check out the limits section (e.g., [Limitations Admin - WorkspaceInfo GetScanResult](https://learn.microsoft.com/en-us/rest/api/power-bi/admin/workspace-info-get-scan-result#limitations)) |
| 48 | +
|
| 49 | +| API Endpoint | Rate Limit | Description | |
| 50 | +|------------------|----------------|-----------------| |
| 51 | +| General API Requests | 120 requests per minute per user | Applies to most general API requests| |
| 52 | +| POST Rows Requests (Tables with < 250,000 rows) | 120 requests per minute per dataset | For tables with fewer than 250,000 rows | |
| 53 | +| POST Rows Requests (Tables with ≥ 250,000 rows) | 120 requests per hour per dataset | For tables with 250,000 or more rows| |
| 54 | +| Admin - GetDatasourcesAsAdmin | 50 requests per hour | Specific to the GetDatasourcesAsAdmin endpoint | |
| 55 | +| Specific Endpoints (e.g., Get Report Users) | 200 requests per hour | Applies to certain endpoints like retrieving report users. | |
| 56 | + |
| 57 | +## How to work around the rate limits |
| 58 | + |
| 59 | +> Few stategies below: |
| 60 | +
|
| 61 | +| **Strategy** | **Description** | **Implementation** | |
| 62 | +|--------------------|---------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------| |
| 63 | +| **Batch Requests** | Combine multiple operations into a single batch request. | Use the `/batch` endpoint to send multiple sub-requests in one HTTP request. | |
| 64 | +| **Optimize Queries** | Ensure queries are efficient and retrieve only necessary data. | Use filters and select specific fields to minimize data retrieval. | |
| 65 | +| **Caching** | Store frequently accessed data to reduce repeated API calls. | Implement in-memory or distributed caching systems like Redis or Memcached. | |
| 66 | +| **Rate Limiting** | Control the rate at which your application makes API requests. | Use libraries or frameworks that support rate limiting to manage request frequency. | |
| 67 | +| **Retry Logic** | Handle cases where you hit the rate limit by retrying requests after a delay. | Implement logic to check for HTTP status code 429 and use the `Retry-After` header to determine retry time.| |
| 68 | +| **Parallel Processing** | Distribute API requests over multiple time periods or user accounts. | Schedule requests to avoid hitting the rate limit for a single user or distribute requests across accounts.| |
| 69 | + |
| 70 | +### Batch Request |
| 71 | + |
| 72 | +> Example of this works: |
| 73 | +
|
| 74 | +```mermaid |
| 75 | +graph TD |
| 76 | + A[Client Application] -->|Batch Request| B[Power BI REST API] |
| 77 | + B -->|Response| A |
| 78 | + B --> C[Sub-Request 1] |
| 79 | + B --> D[Sub-Request 2] |
| 80 | + C -->|Response 1| B |
| 81 | + D -->|Response 2| B |
| 82 | +``` |
| 83 | + |
| 84 | +```json |
| 85 | +POST https://api.powerbi.com/v1.0/myorg/$batch |
| 86 | +Content-Type: application/json |
| 87 | + |
| 88 | +{ |
| 89 | + "requests": [ |
| 90 | + { |
| 91 | + "id": "1", |
| 92 | + "method": "GET", |
| 93 | + "url": "/v1.0/myorg/reports/{reportId}/users" |
| 94 | + }, |
| 95 | + { |
| 96 | + "id": "2", |
| 97 | + "method": "GET", |
| 98 | + "url": "/v1.0/myorg/reports/{reportId}" |
| 99 | + } |
| 100 | + ] |
| 101 | +} |
| 102 | +``` |
| 103 | + |
| 104 | +#### Example Implementation in Python |
| 105 | + |
| 106 | +```python |
| 107 | +import requests |
| 108 | + |
| 109 | +def batch_request(access_token, requests): |
| 110 | + url = "https://api.powerbi.com/v1.0/myorg/$batch" |
| 111 | + headers = { |
| 112 | + "Authorization": f"Bearer {access_token}", |
| 113 | + "Content-Type": "application/json" |
| 114 | + } |
| 115 | + body = { |
| 116 | + "requests": requests |
| 117 | + } |
| 118 | + response = requests.post(url, headers=headers, json=body) |
| 119 | + return response.json() |
| 120 | + |
| 121 | +# Example usage |
| 122 | +access_token = "YOUR_ACCESS_TOKEN" |
| 123 | +requests = [ |
| 124 | + { |
| 125 | + "id": "1", |
| 126 | + "method": "GET", |
| 127 | + "url": "/v1.0/myorg/reports/{reportId}/users" |
| 128 | + }, |
| 129 | + { |
| 130 | + "id": "2", |
| 131 | + "method": "GET", |
| 132 | + "url": "/v1.0/myorg/reports/{reportId}" |
| 133 | + } |
| 134 | +] |
| 135 | + |
| 136 | +response = batch_request(access_token, requests) |
| 137 | +print(response) |
| 138 | +``` |
| 139 | + |
| 140 | + |
| 141 | + |
| 142 | +<div align="center"> |
| 143 | + <h3 style="color: #4CAF50;">Total Visitors</h3> |
| 144 | + <img src="https://profile-counter.glitch.me/brown9804/count.svg" alt="Visitor Count" style="border: 2px solid #4CAF50; border-radius: 5px; padding: 5px;"/> |
| 145 | +</div> |
0 commit comments