Blog Post

Power BI Community Blog
3 MIN READ

[SimplePBI] Get Tables, Measures, Columns, or Roles info using PBI Rest API and Python

ibarrau's avatar
ibarrau
Icon for Super User rankSuper User
3 days ago

Today, Power BI has become widely adopted. A huge number of companies are already using it, and the number of deployed models and reports is enormous. As a result, the challenges we face today are no longer the same ones we faced in the past.

Previously, the community was more focused on learning Power BI tips and figuring out how to build specific user experience features. This was because many companies were creating their first reports. Today, however, we often need to understand reports that have already been implemented by other people or companies.

How can we understand what is behind a published semantic model? The latest update/release of the Python library SimplePBI, which can be used to interact with the Power BI REST API, can help us retrieve information about the tables, measures, columns, and even roles that exist in a model.

When we need to edit, support, or understand a semantic model in production, it can be a major challenge if proper versioning practices are not in place. Likewise, if we need to quickly understand fine-grained lineage at the table level and we do not have a proper governance catalog, we have a problem.

For these situations, and many others, SimplePBI now makes it easy to query information about a semantic model using Python code.

What can we retrieve?

  • Tables: information about the tables in the model.
  • Measures: all measures in the model, their expressions, and the tables they belong to.
  • Columns: information about the model's columns and the tables they belong to.
  • Roles: information about RLS roles created in the model.
  • Relationships: how relationships are built.
  • Partitions: table partitions with the power query definition.

Let's see how easy it is to query the model.

Prerequisites

The first thing we need is to understand how to use the Power BI REST API. You can take your first steps with it by following this article. The second requirement is to make sure we have version 1.0.4 or later of the library. If you already have the library installed, you can run the following command in the console:

pip install SimplePBI --upgrade

Getting Information from the Model

From this point on, we can query a semantic model or dataset from a specific workspace. First, we authenticate and generate a token:

Then, we create an object for the category we want to work with. In this case, datasets refers to semantic models:

sem = semanticmodels.SemanticModels(t.token)

Although this functionality was developed specifically for the library, the available methods have user-friendly names that closely resemble those already used in the API documentation:

  • list_roles_from_semantic_model
  • list_tables_from_semantic_model
  • get_tables_schema_from_semantic_model

The last method provides information about columns and measures.

We can obtain the workspace ID and semantic model ID from the browser URL when we open the model. It looks like this:

https://app.powerbi.com/groups/[Workspace ID]/datasets/[Dataset ID]/details?experience=power-bi

This is how we would execute a request to retrieve the measures and columns of a table for workspace ws and semantic model ds:

As we can see, it only takes a single line to retrieve all the information. Each item in the rows list within the response dictionary represents either a measure or a column. The type field specifies which one it is. In this example, the first five rows show one column from the Cambiador_unidades table, as well as three measures and one column from the Customer table. Among the most useful pieces of information, we can see the object's name, the data type for columns, and the expression for measures created using DAX.

NOTE: These requests are part of long-running operations, so the response may not be instantaneous and could take a few seconds.

I hope this helps you investigate deployed models more effectively, whether you need to learn about them while providing support, improve your catalog structures, or quickly review measure expressions. Ultimately, the only limit to how you use it is your creativity.

Published 3 days ago
Version 1.0
No CommentsBe the first to comment