根据一个表中的存在进行过滤而另一个表中不存在

时间:2015-06-22 22:31:02

标签: excel powerpivot dax

我有以下数据模型:

记录 Id ,...,CreateDate

FactA :RecordId,CreateDate

FactB :RecordId,CreateDate

FactA与Record,FactB to Record之间存在关系。

我已经对此类记录采取了措施,没有任何问题:

FactA's:=CALCULATE(DISTINCTCOUNT(Records[Id]), FactA)
FactB's:=CALCULATE(DISTINCTCOUNT(Records[Id]), FactB)

现在我想要使用FactA计数记录,但没有FactB,在SQL中我做LEFT JOIN WHERE FactB.RecordId IS NULL但我无法弄清楚如何做类似的DAX。我试过了:

 -- this returns blank, presumably because when there is a FactB then RecordId isn't blank, and when there is no Fact B then RecordId a NULL which isn't blank either
FactA_No_FactB:=CALCULATE(DISTINCTCOUNT(Records[Id]), FactA, FILTER(FactB, ISBLANK([RecordId])))

-- this returns the long "The value for columns "RecordId" in table "FactB" cannot be determined in the current context" error.
FactA_No_FactB:=CALCULATE(DISTINCTCOUNT(Records[Id]), FILTER(FactA, ISBLANK(FactB[RecordId])))

我也尝试了各种使用RELATEDRELATEDTABLE的方式,但我对DAX和背景知之甚少,不知道我在做什么。< / p>

有人可以解释我如何编写计算的度量来计算记录与FactA但没有FactB吗?

提前致谢。

编辑 - 解决方法

我已经提出了这个问题,到目前为止它看起来是正确的,但我不确定这是否是通常正确的方法:

-- Take the count with FactA and subtract the count of (FactA and FactB)
FactA_No_FactB:=CALCULATE(DISTINCTCOUNT(Records[Id]), FactA) - CALCULATE(DISTINCTCOUNT(Records[Id]), FactA, FactB)

1 个答案:

答案 0 :(得分:0)

这是另一种选择,可能仍然不是最佳方式:

FactA_No_FactB:=CALCULATE(DISTINCTCOUNT(Records[ID]), FILTER(Records,CONTAINS(FactA, FactA[RecordID],Records[ID]) && NOT(CONTAINS(FactB,FactB[RecordID],Records[ID]))))

我的版本和你的版本之间的区别在于,我和其中的所有项目中的那些项目返回值为1但不包括B和BLANK。对于A中的项而不是B,您的版本返回1,对于A和B中的项,您的版本返回0,对于其他所有项,您的版本返回BLANK。根据您的使用情况,一种结果可能优先于另一种结果。

相关问题