Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Earn the coveted Fabric Analytics Engineer certification. 100% off your exam for a limited time only!

Reply
bi-developer
New Member

Real-time dashboard using Web API

Hello!

Recently, I've struggling with real-time dashboards using API.

 

What I want: to push data (which changes frequently) into datasets. Create dashboards where that data is updated automatically.

 

Scenario:

 

There is open web api about bitcoins: https://api.cryptonator.com/api/ticker/btc-usd

 

I want that data which is displayed in that api to be display in a dashboard, so that it is always showing the newest data. Is it possible to achieve that somehow?

What I did:

1. GET https://api.cryptonator.com/api/ticker/btc-usd

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

 

C# Console application

 

I've already went through authorization - it is all good. I've also posted the data into power bi dataset, however, it is static and there is no current data displayed. Only the data once I get it from with GET.

 

Does anyone know how to make it work if it is possible? Or how to achieve real-time dashboards?

Btw, I've used google a lot, yet did not find a sample code (C# or smth) to achieve that. I would really appreaciate an assistant!

 

Thanks in advance!

 

 

using System;
using Microsoft.IdentityModel.Clients.ActiveDirectory;
using System.Net;
using System.IO;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Collections.Generic;

namespace walkthrough_push_data
{
    class Program
    {
        private static string token = string.Empty;

        static void Main(string[] args)
        {
            token = GetToken();

            CreateDataset();                  
            string datasetId = GetDataset();
AddRows(datasetId, "Bitcoin"); } #region Get an authentication access token private static string GetToken() { string clientID = "******************"; string redirectUri = "https://login.live.com/oauth20_desktop.srf"; string resourceUri = "https://analysis.windows.net/powerbi/api"; string authorityUri = "https://login.windows.net/common/oauth2/authorize"; AuthenticationContext authContext = new AuthenticationContext(authorityUri); string token = authContext.AcquireToken(resourceUri, clientID, new Uri(redirectUri)).AccessToken; Console.WriteLine(token); Console.ReadLine(); return token; } #endregion #region Create a dataset in Power BI private static void CreateDataset() { string powerBIDatasetsApiUrl = "https://api.powerbi.com/v1.0/myorg/datasets?defaultRetentionPolicy=basicFIFO"; HttpWebRequest request = WebRequest.Create(powerBIDatasetsApiUrl) as HttpWebRequest; request.KeepAlive = true; request.Method = "POST"; request.ContentLength = 0; request.ContentType = "application/json"; //Add token to the request header request.Headers.Add("Authorization", String.Format("Bearer {0}", token)); //Create dataset JSON for POST request string datasetJson = "{\"name\": \"Test2\", \"tables\": " + "[{\"name\": \"Bitcoin\", \"columns\": " + "[{ \"name\": \"base\", \"dataType\": \"string\"}, " + "{ \"name\": \"target\", \"dataType\": \"string\"}, " + "{ \"name\": \"price\", \"dataType\": \"string\"}," + "{ \"name\": \"volume\", \"dataType\": \"string\"}," + "{ \"name\": \"change\", \"dataType\": \"string\"}" + "]}]}"; //POST web request byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(datasetJson); request.ContentLength = byteArray.Length; using (Stream writer = request.GetRequestStream()) { writer.Write(byteArray, 0, byteArray.Length); var response = (HttpWebResponse)request.GetResponse(); Console.WriteLine(string.Format("Dataset {0}", response.StatusCode.ToString())); Console.ReadLine(); } } #endregion #region Get a dataset to add rows into a Power BI table private static string GetDataset() { string powerBIDatasetsApiUrl = "https://api.powerbi.com/v1.0/myorg/datasets"; HttpWebRequest request = WebRequest.Create(powerBIDatasetsApiUrl) as HttpWebRequest; request.KeepAlive = true; request.Method = "GET"; request.ContentLength = 0; request.ContentType = "application/json"; request.Headers.Add("Authorization", String.Format("Bearer {0}", token)); string datasetId = string.Empty; using (HttpWebResponse httpResponse = request.GetResponse() as HttpWebResponse) { using (StreamReader reader = new StreamReader(httpResponse.GetResponseStream())) { string responseContent = reader.ReadToEnd(); var results = JsonConvert.DeserializeObject<dynamic>(responseContent); datasetId = results["value"][0]["id"];
Console.WriteLine(String.Format("Dataset ID: {0}", datasetId)); Console.ReadLine(); return datasetId; } } } #endregion #region Add rows to a Power BI table private static string AddRows(string datasetId, string tableName) { string powerBIApiAddRowsUrl = String.Format("https://api.powerbi.com/v1.0/myorg/datasets/{0}/tables/{1}/rows", "**********************", tableName);
HttpWebRequest request = WebRequest.Create(powerBIApiAddRowsUrl) as HttpWebRequest; request.KeepAlive = true; request.Method = "POST"; request.ContentLength = 0; request.ContentType = "application/json"; request.Headers.Add("Authorization", String.Format("Bearer {0}", token)); string rowsJson = "{\"rows\":" + "[" + GetJson() + "]}"; byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(rowsJson); request.ContentLength = byteArray.Length; using (Stream writer = request.GetRequestStream()) { writer.Write(byteArray, 0, byteArray.Length); var response = (HttpWebResponse)request.GetResponse();
Console.WriteLine("Rows Added"); Console.ReadLine(); return response.ToString(); } } #endregion private static string GetJson() { string url = "https://api.cryptonator.com/api/ticker/btc-usd"; // url pabandymui HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest; request.KeepAlive = true; request.Method = "GET"; request.Accept = "application/json"; using (HttpWebResponse httpResponse = request.GetResponse() as HttpWebResponse) { using (StreamReader reader = new StreamReader(httpResponse.GetResponseStream())) { string responseContent = reader.ReadToEnd(); var results = JsonConvert.DeserializeObject<dynamic>(responseContent); var json = JsonConvert.SerializeObject(results.ticker); return json; } } } } }

 

4 REPLIES 4
TedPattison
Employee
Employee

You cannot create a real-time dashboard by making a single call into the Power BI Service API to create rows. Instead, you must call into the Power BI Service API any time the underlying data changes. This means you must find a way to periodcally trigger the code that retrieves the latest data and updates the dataset in Power BI

 

For example, you could create an Azure function with a schedule that executes once a minute. When this Azure function executes, it would call to the bitcoins API to get the latest data and then call to the Power BI Service API to push the changes into your dataset.

 

It's easier to create a realt-time dashboard if the API you are using to get the data provides some callback mechanism such as a webhook. If yoy can register a webhook, you don't have to periodically poll the API to detect changes because the webhook callback can trigger the code which gets the latest values and updates the real-time dataset in Power BI.

v-micsh-msft
Employee
Employee

Hi,

 

If you mean the real-time streaming in Power BI, then please check:

Real-time streaming in Power BI

 

Regards,

Michael

Hi,

 

thank you:)

Our aim is to create real-time streaming with push dataset. The the question is: how?

Thanks in advance!

Anonymous
Not applicable

I have same requirement. Did you find any solution?

Helpful resources

Announcements
April AMA free

Microsoft Fabric AMA Livestream

Join us Tuesday, April 09, 9:00 – 10:00 AM PST for a live, expert-led Q&A session on all things Microsoft Fabric!

March Fabric Community Update

Fabric Community Update - March 2024

Find out what's new and trending in the Fabric Community.