Accessing Power BI Data with Python Using Power BI REST-API
type
status
date
slug
summary
tags
category
icon
What is Power BI Report Server REST API?
The Power BI Report Server REST API lets developers programmatically access and manage the report server catalog. It supports basic CRUD operations on folders, reports, KPIs, data sources, datasets, refresh plans, and subscriptions. The API also offers advanced features like navigating folder hierarchies, discovering contents, downloading report definitions, modifying parameters, and executing refresh plans. As a RESTful replacement for the older SOAP API, it extends the SQL Server Reporting Services REST API with Power BI Report Server's enhanced capabilities, enabling custom applications, automation, and integration.
Note that this API only applies to local Power BI server deployments, not Microsoft's cloud-based Power BI service, which uses different authentication methods and features.
Authenticating with Power BI Report Server
Authenticating with the Power BI Report Server API involves using the
requests_ntlm2
library and the HttpNtlmAuth
method for NTLM authentication. Set the username
, password
, and baseurl
variables, and then create an instance of HttpNtlmAuth
with the provided credentials. This instance can be used in API requests to authenticate with the Power BI Report Server and access the API endpoints. The example code snippet demonstrates making a sample API request using Python to retrieve the list of Power BI reports available on the server.Get the List of PowerBI Reports
The provided code retrieves a list of Power BI reports from a Power BI Report Server using the
PowerBIReports
endpoint. It makes a GET request to the specified endpoint and authenticates using the provided credentials. The response is then parsed into a Pandas DataFrame for ease of manipulation and analysis. For further information, refer to the document available at Gets an array of PowerBIReport CatalogItems.
Get the List of Folders
You can use the following Python code to retrieve the list of folders in the Power BI Report Server. This code uses the requests library to send a GET request to the appropriate endpoint and processes the JSON response to create a pandas DataFrame containing information about the folders available in the Power BI Report Server.
For further information, refer to the document available at Gets an array of Folder CatalogItems.
Get the List of Refresh Plan
To retrieve the
CacheRefreshPlans
for all Power BI Reports published in the Power BI Report Server, the process involves obtaining the information for all reports and then iterating through each report to retrieve its CacheRefreshPlans
. This is achieved by initially fetching the list of all Power BI Reports and then accessing the CacheRefreshPlans
for each individual report. By consolidating the results from each report, a comprehensive CacheRefreshPlans
dataframe is generated, providing an overview of the refresh plans for all the Power BI Reports.For further information, refer to the document available at Gets the CacheRefreshPlans for a given Power BI Report.
Get the List of Users
To retrieve the list of Users associated with Power BI reports on the Power BI report server, you can use the provided Python code. This code uses the Power BI REST API to fetch the item policies for each Power BI report and extract the user information. After executing the code, you will have a DataFrame containing the report IDs, group user names, and roles of the users associated with each report. You can use this information to manage and analyze the access policies and user permissions for the Power BI reports on the server.
For further information, refer to the document available at Gets ItemPolicies associated with the specified PowerBIReport CatalogItem
Get the List of the Row-level security (RLS)
The code utilizes the Power BI Report Server API to retrieve Row-level security information from Power BI reports. It consists of two functions:
get_df_from_pbrs
to obtain a DataFrame of reports and get_report_related_data
to retrieve data model roles and role assignments for each report. The resulting DataFrames contain details of Row-level security settings within the Power BI reports on the Power BI Report Server. Simply call the functions with the appropriate parameters to retrieve the information.For further information, refer to the document available at Gets the data model roles that are associated with the specified PowerBIReport and Gets the data model role assignments that are associated with the specified PowerBIReport.
Conclusion
In conclusion, this article has shown how you can use Python code to access different parts of the Power BI Report Server via the Power BI Report Server REST API. The code examples demonstrate how you can retrieve Power BI Reports, Folders, Refresh Plans, Users, and Row-level security settings, giving you a good understanding of what you can do with the API.
While the examples mainly use the
GET
method to fetch data, it's worth mentioning that the REST API also supports other HTTP methods like POST
and DELETE
. If you're interested in exploring the Power BI Report Server REST API for other tasks, feel free to leave a comment for more detailed articles and further exploration.- Author:Luca Liu
- URL:http://www.blog.luca-liu.com/article/accessing-power-bi-data-with-python-using-the-power-bi-rest-api
- Copyright:All articles in this blog, except for special statements, adopt BY-NC-SA agreement. Please indicate the source!