First Visit =
// Make a table of all patients in context and their first visit id in context
VAR PatientFirstVisits =
ADDCOLUMNS (
VALUES ( 'F_Visits'[Customer Fk] ),
"MinVisitId", CALCULATE (
MIN ( 'F_Visits'[Visit Id] ),
ALL ( 'F_Visits'[Visit Id], 'F_Visits'[Seller] ), // note: fragile
ALLSELECTED ( 'D_Calendar' )
)
)
RETURN
// Count the Visit Ids that are associated with one of the patient-visit pairs in
// PatientFirstVisits
CALCULATE (
DISTINCTCOUNT ( 'F_Visits'[Visit Id] ),
KEEPFILTERS ( TREATAS ( PatientFirstVisits, 'F_Visits'[Customer Fk], 'F_Visits'[Visit Id] ) )
)
请注意脆弱部分,最佳实践是仅构建 FK 和可聚合事实的事实表,即事实表中没有任何描述性属性。注释部分脆弱的原因是您需要添加到ALL
您添加到事实中的任何新的描述性列可能最终会出现在使用此度量的视觉效果中。如果你把卖家拉到暗淡的地方,你可以重构如下:
First Visit =
VAR PatientFirstVisits =
ADDCOLUMNS (
VALUES ( 'F_Visits'[Customer Fk] ),
"MinVisitId", CALCULATE (
MIN ( 'F_Visits'[Visit Id] ),
ALL ( 'F_Visits'[Visit Id] ),
ALLSELECTED ( 'D_Calendar' ),
ALL ( 'Seller' )
)
)
RETURN
CALCULATE (
DISTINCTCOUNT ( 'F_Visits'[Visit Id] ),
KEEPFILTERS ( TREATAS ( PatientFirstVisits, 'F_Visits'[Customer Fk], 'F_Visits'[Visit Id] ) )
)
根据我的经验,表中的列比模型中的表的波动性大约高一个数量级。我的意思是,我通常会看到列中的流失(以某种方式添加、删除、重构)比表中的流失更多。通过拉出“卖家”暗淡,您可以这样做ALL ( 'Seller' )
并且您不必担心跟踪列。
以下是实际采取的措施:
Edit:基于询问我们是否可以做的评论的版本COUNTROWS
版本而不是DISTINCTCOUNT
。 VAR 是一样的,所以我只是显示了之后的计数RETURN
.
...
RETURN
// Count the Visit Ids that are associated with one of the patient-visit pairs in
// PatientFirstVisits
COUNTROWS (
INTERSECT (
GROUPBY( 'F_visits', 'F_visits'[Customer FK], 'F_visits'[Visit Id] ),
PatientFirstVisits
)
)
这里我们只是将 VAR 表和事实中的值相交。该相交为 1 行或对于详细信息行为空,并且它是未经“F_Visits”[访问 ID] 过滤的总数。