Create Dataset

 

Updated: August 24, 2017


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

The Create Dataset operation creates a new Dataset from a JSON schema definition and returns the Dataset ID and the properties of the dataset created. A Dataset can be created in a Group.

Required scope: Dataset.ReadWrite.All

To set the permissions scope, see Register an app.

POST https://api.powerbi.com/v1.0/myorg/datasets

Enable a default retention policy

POST https://api.powerbi.com/v1.0/myorg/datasets?defaultRetentionPolicy={None | basicFIFO}

Query string parameter

NameDescriptionValues
defaultRetentionPolicyEnables a default retention policy to automatically clean up old data while keeping a constant flow of new data going into your dashboard. To learn more about automatic retention policy, see Automatic retention policy for real-time data.None or basicFIFO

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

Uri parameter

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

Header

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

Body schema

A Dataset has a name and collection of Table objects (tables). A Table has a name and collection of Column objects (columns). A Column has a name and data type. For a list of Power BI REST data types, see Supported data types.

A Dataset also has a defaultMode property, which determines whether the dataset is pushed, streamed, or both, with values of "Push", "Streaming", "PushStreaming", "AzureAS" and "AsOnPrem". The default value is "Push".

Body Schema for Push, Streaming and PushStreaming

{"name": "dataset_name", 
    "defaultMode" : "PushStreaming",
    "tables":   
    [{"name": "", "columns":   
        [{ "name": "column_name1", "dataType": "data_type"},  
         { "name": "column_name2", "dataType": "data_type"},  
         { ... }  
        ]  
      }  
    ]
}

Body schema for Azure Analysis Services (AzureAS) and AsOnPrem

Body should have data sources in a datasources array. A data source has a name and connection string.

    {"name": "dataset_name", 
        "defaultMode": "AzureAS",
        "tables": [],
        "datasources":
        [{"name":"datasource1","connectionstring":"connection string"}]
        }

Body example

A SalesMarketing Dataset with a Product Table schema (tables). The product Table has columns that describe a product.

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

Here is an example for a dataset on Analysis Services on-premises.

{
   "name":"SalesMarketing",
   "defaultMode":"AsOnPrem",
   "tables": [],
   "datasources":[
      {
         "name":"SalesMarketing",
         "connectionString":"Data Source=asazure://westcentralus.asazure.windows.net/mypowerbi;Initial Catalog=SalesMarketing;Cube=Model"
      }
   ]
}

Here is an example for a dataset on Azure AS.

{
   "name":"SalesMarketing",
   "defaultMode":"AzureAS",
   "tables": [],
   "datasources":[
      {
         "name":"SalesMarketing",
         "connectionString":"Data Source=asazure://westcentralus.asazure.windows.net/mypowerbi;Initial Catalog=SalesMarketing;Cube=Model"
      }
   ]
}

Status code

CodeDescription
201Created. The request was fulfilled and a new Dataset was created.
202Accepted. The request was accepted and dataset creation is in progress.
400Bad request.
403Not authorized.

Location header

Created location header

A location header is returned and is used to see the created dataset details. By accessing the location, the user will get a dataset object.

Example: https://api.powerbi.com/v1.0/myorg/datasets('075de301-cfff-4808-a1a2-4ea749796695')

Accepted location header

A location header is returned and is used for pulling to check the creation status. By accessing the location, the user will get an imported package object.

Example: https://api.powerbi.com/v1.0/myorg/imports/51e47fc5-48fd-4826-89f0-021bd3a80abd

Content-Type

application/json

Body schema

A Create Dataset response has the following schema:

{            
    "@odata.context":"https://api.powerbi.com/v1.0/myorg/$metadata#datasets/$entity",         
    "id":"{dataset_id}",  
    "name":"{name}",  
    "defaultRetentionPolicy":"{None | basicFIFO}"  
}  

Body example

The following is an example of a SalesMarketing Dataset JSON response with defaultRetentionPolicy set to basicFIFO. The Retention Policy automatically cleans up old data while keeping a constant flow of new data going into your dashboard.

{  
    "@odata.context":"https://api.powerbi.com/v1.0/myorg/$metadata#datasets/$entity",  
    "id":"7c0b090e--b51-172874c749e0",  
    "name":"SalesMarketing",  
    "defaultRetentionPolicy":"basicFIFO"  
}  


[Top of article]

The following C# example calls the Create Dataset operation.

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.

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

    static string CreateDataset()  
    {  
        //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}";  

        //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 = "https://api.powerbi.com/v1.0/myorg";  

        //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)).AccessToken;  

        //POST web request to create a dataset.  
        //To create a Dataset in a group, use the Groups uri: https://api.powerbi.com/v1.0/myorg/groups/{group_id}/datasets  
        HttpWebRequest request = System.Net.WebRequest.Create(string.Format("{0}/datasets", 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));  

        //Create dataset JSON for POST request  
        string json = "{"name": "SalesMarketing", "tables": " +  
            "[{"name": "Product", "columns": " +  
            "[{ "name": "ProductID", "dataType": "Int64"}, " +  
            "{ "name": "Name", "dataType": "string"}, " +  
            "{ "name": "Category", "dataType": "string"}," +  
            "{ "name": "IsCompete", "dataType": "bool"}," +  
            "{ "name": "ManufacturedOn", "dataType": "DateTime"}" +  
            "]}]}";  

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

            var response = (HttpWebResponse)request.GetResponse();  

            return response.StatusCode.ToString();  
        }  
    }  

More questions? Try the Power BI Community

Show: