Pushing Data into Power BI Preview Using the New REST API – Part 2

In my previous blog post, I described how to push some basic test data into Power BI Preview.  I have also uploaded the sample code to GitHub.  The previous article describes the basics for creating a dataset and adding rows – this article expands on this scenario by adding a Web API layer and allowing for pushing data from a AngularJS web site.

Pushing Data from a Public Web Site

Imagine a public web site with a set of voting buttons:

image

This little widget uses Bootstrap and AngularJS to manage the user interface.  In the backend, we created a basic ASP.NET Web API project to act as a receiver of the votes as triggered by the clicking of one of these buttons.

AngularJS is a great framework for integrating with ASP.NET Web API because both support JSON.  The AngularJS controller simply creates a vote entity, serializes it to JSON and sends it to a custom built vote controller.

public static void AddRows(Object myObject, EventArgs myEventArgs) { Random random = new Random(); double randomDouble = random.NextDouble() * 5; int randomInt = random.Next(1, 5); ArrayList rows = new ArrayList(); rows.Add(new TestRow() { TestColumnBool = true, TestColumnDateTime = DateTime.Now, TestColumnDouble = randomDouble, TestColumnInt = randomInt, TestColumnString = "test" }); PowerBI.AddRow(dataset, "testTable", rows); }

Authenticating with a Service Account

There are two ways we can get a token from Azure AD – we can redirect the user to a Microsoft authentication page and get them to login or we can supply a username and password as a service account.  We used the second option in this scenario to simulate an anonymous public web site experience.

NOTE: I have not put in anything to encrypt username and password in the sample code – this is something you would want to do in a production scenario in your web.config.

Processing the Incoming Data and Adding Rows

When the controller receives the JSON object, it authenticates using Azure AD with a preconfigured service account.  This allows us to authenticate within the Web API controller and and act as a proxy for public access.  The configuration parameters (username, password, datasetname) are stored in the web.config and/or the azure web site configuration when you publish the web site to Azure.

The controller uses the PowerBITransferService class to manage all the interactions with Power BI Preview – see the previous post for a more detailed explanation of what is going on under the covers.

For our scenario, we take the incoming vote and we add to the data by creating a VoteRow that has: 1) a vote count that = 1 (so we can count votes); 2) a vote date which is now; 3) a vote name (so we can group and filter by the name) and the value of the vote (e.g. from 1 as very unsatisfied to 5 as very satisfied).  The vote value allows us to create an average vote from 1 to 5 based on the rows provided.

Testing the Results

Here is a sample of the resulting Power BI Preview Dashboard.   This dashboard is updated in real time every time I click one of the voting buttons.

image