Update Table Schema

 

Updated: July 21, 2017



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

The Update Table Schema operation updates a Table schema. A Table schema can be updated in a Group.

Required scope: Dataset.ReadWrite.All

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

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.

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

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

PUT example

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

Header

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

Body schema

A Table schema has a collection of Columns. A Column has a name and data type.

{  
  "name": "table_name",  
  "columns": [  
    {  
      "name": "column_name1",  
      "dataType": "data_type"  
    },  
    {  
      "name": " column_name2",  
      "dataType": "data_type"  
    },  
    {  
    ...  
    }  
  ]  
}    

Body example

A product Table with Columns that describe a product.

{  
  "name": "Product",  
  "columns": [  
    {  
      "name": "ProductID",  
      "dataType": "Int64"  
    },  
    {  
      "name": "Name",  
      "dataType": "string"  
    },  
    {  
      "name": "Category",  
      "dataType": "string"  
    },  
    {  
      "name": "IsCompete",  
      "dataType": "bool"  
    },  
    {  
      "name": "ManufacturedOn",  
      "dataType": "DateTime"  
    },  
    {  
      "name": "NewColumn",  
      "dataType": "string"  
    }  
  ]  
}    

Status code

CodeDescription
201Created. The request was fulfilled and a new Dataset was created.

Content-Type

application/json

Body schema

Returns entity name that was updated.

{  
  "@odata.context":"http://api.powerbi.com/v1.0/myorg/$metadata#tables/$entity",  
  "name":"{table_name}"  
}  

Body Example

The following is an example of a Product entity that was updated.

{  
  "@odata.context":"http://api.powerbi.com/v1.0/myorg/$metadata#tables/$entity",  
  "name":"Product"  
}  


[Top of article]

The following C# example calls the Update Table Schema 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 UpdateTableSchema()  
    {  
        //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}", 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;  

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

        //Add a column to Product JSON content  
        string json = "{"name": "Product", "columns": " +  
            "[{ "name": "ProductID", "dataType": "Int64"}, " +  
            "{ "name": "Name", "dataType": "string"}, " +  
            "{ "name": "Category", "dataType": "string"}," +  
            "{ "name": "IsCompete", "dataType": "bool"}," +  
            "{ "name": "ManufacturedOn", "dataType": "DateTime"}," +  
            "{ "name": "NewColumn", "dataType": "string"}" +  
            "]}";  

        //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; }  
    }  

Show: