PowerBI RS: Setting Data Refresh in Trigger Mode | by Davis Zhang | Towards Data Science

Using the Power BI Report Server database can do a lot of things you can’t do in Power BI Services, one of them is to set a triggered data refresh on the report data set.
As the name implies, it is to trigger the dataset of your PBI reports to refresh with events. For example, when the data source referenced by the report has changed (added, changed, or deleted), then the trigger condition is activated to allow the PBI report to refresh the data immediately and keep the PBI report data up to date. The typical application process is as follows:
In PBIRS, there are two main types of data refresh for PBI reports: scheduled refresh and DirectQuery. DirectQuery enables users to continuously obtain the latest data every time they use a report, but the disadvantage is that when the report query logic is complex, it may seriously affect the performance of the report. Besides, some DAX functions are not supported by the calculation engine (such as time intelligence functions). While in scheduled refresh mode, theoretically, your report performance will not be affected by the refresh behavior, and you can use any DAX function that supports PowerBI, that’s great, however, you cannot guarantee that users will get the latest data when they using reports.
Therefore, this has become a very embarrassing fact. Imagine that you need to develop a report, which requires the use of DAX time intelligence functions (instead of SQL), and you must ensure that users can see the latest data in time when they using it. So how do you choose the refresh mode for your dataset? Maybe you will set your report to refresh every one minute, but if your report has more data, it will not only increase the probability of data refresh failure but also cause a certain burden on the server. Therefore, data refresh in trigger mode has become the best solution. It combines the advantages of both DirectQuery and scheduled refresh. Data refresh will only be performed when the data has changed. This can not only ensure that users can see the latest data when using reports but also reduce the burden on the server without affecting report performance. That’s why you should refresh your report data in trigger mode.
First, I create a table in the database as an example. As shown below, I created a new table, inserting three rows of data-the three great generals of Ancient China in the Warring States period (475–221 BC): Bai Qi, Lian Po, and Wang Jian, simple like this:
CREATE TABLE [TriggerRefreshTest]([No_] int,[Name] nvarchar(10),[Country] nvarchar(20))Insert Into [TriggerRefreshTest]values(1, 'BaiQi', 'The Kingdom of Qin'),(2, 'LianPo', 'The Kingdom of Zhao'),(3, 'WangJian', 'The Kingdom of Qin')
Import the data into Power BI Desktop and publish it as follows:
The effect we want to achieve now is when I insert a new row of data in the database-(Li Mu, The Kingdom of Zhao), and then immediately trigger a data refresh event to keep the report data up to date. In SSMS, create a new trigger event, as shown below:
Then we need to write a trigger script with SQL. It’s OK for readers who haven’t learned SQL. You can directly use the following code. I have simplified and optimized the script. You only need to replace the following code (where I have annotated) with your report information:
SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOIF OBJECT_ID(    N'trigger_data_refresh') is not nullDROP TRIGGER trigger_data_refresh;GOCREATE TRIGGER trigger_data_refresh ON [TriggerRefreshTest]  --Replace with your table name hereAFTER INSERTAS SET NOCOUNT ON;DECLARE @REPORT_NAME NVARCHAR(    50),@REPORT_ID VARCHAR(    100),@SUBSCRIPTION_ID VARCHAR(    100)SET @REPORT_NAME = 'WarringStates' --Replace with your PBI report name.SET @REPORT_ID = (    SELECT TOP 1 [ItemID]    FROM [ReportServer].[dbo].[Catalog]    WHERE [Name] = @REPORT_NAME)SET @SUBSCRIPTION_ID = (    SELECT TOP 1 SubscriptionID    FROM [ReportServer].[dbo].[ReportSchedule]    WHERE [ReportID] = @REPORT_ID)BEGINWAITFOR DELAY '0:0:3'exec [ReportServer].dbo.AddEvent @EventType='DataModelRefresh',@EventData=@SUBSCRIPTION_IDENDGO
Finally, run the code, so our trigger refresh is now set up. Let’s insert a new row into the table — [TriggerRefreshTest]:
Insert Into [TriggerRefreshTest]values (4, 'LiMu', 'The Kingdom of Zhao')
Now our trigger mechanism has detected data changes, so data refresh event has been activated. Return to the report page of Power BI RS, click “Refresh” to refresh the cache, as you can see, the data just inserted is immediately shown in the report!
If the data source table is maintained by multiple users, then they may modify the data at the same time, which may cause problems (such as table locking). I think one of the solutions is to force a minimum refresh interval. This can be achieved by modifying stored procedures.
End~
(This article was first published in Chinese on D-BI in DEC, 2019)