简体   繁体   English

如何在Power BI中使用SQL时态表

[英]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. 我正在构建一个Power BI报表,该报表使用一组SQL时态表作为源。 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 SQL通过使用允许查询的系统版本表来实现此目的,例如

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. 在Power BI中找不到有关如何使用时态表的任何文档。

What's the best way of doing this in power BI? 在Power BI中执行此操作的最佳方法是什么?

Thanks in advance for your help 在此先感谢您的帮助

I have just done this recently. 我最近刚做过。 @RADO is correct; @RADO是正确的; For import mode, you have rows for each date (or month, in your case) in a Power BI table. 对于导入模式,在Power BI表中每个日期(对于您而言,一个月)都有行。 All the work is in SQL. 所有工作都在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. 首先,我创建了一个内联(可)表值函数,该函数接受SYSTEM_TIME AS OF子句的datetime2并根据需要返回带有联接的所需结果集。

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. 您必须调整@utcTimeStamp,以便为您提供适合日期或月份含义的瞬间。

--- 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). 当我发现查询视图花费了很长时间时,我创建了一个缓存表和SQL Agent作业,以插入从最后一个日期到昨天的结果(这确保了所有时区的昨天结束)。 If you only need monthly results, you probably wouldn't need this step. 如果您只需要每月的结果,则可能不需要此步骤。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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