我有 2 个查询:
优质的:
和损失:
如何使用 DAX 简单地汇总 Premium 查询中的数据并将其 LEFT JOIN 到 Losses 查询中的汇总数据?
在 SQL 中是这样的:
declare @PremiumTable table (PolicyNumber varchar(50), Premium money)
insert into @PremiumTable values
('Pol1', 100),
('Pol1', 50),
('Pol2', 300),
('Pol3', 500),
('Pol3', 200),
('Pol4',400)
declare @LossesTable table (PolicyNumber varchar(50), Losses money)
insert into @LossesTable values ('Pol1',115),
('Pol1',25),
('Pol2',0),
('Pol3',110),
('Pol3',75)
select p.PolicyNumber,
sum(p.Premium) as Premium,
sum(l.Losses)as Losses
from @PremiumTable p
LEFT JOIN @LossesTable l on p.PolicyNumber = l.PolicyNumber
group by p.PolicyNumber
结果:
我尝试使用NATURALLEFTOUTERJOIN
,但它给了我一个错误:
*An incompatible join column, (''[PolicyNumber]) was detected. 'NATURALLEFTOUTERJOIN' doesn't support joins by using columns with different data types or lineage.*
MyTable =
VAR Premium =
SELECTCOLUMNS(
fact_Premium,
"PolicyNumber",fact_Premium[PolicyNumber],
"Premium", fact_Premium[Premium]
)
VAR Losses =
SELECTCOLUMNS(
fact_Losses,
"PolicyNumber", fact_Losses[PolicyNumber],
"Losses", fact_Losses[PaymentAmount]
)
VAR Result = NATURALLEFTOUTERJOIN(Premium,Losses)
RETURN Result