Sign up with your email address to be the first to know about new publications

Azure Function to push rows in Power BI

Ingesting IoT  information has become extremely important in the data analytics space and with the Azure offering it is quite accessible as well. In this post, we will push rows to a Power BI Service dataset using Azure Functions v2 and c#.

We will create an Azure Function with Visual Studio 2017 (you can copy the code to Visual Studio Code or directly in the Azure portal) and deploy it to Azure so we don’t need any type of On-Premises workstation or Virtual Machine to continue to stream data on time basis.

Pre-Requirements:

Copy and paste the following code into the .cs file:

using System;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Host;
using System.Net.Http;
using System.Text;
using Newtonsoft.Json.Linq;


namespace PowerBIBitcoinPrice
{
 public static class PowerBIRealTime
 {
 [FunctionName("PowerBIBitcoinPrice")]
 /* Time Trigger function * * * * * * = {second} {minute} {hour} {day} {month} {day-of-week} */
 public static async System.Threading.Tasks.Task RunAsync([TimerTrigger("* * * * * *")]TimerInfo myTimer, TraceWriter log)
 {
 //Define http client to get the Bitcoin price from the Binance API
 HttpClient client = new HttpClient();
 HttpResponseMessage response = await client.GetAsync("https://api.binance.com/api/v3/ticker/price?symbol=BTCUSDT");

 //Parse to Json
 JObject jsonresponse = JObject.Parse(await response.Content.ReadAsStringAsync());

 //Using Parameter in local.setting.json -- URL can be define directly in the following variable
 String PowerBIDataSetUrl = Environment.GetEnvironmentVariable("PowerBIDataSetURL");

 //Define Json body for Power BI
 JObject PowerBIRow =
 new JObject(
 new JProperty("Timestamp", DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ss.fffffff")),
 new JProperty("Price", (string)jsonresponse["price"])
 );
 
 //Generate content for http post request
 var content = new StringContent(PowerBIRow.ToString(), Encoding.UTF8, "application/json"); 

 //Define http client to push the row
 HttpClient powerbiclient = new HttpClient();
 HttpResponseMessage pushrow = await powerbiclient.PostAsync(PowerBIDataSetUrl, content);
 

 }


 }
}

Note: Embed the dataset url in the code above or in your local.setting.json file add the following parameter:

“PowerBIDataSetURL”: “”

You are ready to publish the Azure Function!

If you choose the add the parameter, you will need to add it to the Azure Function application setting as well:

Download the source code from this link.

Final notes 

The solution above will be executed every second (around 2.592.000 time per month) and the ongoing cost will be only around $0.50!

When you are working with real-time information, don’t forget to persist the information in a data repository for further batch and historical analysis. There are many tools that will help you during this process and work with high volumes of data such as:

  • Azure Event Hubs
  • Azure IoT
  • Azure Stream Analytics
  • Azure Databricks
  • Azure HDInsight Kafka

 

In my previous post, I highlighted how to easily push rows with Jupyter Notebooks to a Power BI dataset achieving real time data streaming, click here for more information.

No Comments Yet.

Do you want to leave a comment?

Your email address will not be published. Required fields are marked *