如何将SQL翻译成DAX,需要加FILTER
How to translate SQL to DAX, Need to add FILTER
我想创建计算 table 来总结现有 table fact_Premium
的 In_Force
溢价。
我如何过滤结果:
TODAY() has to be between `fact_Premium[EffectiveDate]` and (SELECT TOP 1 fact_Premium[ExpirationDate] ORDE BY QuoteID DESC)
在 SQL 我会这样做:
`WHERE CONVERT(date, getdate()) between CONVERT(date, tblQuotes.EffectiveDate)
and (
select top 1 q2.ExpirationDate
from Table2 Q2
where q2.ControlNo = Table1.controlno
order by quoteid` desc
)
到目前为止,这是我的 DAX 报表:
In_Force Premium =
FILTER(
ADDCOLUMNS(
SUMMARIZE(
//Grouping necessary columns
fact_Premium,
fact_Premium[QuoteID],
fact_Premium[Division],
fact_Premium[Office],
dim_Company[CompanyGUID],
fact_Premium[LineGUID],
fact_Premium[ProducerGUID],
fact_Premium[StateID],
fact_Premium[ExpirationDate]
),
"Premium", CALCULATE(
SUM(fact_Premium[Premium])
),
"ControlNo", CALCULATE(
DISTINCTCOUNT(fact_Premium[ControlNo])
)
), // Here I need to make sure TODAY() falls between fact_Premium[EffectiveDate] and (SELECT TOP 1 fact_Premium[ExpirationDate] ORDE BY QuoteID DESC)
)
此外,什么是更有效的方法,从 fact_Premium 创建计算 table 或使用 sql 语句创建相同的 table (--> Get Data- -> SQL 服务器)?
在 T-SQL 中有 2 种可能的方法来获取下一个生效日期。一种是使用 LEAD()
,另一种是使用 APPLY
运算符。由于这里需要处理的事实很少 samples:
select *
from (
select *
, lead(EffectiveDate) over(partition by CompanyGUID order by quoteid desc) as NextEffectiveDate
from Table1
join Table2 on ...
) d
或
select table1.*, oa.NextEffectiveDate
from Table1
outer apply (
select top(1) q2.ExpirationDate AS NextEffectiveDate
from Table2 Q2
where q2.ControlNo = Table1.controlno
order by quoteid desc
) oa
注意。 outer apply
与 left join
有点相似,因为它将允许查询返回具有 NULL 的行,如果不需要则使用 cross apply
代替。
在这两种方法中,您都可以在最后的 where 子句中引用 NextEffectiveDate
,但如果可行(这取决于数据),我宁愿避免使用转换函数。
我想创建计算 table 来总结现有 table fact_Premium
的 In_Force
溢价。
我如何过滤结果:
TODAY() has to be between `fact_Premium[EffectiveDate]` and (SELECT TOP 1 fact_Premium[ExpirationDate] ORDE BY QuoteID DESC)
在 SQL 我会这样做:
`WHERE CONVERT(date, getdate()) between CONVERT(date, tblQuotes.EffectiveDate)
and (
select top 1 q2.ExpirationDate
from Table2 Q2
where q2.ControlNo = Table1.controlno
order by quoteid` desc
)
到目前为止,这是我的 DAX 报表:
In_Force Premium =
FILTER(
ADDCOLUMNS(
SUMMARIZE(
//Grouping necessary columns
fact_Premium,
fact_Premium[QuoteID],
fact_Premium[Division],
fact_Premium[Office],
dim_Company[CompanyGUID],
fact_Premium[LineGUID],
fact_Premium[ProducerGUID],
fact_Premium[StateID],
fact_Premium[ExpirationDate]
),
"Premium", CALCULATE(
SUM(fact_Premium[Premium])
),
"ControlNo", CALCULATE(
DISTINCTCOUNT(fact_Premium[ControlNo])
)
), // Here I need to make sure TODAY() falls between fact_Premium[EffectiveDate] and (SELECT TOP 1 fact_Premium[ExpirationDate] ORDE BY QuoteID DESC)
)
此外,什么是更有效的方法,从 fact_Premium 创建计算 table 或使用 sql 语句创建相同的 table (--> Get Data- -> SQL 服务器)?
在 T-SQL 中有 2 种可能的方法来获取下一个生效日期。一种是使用 LEAD()
,另一种是使用 APPLY
运算符。由于这里需要处理的事实很少 samples:
select *
from (
select *
, lead(EffectiveDate) over(partition by CompanyGUID order by quoteid desc) as NextEffectiveDate
from Table1
join Table2 on ...
) d
或
select table1.*, oa.NextEffectiveDate
from Table1
outer apply (
select top(1) q2.ExpirationDate AS NextEffectiveDate
from Table2 Q2
where q2.ControlNo = Table1.controlno
order by quoteid desc
) oa
注意。 outer apply
与 left join
有点相似,因为它将允许查询返回具有 NULL 的行,如果不需要则使用 cross apply
代替。
在这两种方法中,您都可以在最后的 where 子句中引用 NextEffectiveDate
,但如果可行(这取决于数据),我宁愿避免使用转换函数。