Add Rows

 

Updated: July 21, 2017



Request | Response | Example | Try on Apiary | Client and web app samples

The Add Rows operation adds Rows to a Table in a Dataset or a Dataset in a Group.

REST API Limitations: See Power BI REST API limitations.

Required scope: Dataset.ReadWrite.All

POST https://api.powerbi.com/v1.0/myorg/datasets/{dataset_id}/tables/{table_name}/rows

Groups

Groups are a collection of unified Azure Active Directory groups that the user is a member of and is available in the Power BI service. These are referred to as app workspaces within the Power BI service. To learn how to create a group, see Create an app workspace.

POST https://api.powerbi.com/v1.0/myorg/groups/{group_id}/datasets/{dataset_id}/tables/{table_name}/rows

Uri parameters

NameDescriptionData Type
group_idGuid of the Group to use. You can get the group id from the Get Groups operation. Groups are referred to as app workspaces within the Power BI service.String
dataset_idGuid of the Dataset to use. You can get the dataset id from the Get Datasets operation.String
table_nameName of Table in the Dataset.String

POST example

https://api.powerbi.com/v1.0/myorg/datasets/{dataset_id}/tables/Product/rows

Header

Content-Type: application/json
Authorization: Bearer eyJ0eX ... FWSXfwtQ

Body schema

Rows have a collection of Column names and values.

{"rows":  
    [  
        {"column_name1":value, "column_name2": value, "column_name3":value, ...}  
    ]  
}  

Body example

Rows for a product schema.

{"rows":  
    [  
        {"ProductID":1,"Name":"Adjustable Race","Category":"Components","IsCompete":true,"ManufacturedOn":"07/30/2014"},  
        {"ProductID":2,"Name":"LL Crankarm","Category":"Components","IsCompete":true,"ManufacturedOn":"07/30/2014"},  
        {"ProductID":3,"Name":"HL Mountain Frame - Silver","Category":"Bikes","IsCompete":true,"ManufacturedOn":"07/30/2014"}  
    ]  
}  

Status code

CodeDescription
200OK. Indicates success. The dataset is returned in the response body.

Content-Type

application/json

Body

None


[Top of article]

The following C# example calls the Add Rows operation. The example assumes you have a dataset named SalesMarketing with a Product table. To create a dataset, see Create Dataset operation. The example also shows how to:

  • Get a Dataset id using a LINQ query. To get a dataset id, see Get Datasets.

Note The Client app sample also shows you how to Serialize and Deserialize a JSON request and response as well as call Group operations.

To run this code snippet, you need to:

s1 - Have an Azure Active Directory tenant.

s2 - Sign up for Power BI.

s3 - Register your app to get a client id.

s4 - You will need a Dataset id. To get a dataset id, see Get Datasets operation.

    using System;  
    using System.Net;  
    using Microsoft.IdentityModel.Clients.ActiveDirectory;  
    using System.IO;  
    using System.Web.Script.Serialization;  
    using System.Linq;  

    public void AddRows()  
    {  
        //This is sample code to illustrate a Power BI operation.   
        //In a production application, refactor code into specific methods and use appropriate exception handling.  

        //The client id that Azure AD creates when you register your client app.  
        //  To learn how to register a client app, see https://msdn.microsoft.com/en-US/library/dn877542(Azure.100).aspx    
        string clientID = "{client id from Azure AD app registration}";  

        //Assuming you have a dataset named SalesMarketing  
        // To get a dataset id, see Get Datasets operation.             
        dataset[] datasets = GetDatasets();  
        string datasetId = (from d in datasets where d.Name == "SalesMarketing" select d).FirstOrDefault().Id;  

        // Assumes the Dataset named SalesMarketing has a Table named Product  
        string tableName = "Product";  

        //RedirectUri you used when you register your app.  
        //For a client app, a redirect uri gives Azure AD more details on the application that it will authenticate.  
        // You can use this redirect uri for your client app  
        string redirectUri = "https://login.live.com/oauth20_desktop.srf";  

        //Resource Uri for Power BI API  
        string resourceUri = "https://analysis.windows.net/powerbi/api";  

        //OAuth2 authority Uri  
        string authorityUri = "https://login.windows.net/common/oauth2/authorize";  

        string powerBIApiUrl = String.Format("https://api.powerbi.com/v1.0/myorg/datasets/{0}/tables/{1}/rows", datasetId, tableName);  

        //Get access token:   
        // To call a Power BI REST operation, create an instance of AuthenticationContext and call AcquireToken  
        // AuthenticationContext is part of the Active Directory Authentication Library NuGet package  
        // To install the Active Directory Authentication Library NuGet package in Visual Studio,   
        //  run "Install-Package Microsoft.IdentityModel.Clients.ActiveDirectory" from the nuget Package Manager Console.  

        // AcquireToken will acquire an Azure access token  
        // Call AcquireToken to get an Azure token from Azure Active Directory token issuance endpoint  
        AuthenticationContext authContext = new AuthenticationContext(authorityUri);  
        string token = authContext.AcquireToken(resourceUri, clientId, new Uri(redirectUri), PromptBehavior.RefreshSession).AccessToken;  

        //POST web request to add rows.  
        //To add rows to a dataset in a group, use the Groups uri: https://api.powerbi.com/v1.0/myorg/groups/{group_id}/datasets/{dataset_id}/tables/{table_name}/rows  
        HttpWebRequest request = System.Net.WebRequest.Create(powerBIApiUrl) as System.Net.HttpWebRequest;  
        request.KeepAlive = true;  
        request.Method = "POST";  
        request.ContentLength = 0;  
        request.ContentType = "application/json";  
        request.Headers.Add("Authorization", String.Format("Bearer {0}", token));  

        //JSON content for product row  
        string json = "{"rows":" +  
            "[{"ProductID":1,"Name":"Adjustable Race","Category":"Components","IsCompete":true,"ManufacturedOn":"07/30/2014"}," +  
            "{"ProductID":2,"Name":"LL Crankarm","Category":"Components","IsCompete":true,"ManufacturedOn":"07/30/2014"}," +  
            "{"ProductID":3,"Name":"HL Mountain Frame - Silver","Category":"Bikes","IsCompete":true,"ManufacturedOn":"07/30/2014"}]}";  

        //POST web request  
        byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(json);  
        request.ContentLength = byteArray.Length;  

        //Write JSON byte[] into a Stream  
        using (Stream writer = request.GetRequestStream())  
        {  
            writer.Write(byteArray, 0, byteArray.Length);  
        }  
    }  
      
    public class Datasets  
    {  
        public dataset[] value { get; set; }  
    }  

    public class dataset  
    {  
        public string Id { get; set; }  
        public string Name { get; set; }  
    }  

    public class Tables  
    {  
        public table[] value { get; set; }  
    }  

    public class table  
    {  
        public string Name { get; set; }  
    }  

    public class Groups  
    {  
        public group[] value { get; set; }  
    }  

    public class group  
    {  
        public string Id { get; set; }  
        public string Name { get; set; }  
    }               

Sequence numbers guarantee which rows have been successfully pushed.

When pushing rows to a table in Power BI, you'll receive a 200 OK response when the rows are successfully loaded to Power BI, and error status codes when they are not. However, what about those rare cases when you don’t receive a response at all due to some network or timeout issue? Should you push the data again? Before now, there was no way to know. Now, with sequence numbers, you always know what state your data is in. Sequence numbers are assigned to a set of rows that you are pushing. If you're unsure if the previous call succeeded, send with the same Sequence Number and we’ll determine if the call is a duplicate or not.

You can take advantage of sequence numbers by adding the X-PowerBI-PushData-SequenceNumber header to your POST rows call:

Sequence numbers header

This header takes a 64-bit integer, which you should increase in value each time you post new rows. Power BI will save the most recent sequence number with your data. As long as each of your POST rows requests have a higher sequence number than what is stored with the data, the request will succeed. If you're unsure if your request succeeded or not, you should resend it with the same sequence number as the original request. If the original request had succeeded, the second request would fail with a 412 Precondition failed error, like this:

Sequence numbers error

Then you can move on to the next set of data with a new sequence number. If you need to discover the current sequence number which is stored with your table data, you can make the following API call on the table:

Sequence numbers api

The response body will look something like this:

squence_numbers_response

You can try this API now at Apiary.

The above approach works great if you have a single device or client pushing to a table in Power BI. However, if you have multiple devices or clients pushing to the same table at the same time you can add the X-PowerBI-PushData-ClientId header and a client ID to maintain a separate sequence number for each client. Use of this header is optional.

Sequence numbers are support in both the v1 and beta versions of the API.

To try this or any of the other APIs mentioned in this article, be sure to check out Power BI API on Apiary.

Show: