简体   繁体   中英

How to use SQL temporal tables in Power BI

I am building a Power BI report that uses a set of SQL temporal tables as source. I would like to provide the user the opportunity of selecting a month from a date slicer and based on that selection, the report would show the valid data AS OF that date.

SQL allows this by using system versioned tables that allow queries such as

SELECT * from table FOR SYSTEM_TIME BETWEEN 'date1' and 'date2'

which would give back all valid values in the table as if the query was run between those dates.

I couldn't find any documentation on how to work with temporal tables in Power BI.

What's the best way of doing this in power BI?

Thanks in advance for your help

I have just done this recently. @RADO is correct; For import mode, you have rows for each date (or month, in your case) in a Power BI table. All the work is in SQL.

First, I created a inline(-able) table-valued function that accepts a datetime2 for the SYSTEM_TIME AS OF clause and returns the desired resultset with in joins as needed.

SELECT 
    header.*, 
    detail.*
FROM header FOR SYSTEM_TIME AS OF @utcTimeStamp AS header
LEFT JOIN detail FOR SYSTEM_TIME AS OF @utcTimeStamp AS detail ON detail.header_id = header.id ;

You would have to adjust @utcTimeStamp so it gives the appropriate instant for your meaning of the date or the month.

--- Ajust to match your time period datatype
DECLARE @utcTimeStamp datetime2(3) = DATEADD(ms, -1, DATEADD(DAY, 1, CAST(@date AS datetime2(3)))) AT TIME ZONE 'Eastern Standard Time' AT TIME ZONE 'UTC';

Then, I created a view to cross apply the function against the dates needed.

When I found that it took a long time to query the view, I created a caching table and SQL Agent job to insert results from the last date through yesterday (at a time that ensures yesterday is over in all of our timezones). If you only need monthly results, you probably wouldn't need this step.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM