Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
5 years ago
Solved

power BI rest API with python

I am new to power BI rest APIs need some help on power BI APIs with python.

I have a requirement to use power BI rest API with python script.

As part of the requirement I have to create a new dataset using API calls from python and then post data from a json/csv file into the newly created dataset using API calls from python. 

Can someone please share some documentation/threads around these topics

 

I can create a dataset with help of python and rest API calls but can't insert data from a json/csv file into newly created dataset is 

Below is the code for creating a dataset

 

import adal
import pandas as pd
import requests
import json
from pypowerbi.dataset import Column, Table, Dataset
from pypowerbi.client import PowerBIClient

# you might need to change these, but i doubt it 

# change these to your credentials
client_id = 'XXXXXXXXXXXXXXXXXXXXX' 
username = 'XXXXXXXXXXXXXXXXXXXXXXXXX'
password = 'XXXXXXXXXXXXXXX'

# Authenticate using adal
context = adal.AuthenticationContext(authority=authority_url,
                                     validate_authority=True,
                                     api_version=None)

# get your authentication token
token = context.acquire_token_with_username_password(resource=resource_url,
                                                     client_id=client_id,
                                                     username=username,
                                                     password=password)

# create your powerbi api client
client = PowerBIClient.get_client_with_username_password(client_id=client_id, username=username, password=password)

# create your columns
columns = []
columns.append(Column(name='CHNG', data_type='Int64'))
columns.append(Column(name='CUMULATIVE', data_type='Int64'))
columns.append(Column(name='S', data_type='Int64'))
columns.append(Column(name='VALUE', data_type='Int64'))
columns.append(Column(name='COUNTY', data_type='string'))
columns.append(Column(name='METRIC', data_type='string'))
columns.append(Column(name='SESSIONID', data_type='datetime'))

# create your tables
tables = []
tables.append(Table(name='contacts_daily_chng', columns=columns))

# create your dataset
dataset = Dataset(name='apidemo2', tables=tables)

# post your dataset!
client.datasets.post_dataset(dataset)

print(dataset.name)

4 Replies

    • Anonymous's avatar
      Anonymous
      Not applicable

      Thanks for your reply!

       

      This is my actual requirement

      1. Creating an empty dataset using PBI API calls with python

      2. Inseting some data from any file type like (json, csv, text etc..) into the newly created dataset with help of PBI API calls and python

      3. Append the existing data with help of PBI API calls and python

       

      I did all above steps with help of postman, and now we are trying to implement the same with help of python.

       

      I did see the video you shared, I can create a table using python script from power BI desktop. But my requirement is using API calls need to create datasets and Insert data. 

  • Anonymous's avatar
    Anonymous
    Not applicable

    Given information is very informative.

     

    Could you please also suggest if the same approach can be used to GET data from PowerBI REST API and in this steps i need to load GET response into JSON or db table.

     

    Appreciate your response in advance