我正在使用DirectQuery针对具有销售表和日期表的SQL服务器运行Power BI仪表板,它们使用假定参照完整性在PBI数据模型中连接以确保内部联接。在这个模型中,我想使用时间维度,如MTD,YTD,R12M。
R12M的计算量度
R12M =
VAR
V_START_DATE =
NEXTDAY(SAMEPERIODLASTYEAR(LASTDATE(datetable[calendar_date])))
VAR
V_END_DATE = LASTDATE(datetable[calendar_date])
RETURN
CALCULATE(SUM(v_geo_tool_sales[sales_volume]);FILTER(ALL(datetable[calendar_date
]);datetable[calendar_date]>=V_START_DATE&&datetable[calendar_date]<=V_END_DATE))
PBI生成的SQL
SELECT SUM([t0].[sales_volume])
AS [a0]
FROM
(
(select [$Table].[sold_to] as [sold_to],
[$Table].[material] as [material],
[$Table].[calendar_date] as [calendar_date],
[$Table].[sales_volume] as [sales_volume]
from [dbo].[v_geo_tool_sales] as [$Table])
)
AS [t0]
WHERE
(
([t0].[calendar_date] IN (CAST( N'2017-01-18 00:00:00' AS datetime),CAST( N'2016-12-12 00:00:00' AS datetime),CAST( N'2017-04-25 00:00:00' AS datetime),CAST( N'2017-09-23 00:00:00' AS datetime),CAST( N'2016-11-05 00:00:00' AS datetime),CAST( N'2017-04-05 00:00:00' AS datetime),CAST( N'2017-08-17 00:00:00' AS datetime),CAST( N'2017-02-27 00:00:00' AS datetime),CAST( N'2017-07-11 00:00:00' AS datetime),CAST( N'2017-01-21 00:00:00' AS datetime),CAST( N'2017-06-21 00:00:00' AS datetime),CAST( N'2016-12-15 00:00:00' AS datetime),CAST( N'2017-05-15 00:00:00' AS datetime),CAST( N'2017-09-26 00:00:00' AS datetime),CAST( N'2016-11-25 00:00:00' AS datetime),CAST( N'2017-04-08 00:00:00' AS datetime),CAST( N'2017-09-06 00:00:00' AS datetime),CAST( N'2016-10-19 00:00:00' AS datetime),CAST( N'2017-03-02 00:00:00' AS datetime),CAST( N'2017-03-19 00:00:00' AS datetime),CAST( N'2017-07-31 00:00:00' AS datetime),CAST( N'2017-02-10 00:00:00' AS datetime),CAST( N'2017-06-24 00:00:00' AS datetime),CAST( N'2017-01-04 00:00:00' AS datetime),CAST( N'2017-05-18 00:00:00' AS datetime),CAST( N'2017-06-04 00:00:00' AS datetime),CAST( N'2016-11-28 00:00:00' AS datetime),CAST( N'2017-04-28 00:00:00' AS datetime),CAST( N'2017-09-09 00:00:00' AS datetime),CAST( N'2016-10-22 00:00:00' AS datetime),CAST( N'2016-11-08 00:00:00' AS datetime),CAST( N'2017-03-22 00:00:00' AS datetime),CAST( N'2017-08-20 00:00:00' AS datetime),CAST( N'2016-10-02 00:00:00' AS datetime),CAST( N'2017-02-13 00:00:00' AS datetime),CAST( N'2017-07-14 00:00:00' AS datetime),CAST( N'2017-01-24 00:00:00' AS datetime),CAST( N'2017-06-07 00:00:00' AS datetime),CAST( N'2016-12-18 00:00:00' AS datetime),CAST( N'2017-05-01 00:00:00' AS datetime),CAST( N'2017-09-29 00:00:00' AS datetime),CAST( N'2016-11-11 00:00:00' AS datetime),CAST( N'2017-04-11 00:00:00' AS datetime),CAST( N'2017-08-23 00:00:00' AS datetime),CAST( N'2016-10-05 00:00:00' AS datetime),CAST( N'2017-03-05 00:00:00' AS datetime),CAST( N'2017-07-17 00:00:00' AS datetime),CAST( N'2017-08-03 00:00:00' AS datetime),CAST( N'2017-01-27 00:00:00' AS datetime),CAST( N'2017-06-27 00:00:00' AS datetime),CAST( N'2016-12-21 00:00:00' AS datetime),CAST( N'2017-01-07 00:00:00' AS datetime),CAST( N'2017-05-21 00:00:00' AS datetime),CAST( N'2016-12-01 00:00:00' AS datetime),CAST( N'2017-04-14 00:00:00' AS datetime),CAST( N'2017-09-12 00:00:00' AS datetime),CAST( N'2016-10-25 00:00:00' AS datetime),CAST( N'2017-03-25 00:00:00' AS datetime),CAST( N'2017-08-06 00:00:00' AS datetime),CAST( N'2017-02-16 00:00:00' AS datetime),CAST( N'2017-06-30 00:00:00' AS datetime),CAST( N'2017-01-10 00:00:00' AS datetime),CAST( N'2017-06-10 00:00:00' AS datetime),CAST( N'2016-12-04 00:00:00' AS datetime),CAST( N'2017-05-04 00:00:00' AS datetime),CAST( N'2017-09-15 00:00:00' AS datetime),CAST( N'2016-11-14 00:00:00' AS datetime),CAST( N'2017-03-28 00:00:00' AS datetime),CAST( N'2017-08-26 00:00:00' AS datetime),CAST( N'2016-10-08 00:00:00' AS datetime),CAST( N'2017-02-19 00:00:00' AS datetime),CAST( N'2017-03-08 00:00:00' AS datetime),CAST( N'2017-07-20 00:00:00' AS datetime),CAST( N'2017-01-30 00:00:00' AS datetime),CAST( N'2017-06-13 00:00:00' AS datetime),CAST( N'2016-12-24 00:00:00' AS datetime),CAST( N'2017-05-07 00:00:00' AS datetime),CAST( N'2017-05-24 00:00:00' AS datetime),CAST( N'2016-11-17 00:00:00' AS datetime),CAST( N'2017-04-17 00:00:00' AS datetime),CAST( N'2017-08-29 00:00:00' AS datetime),CAST( N'2016-10-11 00:00:00' AS datetime),CAST( N'2016-10-28 00:00:00' AS datetime),CAST( N'2017-03-11 00:00:00' AS datetime),CAST( N'2017-08-09 00:00:00' AS datetime),CAST( N'2017-02-02 00:00:00' AS datetime),CAST( N'2017-07-03 00:00:00' AS datetime),CAST( N'2017-01-13 00:00:00' AS datetime),CAST( N'2017-05-27 00:00:00' AS datetime),CAST( N'2016-12-07 00:00:00' AS datetime),CAST( N'2017-04-20 00:00:00' AS datetime),CAST( N'2017-09-18 00:00:00' AS datetime),CAST( N'2016-10-31 00:00:00' AS datetime),CAST( N'2017-03-31 00:00:00' AS datetime),CAST( N'2017-08-12 00:00:00' AS datetime),CAST( N'2017-02-22 00:00:00' AS datetime),CAST( N'2017-07-06 00:00:00' AS datetime),CAST( N'2017-07-23 00:00:00' AS datetime),CAST( N'2017-01-16 00:00:00' AS datetime),CAST( N'2017-06-16 00:00:00' AS datetime),CAST( N'2016-12-10 00:00:00' AS datetime),CAST( N'2016-12-27 00:00:00' AS datetime),CAST( N'2017-05-10 00:00:00' AS datetime),CAST( N'2016-11-20 00:00:00' AS datetime),CAST( N'2017-04-03 00:00:00' AS datetime),CAST( N'2017-09-01 00:00:00' AS datetime),CAST( N'2016-10-14 00:00:00' AS datetime),CAST( N'2017-03-14 00:00:00' AS datetime),CAST( N'2017-07-26 00:00:00' AS datetime),CAST( N'2017-02-05 00:00:00' AS datetime),CAST( N'2017-06-19 00:00:00' AS datetime),CAST( N'2016-12-30 00:00:00' AS datetime),CAST( N'2017-05-30 00:00:00' AS datetime),CAST( N'2016-11-23 00:00:00' AS datetime),CAST( N'2017-04-23 00:00:00' AS datetime),CAST( N'2017-09-04 00:00:00' AS datetime),CAST( N'2017-09-21 00:00:00' AS datetime),CAST( N'2016-11-03 00:00:00' AS datetime),CAST( N'2017-03-17 00:00:00' AS datetime),CAST( N'2017-08-15 00:00:00' AS datetime),CAST( N'2017-02-08 00:00:00' AS datetime),CAST( N'2017-02-25 00:00:00' AS datetime),CAST( N'2017-07-09 00:00:00' AS datetime),CAST( N'2017-01-19 00:00:00' AS datetime),CAST( N'2017-06-02 00:00:00' AS datetime),CAST( N'2016-12-13 00:00:00' AS datetime),CAST( N'2017-04-26 00:00:00' AS datetime),CAST( N'2017-05-13 00:00:00' AS datetime),CAST( N'2017-09-24 00:00:00' AS datetime),CAST( N'2016-11-06 00:00:00' AS datetime),CAST( N'2017-04-06 00:00:00' AS datetime),CAST( N'2017-08-18 00:00:00' AS datetime),CAST( N'2016-10-17 00:00:00' AS datetime),CAST( N'2017-02-28 00:00:00' AS datetime),CAST( N'2017-07-29 00:00:00' AS datetime),CAST( N'2017-01-22 00:00:00' AS datetime),CAST( N'2017-06-22 00:00:00' AS datetime),CAST( N'2017-01-02 00:00:00' AS datetime),CAST( N'2017-05-16 00:00:00' AS datetime),CAST( N'2016-11-26 00:00:00' AS datetime),CAST( N'2017-04-09 00:00:00' AS datetime),CAST( N'2017-09-07 00:00:00' AS datetime),CAST( N'2016-10-20 00:00:00' AS datetime),CAST( N'2017-03-20 00:00:00' AS datetime),CAST( N'2017-08-01 00:00:00' AS datetime),CAST( N'2017-02-11 00:00:00' AS datetime),CAST( N'2017-06-25 00:00:00' AS datetime),CAST( N'2017-07-12 00:00:00' AS datetime),CAST( N'2017-01-05 00:00:00' AS datetime),CAST( N'2017-06-05 00:00:00' AS datetime),CAST( N'2016-11-29 00:00:00' AS datetime),CAST( N'2016-12-16 00:00:00' AS datetime),CAST( N'2017-04-29 00:00:00' AS datetime),CAST( N'2017-09-27 00:00:00' AS datetime),CAST( N'2016-11-09 00:00:00' AS datetime),CAST( N'2017-03-23 00:00:00' AS datetime),CAST( N'2017-08-21 00:00:00' AS datetime),CAST( N'2016-10-03 00:00:00' AS datetime),CAST( N'2017-03-03 00:00:00' AS datetime),CAST( N'2017-07-15 00:00:00' AS datetime),CAST( N'2017-01-25 00:00:00' AS datetime),CAST( N'2017-06-08 00:00:00' AS datetime),CAST( N'2016-12-19 00:00:00' AS datetime),CAST( N'2017-05-19 00:00:00' AS datetime),CAST( N'2017-09-30 00:00:00' AS datetime),CAST( N'2016-11-12 00:00:00' AS datetime),CAST( N'2017-04-12 00:00:00' AS datetime),CAST( N'2017-08-24 00:00:00' AS datetime),CAST( N'2017-09-10 00:00:00' AS datetime),CAST( N'2016-10-23 00:00:00' AS datetime),CAST( N'2017-03-06 00:00:00' AS datetime),CAST( N'2017-08-04 00:00:00' AS datetime),CAST( N'2017-01-28 00:00:00' AS datetime),CAST( N'2017-02-14 00:00:00' AS datetime),CAST( N'2017-06-28 00:00:00' AS datetime),CAST( N'2017-01-08 00:00:00' AS datetime),CAST( N'2017-05-22 00:00:00' AS datetime),CAST( N'2016-12-02 00:00:00' AS datetime),CAST( N'2017-04-15 00:00:00' AS datetime),CAST( N'2017-05-02 00:00:00' AS datetime),CAST( N'2017-09-13 00:00:00' AS datetime),CAST( N'2016-10-26 00:00:00' AS datetime),CAST( N'2017-03-26 00:00:00' AS datetime),CAST( N'2017-08-07 00:00:00' AS datetime),CAST( N'2016-10-06 00:00:00' AS datetime),CAST( N'2017-02-17 00:00:00' AS datetime),CAST( N'2017-07-18 00:00:00' AS datetime),CAST( N'2017-01-11 00:00:00' AS datetime),CAST( N'2017-06-11 00:00:00' AS datetime),CAST( N'2016-12-22 00:00:00' AS datetime),CAST( N'2017-05-05 00:00:00' AS datetime),CAST( N'2016-11-15 00:00:00' AS datetime),CAST( N'2017-03-29 00:00:00' AS datetime),CAST( N'2017-08-27 00:00:00' AS datetime),CAST( N'2016-10-09 00:00:00' AS datetime),CAST( N'2017-03-09 00:00:00' AS datetime),CAST( N'2017-07-21 00:00:00' AS datetime),CAST( N'2017-01-31 00:00:00' AS datetime),CAST( N'2017-06-14 00:00:00' AS datetime),CAST( N'2017-07-01 00:00:00' AS datetime),CAST( N'2016-12-25 00:00:00' AS datetime),CAST( N'2017-05-25 00:00:00' AS datetime),CAST( N'2016-11-18 00:00:00' AS datetime),CAST( N'2016-12-05 00:00:00' AS datetime),CAST( N'2017-04-18 00:00:00' AS datetime),CAST( N'2017-08-30 00:00:00' AS datetime),CAST( N'2017-09-16 00:00:00' AS datetime),CAST( N'2016-10-29 00:00:00' AS datetime),CAST( N'2017-03-12 00:00:00' AS datetime),CAST( N'2017-08-10 00:00:00' AS datetime),CAST( N'2017-02-03 00:00:00' AS datetime),CAST( N'2017-02-20 00:00:00' AS datetime),CAST( N'2017-07-04 00:00:00' AS datetime),CAST( N'2017-01-14 00:00:00' AS datetime),CAST( N'2017-05-28 00:00:00' AS datetime),CAST( N'2016-12-08 00:00:00' AS datetime),CAST( N'2017-05-08 00:00:00' AS datetime),CAST( N'2017-09-19 00:00:00' AS datetime),CAST( N'2016-11-01 00:00:00' AS datetime),CAST( N'2017-04-01 00:00:00' AS datetime),CAST( N'2017-08-13 00:00:00' AS datetime),CAST( N'2016-10-12 00:00:00' AS datetime),CAST( N'2017-02-23 00:00:00' AS datetime),CAST( N'2017-07-24 00:00:00' AS datetime),CAST( N'2017-01-17 00:00:00' AS datetime),CAST( N'2017-06-17 00:00:00' AS datetime),CAST( N'2016-12-28 00:00:00' AS datetime),CAST( N'2017-05-11 00:00:00' AS datetime),CAST( N'2016-11-21 00:00:00' AS datetime),CAST( N'2017-04-04 00:00:00' AS datetime),CAST( N'2017-04-21 00:00:00' AS datetime),CAST( N'2017-09-02 00:00:00' AS datetime),CAST( N'2016-10-15 00:00:00' AS datetime),CAST( N'2017-03-15 00:00:00' AS datetime),CAST( N'2017-07-27 00:00:00' AS datetime),CAST( N'2017-02-06 00:00:00' AS datetime),CAST( N'2017-07-07 00:00:00' AS datetime),CAST( N'2016-12-31 00:00:00' AS datetime),CAST( N'2017-05-31 00:00:00' AS datetime),CAST( N'2016-12-11 00:00:00' AS datetime),CAST( N'2017-04-24 00:00:00' AS datetime),CAST( N'2017-09-22 00:00:00' AS datetime),CAST( N'2016-11-04 00:00:00' AS datetime),CAST( N'2017-03-18 00:00:00' AS datetime),CAST( N'2017-08-16 00:00:00' AS datetime),CAST( N'2017-02-26 00:00:00' AS datetime),CAST( N'2017-07-10 00:00:00' AS datetime),CAST( N'2017-01-20 00:00:00' AS datetime),CAST( N'2017-06-03 00:00:00' AS datetime),CAST( N'2017-06-20 00:00:00' AS datetime),CAST( N'2016-12-14 00:00:00' AS datetime),CAST( N'2017-05-14 00:00:00' AS datetime),CAST( N'2017-09-25 00:00:00' AS datetime),CAST( N'2016-11-07 00:00:00' AS datetime),CAST( N'2016-11-24 00:00:00' AS datetime),CAST( N'2017-04-07 00:00:00' AS datetime),CAST( N'2017-08-19 00:00:00' AS datetime),CAST( N'2017-09-05 00:00:00' AS datetime),CAST( N'2016-10-18 00:00:00' AS datetime),CAST( N'2017-03-01 00:00:00' AS datetime),CAST( N'2017-07-30 00:00:00' AS datetime),CAST( N'2017-01-23 00:00:00' AS datetime),CAST( N'2017-02-09 00:00:00' AS datetime),CAST( N'2017-06-23 00:00:00' AS datetime),CAST( N'2017-01-03 00:00:00' AS datetime),CAST( N'2017-05-17 00:00:00' AS datetime),CAST( N'2016-11-27 00:00:00' AS datetime),CAST( N'2017-04-27 00:00:00' AS datetime),CAST( N'2017-09-08 00:00:00' AS datetime),CAST( N'2016-10-21 00:00:00' AS datetime),CAST( N'2017-03-21 00:00:00' AS datetime),CAST( N'2017-08-02 00:00:00' AS datetime),CAST( N'2016-10-01 00:00:00' AS datetime),CAST( N'2017-02-12 00:00:00' AS datetime),CAST( N'2017-07-13 00:00:00' AS datetime),CAST( N'2017-01-06 00:00:00' AS datetime),CAST( N'2017-06-06 00:00:00' AS datetime),CAST( N'2016-12-17 00:00:00' AS datetime),CAST( N'2017-04-30 00:00:00' AS datetime),CAST( N'2017-09-28 00:00:00' AS datetime),CAST( N'2016-11-10 00:00:00' AS datetime),CAST( N'2017-03-24 00:00:00' AS datetime),CAST( N'2017-04-10 00:00:00' AS datetime),CAST( N'2017-08-22 00:00:00' AS datetime),CAST( N'2016-10-04 00:00:00' AS datetime),CAST( N'2017-03-04 00:00:00' AS datetime),CAST( N'2017-07-16 00:00:00' AS datetime),CAST( N'2017-01-26 00:00:00' AS datetime),CAST( N'2017-06-26 00:00:00' AS datetime),CAST( N'2016-12-20 00:00:00' AS datetime),CAST( N'2017-05-20 00:00:00' AS datetime),CAST( N'2016-11-30 00:00:00' AS datetime),CAST( N'2017-04-13 00:00:00' AS datetime),CAST( N'2017-09-11 00:00:00' AS datetime),CAST( N'2016-10-24 00:00:00' AS datetime),CAST( N'2017-03-07 00:00:00' AS datetime),CAST( N'2017-08-05 00:00:00' AS datetime),CAST( N'2017-02-15 00:00:00' AS datetime),CAST( N'2017-06-29 00:00:00' AS datetime),CAST( N'2017-01-09 00:00:00' AS datetime),CAST( N'2017-05-23 00:00:00' AS datetime),CAST( N'2017-06-09 00:00:00' AS datetime),CAST( N'2016-12-03 00:00:00' AS datetime),CAST( N'2017-05-03 00:00:00' AS datetime),CAST( N'2017-09-14 00:00:00' AS datetime),CAST( N'2016-10-27 00:00:00' AS datetime),CAST( N'2016-11-13 00:00:00' AS datetime),CAST( N'2017-03-27 00:00:00' AS datetime),CAST( N'2017-08-08 00:00:00' AS datetime),CAST( N'2017-08-25 00:00:00' AS datetime),CAST( N'2016-10-07 00:00:00' AS datetime),CAST( N'2017-02-18 00:00:00' AS datetime),CAST( N'2017-07-19 00:00:00' AS datetime),CAST( N'2017-01-12 00:00:00' AS datetime),CAST( N'2017-01-29 00:00:00' AS datetime),CAST( N'2017-06-12 00:00:00' AS datetime),CAST( N'2016-12-23 00:00:00' AS datetime),CAST( N'2017-05-06 00:00:00' AS datetime),CAST( N'2016-11-16 00:00:00' AS datetime),CAST( N'2017-04-16 00:00:00' AS datetime),CAST( N'2017-08-28 00:00:00' AS datetime),CAST( N'2016-10-10 00:00:00' AS datetime),CAST( N'2017-03-10 00:00:00' AS datetime),CAST( N'2017-07-22 00:00:00' AS datetime),CAST( N'2017-02-01 00:00:00' AS datetime),CAST( N'2017-07-02 00:00:00' AS datetime),CAST( N'2016-12-26 00:00:00' AS datetime),CAST( N'2017-05-26 00:00:00' AS datetime),CAST( N'2016-12-06 00:00:00' AS datetime),CAST( N'2017-04-19 00:00:00' AS datetime),CAST( N'2017-09-17 00:00:00' AS datetime),CAST( N'2016-10-30 00:00:00' AS datetime),CAST( N'2017-03-13 00:00:00' AS datetime),CAST( N'2017-03-30 00:00:00' AS datetime),CAST( N'2017-08-11 00:00:00' AS datetime),CAST( N'2017-02-21 00:00:00' AS datetime),CAST( N'2017-07-05 00:00:00' AS datetime),CAST( N'2017-01-15 00:00:00' AS datetime),CAST( N'2017-06-15 00:00:00' AS datetime),CAST( N'2016-12-09 00:00:00' AS datetime),CAST( N'2017-05-09 00:00:00' AS datetime),CAST( N'2017-09-20 00:00:00' AS datetime),CAST( N'2016-11-19 00:00:00' AS datetime),CAST( N'2017-04-02 00:00:00' AS datetime),CAST( N'2017-08-31 00:00:00' AS datetime),CAST( N'2016-10-13 00:00:00' AS datetime),CAST( N'2017-02-24 00:00:00' AS datetime),CAST( N'2017-07-25 00:00:00' AS datetime),CAST( N'2017-02-04 00:00:00' AS datetime),CAST( N'2017-06-18 00:00:00' AS datetime),CAST( N'2016-12-29 00:00:00' AS datetime),CAST( N'2017-05-12 00:00:00' AS datetime),CAST( N'2017-05-29 00:00:00' AS datetime),CAST( N'2016-11-22 00:00:00' AS datetime),CAST( N'2017-04-22 00:00:00' AS datetime),CAST( N'2017-09-03 00:00:00' AS datetime),CAST( N'2016-10-16 00:00:00' AS datetime),CAST( N'2016-11-02 00:00:00' AS datetime),CAST( N'2017-03-16 00:00:00' AS datetime),CAST( N'2017-07-28 00:00:00' AS datetime),CAST( N'2017-08-14 00:00:00' AS datetime),CAST( N'2017-02-07 00:00:00' AS datetime),CAST( N'2017-07-08 00:00:00' AS datetime),CAST( N'2017-01-01 00:00:00' AS datetime),CAST( N'2017-06-01 00:00:00' AS datetime)))
)
PowerBI生成的SQL查询包括表中存在的每个日期,而不是仅使用度量中提供的大于和小于过滤。
有没有人知道如何重写,以便发送到SQL的查询更漂亮?
答案 0 :(得分:0)
SSAS的存储引擎无法处理>和<之类的复杂比较。它们被编译为IN(所有可能的值)。
一个清晰的模式立即出现:减少基数,查询将变得更加简单。
我建议您检查比较,以便不对日期进行两次比较。 相反,请尝试以下操作:
FILTER(ALL(datetable[calendar_date
], datetable[year]);datetable[year]>=V_START_DATE&&datetable[calendar_date]<=V_END_DATE))
请注意,这并不是确切的最终解决方案,但这是建议避免在两个比较中都使用高基数字段。例如,如果使用year
作为比较之一,则将减少基数并简化编译的SQL查询。