我们正在努力解决一个可能比我想象的更简单的问题。我只是卡住了。
问题包括定义一个 DAX Measure
那个动态 仅计数 第一 期间访问量每位患者 .访问次数为 不是第一 期间必须是分配的空白()值(value)。
在讨论目标之前,请让我介绍一下场景。
我们有一个由一个事实表 (
F_Visits
) 和两个维度 ( D_Customer
; D_Calendar
) 组成的模型。F_Visits
有 100 万条记录,包含顾客对服装店的所有访问。它的字段是:Visit Id
: 增量唯一编号,是表的自然键Customer Fk
: 事实表中维度D_Customer的外键Calendar Fk
: 事实表中维度D_Calendar的外键Quantity
: 参观中购买的布片数量Amount
: 访问中花费的美元金额Seller
:为客户服务的员工姓名创建一个度量:
1
如果它是所选时间段内该患者的第一个 (期间的最小访问 ID) ,忽略每个客户在选定时间段内不是第一次的访问
过去两周我一直在在线社区上搜索,但没有发现像我这样的问题。
类似(但不同)的如下:Sum distinct values for first occurance in Power BI
与该问题相比的主要区别在于,对于在所选时间段内不是该患者的第一个访问,此问题的度量需要为 BLANK()。
3. 示例:
+----------+-------------+-------------+----------+--------+--------+
| Visit Id | Customer FK | Calendar FK | Quantity | Amount | Seller |
+----------+-------------+-------------+----------+--------+--------+
| 1 | John | 20170101 | 1 | 10 | Rick |
| 2 | John | 20180101 | 2 | 15 | Morty |
| 3 | John | 20180101 | 3 | 17 | Eric |
| 4 | John | 20190101 | 2 | 17 | Eric |
| 5 | Mark | 20170101 | 1 | 17 | Eric |
| 6 | Mark | 20180101 | 3 | 12 | Eric |
| 7 | Jack | 20190101 | 0 | 0 | Rick |
+----------+-------------+-------------+----------+--------+--------+
用户现在过滤日历 FK = 2018。这将排除一些访问
并且该度量是动态计算的,以仅计算每个患者在选定时间段内的第一次访问:
明细表 :
+----------+-------------+-------------+----------+--------+--------+---------+
| Visit Id | Customer FK | Calendar FK | Quantity | Amount | Seller | Measure |
+----------+-------------+-------------+----------+--------+--------+---------+
| 2 | John | 20180101 | 2 | 15 | Morty | 1 |
| 3 | John | 20180101 | 3 | 17 | Eric | BLANK() |
| 6 | Mark | 20180101 | 3 | 12 | Eric | 1 |
+----------+-------------+-------------+----------+--------+--------+---------+
总计 :
+-------+
| Total |
+-------+
| 2 |
+-------+
卖家总计 :
+--------+---------+
| Seller | Measure |
+--------+---------+
| Morty | 1 |
| Eric | 1 |
| Rick | BLANK() |
+--------+---------+
除了需要在前端动态计算外,该方法没有具体要求。
更新
请引用问题DAX: avoid ALL() causing incorrect show of dimensional attribute in matrix
最佳答案
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' )
并且您不必担心跟踪列。这是正在采取的措施:
编辑:基于评论的版本,询问我们是否可以做
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'[Visit Id] 过滤的总数。
关于powerbi - DAX 测量 : dynamically consider only first for each group and and overall returns their count,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57786786/