[spacer] [spacer] [spacer]

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

[mc4wp_form id="4946"] [spacer]

Azure Function to Push Rows in Power BI

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

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

Pre-Requirements to push rows in Power BI

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”: “”

Now, 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 Thoughts

In summary, the solution above is executed every second (around 2.592.000 times per month) and the ongoing cost will be only around $0.50 USD!

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 can help you during this process and work with high volumes of data from Azure such as:

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

In my previous post, I highlighted how easy it is to push rows with Jupyter Notebooks to a Power BI dataset achieving real-time data streaming.

For more tech talk, make sure you’re following me on Twitter at TechTalkCorner.

Check out some of my other posts below:

No Comments Yet.

Do you want to leave a comment?

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