Integrate Power BI into Your Application: Part 4 – Refresh Data Set

2017-06-29_23h16_09

Power BI REST API is not just for embedding content or getting list of dashboards and reports. It also has many functions to work with data sets, gateways, and data sources. As one of the many exciting features of that; you can easily refresh a data set from an application. you can refresh it as many as times you want. with any frequency you want. You can refresh your data set after ETL run through a console application. or you can have a service application that kick of refresh even every single minute! There is no limitation for your data refresh anymore! Let’s check it out. if you like to learn more about Power BI; read Power BI book; from Rookie to Rock Star.

Step 1: Register your Application

Step 2: Authenticate

Sample Code

The sample code for this example can be downloaded from here.

Step 3: Get Data Set

With Power BI REST API version 2, you can access many objects from Power BI service. One of these object types is data set. You can get list of data sets, and you can then apply some operations on it, such as refresh. After authenticating, code below gets list of data sets;

var datasets = await client.Datasets.GetDatasetsInGroupAsync(GroupId);

GetDatasetsInGroupAsync simply gives you a list of all data sets, and you can then iterate through them. As I’ve mentioned before, group id should be the guid of the group. You can then find the particular data set you want. code below just gets the very first data set in the list:

var datasets = await client.Datasets.GetDatasetsInGroupAsync(GroupId);

                var dataset = datasets.Value.FirstOrDefault();
                
                if (dataset == null)
                {
                    return View(new EmbedConfig()
                    {
                        ErrorMessage = "Group has no datasets."
                    });
                }

This data set is already in a group in my Power BI account;

2017-06-29_22h57_36

As you can see the data set above, is named Pubs 20170529, and it refreshed at 21st of June 2017 as the last time.

Now with running the code, I can easily find this data set;

2017-06-29_22h59_59

After finding the data set, you can easily refresh it.

Refresh Data Set

You can even directly refresh a data set, you just need Id of that data set. one way to find the data set ID is through application. another way is from URL of power bi service. If you go to setting of that data set, you can find the ID of it easily;

2017-06-29_23h02_11

the code to refresh a data set is very simple:

object result= await client.Datasets.RefreshDatasetInGroupAsync(GroupId, dataset.Id);

As a result of refresh, then you can see the data set is refreshed successfully;

2017-06-29_23h07_13

Last refresh time is now 29th of June 2017, 11pm, which is the time of writing this blog post 😉

Imporatnt note to consider here is that this data set is an IMPORT DATA data set, and it is working through gateway. so I need the gateway to be up and running. However, even if you are connected to a cloud data source through import data, then even without gateway you can refresh the data set. so, any data set can be easily refreshed. the fascinating fact is:

You can refresh your data set from an application. Any time you like, at any frequency you like. After ETL run, or on a schedule basis. This is Refreshing data beyond limits.

Refresh History

The new REST API is an evolution in Power BI integration. You can even read the history of refresh for a data set. Here is the code for it:

var history = await client.Datasets.GetRefreshHistoryInGroupAsync(GroupId, dataset.Id);

Running this code will return the whole history of refresh for that data set. You can easily understand the type of refresh, and time and status of each.

2017-06-29_23h16_09

Same refresh history in Power BI service:

2017-06-29_23h18_16

This means you can easily write an application that refresh the data set anytime, and also check the history of refresh for troubleshooting.

In the next blog post I’ll explain some of other great features of Power BI REST API, such as managing gateways and data sources.

Reza Rad on FacebookReza Rad on LinkedinReza Rad on TwitterReza Rad on Youtube
Reza Rad
Trainer, Consultant, Mentor
Reza Rad is a Microsoft Regional Director, an Author, Trainer, Speaker and Consultant. He has a BSc in Computer engineering; he has more than 20 years’ experience in data analysis, BI, databases, programming, and development mostly on Microsoft technologies. He is a Microsoft Data Platform MVP for 12 continuous years (from 2011 till now) for his dedication in Microsoft BI. Reza is an active blogger and co-founder of RADACAD. Reza is also co-founder and co-organizer of Difinity conference in New Zealand, Power BI Summit, and Data Insight Summit.
Reza is author of more than 14 books on Microsoft Business Intelligence, most of these books are published under Power BI category. Among these are books such as Power BI DAX Simplified, Pro Power BI Architecture, Power BI from Rookie to Rock Star, Power Query books series, Row-Level Security in Power BI and etc.
He is an International Speaker in Microsoft Ignite, Microsoft Business Applications Summit, Data Insight Summit, PASS Summit, SQL Saturday and SQL user groups. And He is a Microsoft Certified Trainer.
Reza’s passion is to help you find the best data solution, he is Data enthusiast.
His articles on different aspects of technologies, especially on MS BI, can be found on his blog: https://radacad.com/blog.

4 thoughts on “Integrate Power BI into Your Application: Part 4 – Refresh Data Set

  • Hi,
    thanks for your tips.
    i have a question, what would be the group id if it is myworkspace ?

    • Hi,
      Group Id is the unique identifier for the group. when you browse that group (work space) in power BI in the url you will see that.
      Cheers
      Reza

  • Hi,
    GetRefreshHistoryInGroupAsync give me bad request error randomly …I think there is restriction with the refresh 8 times for pro user. Please reply urgently?
    Regards,
    Swapana

Leave a Reply