The ultimate Fabric, Power BI, SQL, and AI community-led learning event. Save €200 with code FABCOMM.
Get registeredCompete to become Power BI Data Viz World Champion! First round ends August 18th. Get started.
Hi Team,
We are using spark to process our data and was able to refresh Power BI Dataset in Python using PBI Rest API. However We want the ability to add and delete partitions in Power BI Dataset using Python. Can you please tell how to achieve partition management in Python?
Thanks,
Abhiram
Solved! Go to Solution.
@abhiram342 , here powershell script has been used, I think you can use same with python
Yes, it is possible to manage partitions in Power BI datasets using Python. Here are the steps you can follow:
Install the Power BI API client for Python: You can use the "powerbiclient" library to interact with the Power BI REST API from Python. To install this library, run the following command in your Python environment:
pip install powerbiclient
Authenticate with the Power BI service: Before you can manage partitions, you need to authenticate with the Power BI service. You can do this by creating an Azure Active Directory (AAD) application and obtaining a client ID and client secret. You can then use these credentials to authenticate with the Power BI service and get an access token that can be used to make API calls.
Get the dataset ID: You need to know the ID of the Power BI dataset that you want to manage partitions for. You can get this ID by using the "get_datasets" API call and filtering the results by name.
Add a partition: To add a partition, you can use the "add_dataflow_partition" API call. This call requires the dataset ID, the name of the dataflow, and the partition key. You can also specify optional parameters such as the partition query, refresh policy, and retention policy.
Delete a partition: To delete a partition, you can use the "delete_dataflow_partition" API call. This call requires the dataset ID, the name of the dataflow, and the partition key.
Here is an example Python code snippet that shows how to add and delete partitions in a Power BI dataset:
from powerbiclient import Report, models
from azure.identity import ClientSecretCredential
import requests
# Authenticate with the Power BI service
tenant_id = '<your-tenant-id>'
client_id = '<your-client-id>'
client_secret = '<your-client-secret>'
credential = ClientSecretCredential(tenant_id, client_id, client_secret)
access_token = credential.get_token('https://analysis.windows.net/powerbi/api').token
headers = {'Authorization': f'Bearer {access_token}'}
# Get the dataset ID
dataset_name = '<your-dataset-name>'
response = requests.get('https://api.powerbi.com/v1.0/myorg/datasets', headers=headers)
datasets = response.json()['value']
dataset = next(d for d in datasets if d['name'] == dataset_name)
dataset_id = dataset['id']
# Add a partition
dataflow_name = '<your-dataflow-name>'
partition_key = '<your-partition-key>'
partition_query = '<your-partition-query>'
refresh_policy = models.DataflowRefreshPolicy('Daily', 1)
retention_policy = models.DataflowRetentionPolicy(365, True)
partition = models.DataflowPartition(partition_key, partition_query,
Note that this is just an example and you may need to modify it to fit your specific use case. Also, make sure to handle errors and exceptions appropriately in your code.
Yes, it is possible to manage partitions in Power BI datasets using Python. Here are the steps you can follow:
Install the Power BI API client for Python: You can use the "powerbiclient" library to interact with the Power BI REST API from Python. To install this library, run the following command in your Python environment:
pip install powerbiclient
Authenticate with the Power BI service: Before you can manage partitions, you need to authenticate with the Power BI service. You can do this by creating an Azure Active Directory (AAD) application and obtaining a client ID and client secret. You can then use these credentials to authenticate with the Power BI service and get an access token that can be used to make API calls.
Get the dataset ID: You need to know the ID of the Power BI dataset that you want to manage partitions for. You can get this ID by using the "get_datasets" API call and filtering the results by name.
Add a partition: To add a partition, you can use the "add_dataflow_partition" API call. This call requires the dataset ID, the name of the dataflow, and the partition key. You can also specify optional parameters such as the partition query, refresh policy, and retention policy.
Delete a partition: To delete a partition, you can use the "delete_dataflow_partition" API call. This call requires the dataset ID, the name of the dataflow, and the partition key.
Here is an example Python code snippet that shows how to add and delete partitions in a Power BI dataset:
from powerbiclient import Report, models
from azure.identity import ClientSecretCredential
import requests
# Authenticate with the Power BI service
tenant_id = '<your-tenant-id>'
client_id = '<your-client-id>'
client_secret = '<your-client-secret>'
credential = ClientSecretCredential(tenant_id, client_id, client_secret)
access_token = credential.get_token('https://analysis.windows.net/powerbi/api').token
headers = {'Authorization': f'Bearer {access_token}'}
# Get the dataset ID
dataset_name = '<your-dataset-name>'
response = requests.get('https://api.powerbi.com/v1.0/myorg/datasets', headers=headers)
datasets = response.json()['value']
dataset = next(d for d in datasets if d['name'] == dataset_name)
dataset_id = dataset['id']
# Add a partition
dataflow_name = '<your-dataflow-name>'
partition_key = '<your-partition-key>'
partition_query = '<your-partition-query>'
refresh_policy = models.DataflowRefreshPolicy('Daily', 1)
retention_policy = models.DataflowRetentionPolicy(365, True)
partition = models.DataflowPartition(partition_key, partition_query,
Note that this is just an example and you may need to modify it to fit your specific use case. Also, make sure to handle errors and exceptions appropriately in your code.
@abhiram342 , here powershell script has been used, I think you can use same with python