要创建这样的视图,您不需要计算列(除非您有问题中未提及的其他原因)。措施将更容易建立。
措施一:
Last Visit Date = MAX(fact[Date])
措施2:
Current Trainer =
VAR Current_Date = [Last Visit Date]
RETURN
CALCULATE ( VALUES ( fact[Trainer] ), fact[Date] = Current_Date )
措施 3:
Previous Trainer =
VAR Current_Date = [Last Visit Date]
VAR Previous_Date = CALCULATE ( MAX ( fact[Date] ), fact[Date] < Current_Date )
RETURN
CALCULATE ( VALUES ( fact[Trainer] ), fact[Date] = Previous_Date )
措施 4:
Category =
SWITCH (
TRUE (),
[Current Trainer] = "Mario" && [Previous Trainer] = "Mario", "Still Assigned",
[Current Trainer] = "Mario" && [Previous Trainer] <> "Mario", "Newly Assigned",
[Current Trainer] <> "Mario" && [Previous Trainer] = "Mario", "Assignement Removed"
)
将名称放在矩阵行上,将度量 4 和 1 放到值中,您将获得报告。
笔记:
- 我认为提供的示例包含一个错误(Jon Snow 类别应该是“Assignment Removed”,而不是“Newly assignment”;
- 无需对“Mario”进行硬编码,它可以是动态选择的Trainer;
- 该代码依赖于每个日期每个名称一个培训师的假设,否则您将收到错误(您可以使用 If(Hasonevalue[fact[Trainer]), ...
[编辑:防止多个培训师错误]
如果您遇到客户在同一日期可能有多个培训师的情况,VALUES 将返回超过 1 个培训师,这会产生错误。可能的解决方案:
Current Trainer Count:=
VAR Current_Date = [Most Recent]
RETURN
COUNTROWS( CALCULATETABLE(Customer, Customer[Date] = Current_Date ))
这样的措施将返回每个客户当前培训师的数量。然后修改[Current Customer]度量:
Current Trainer:=
VAR Current_Date = [Most Recent]
RETURN
IF([Current Trainer Count] = 1,
CALCULATE( VALUES(Customer[Trainer]), Customer[Date] = Current_Date ),
"Multiple Trainers")
您可以返回所需的任何内容,而不是“多个培训师”文本。例如,您可以返回当前的培训师人数:
Current Trainer:=VAR Current_Date = [Most Recent]
RETURN
IF([Current Trainer Count] = 1,
CALCULATE( VALUES(Customer[Trainer]), Customer[Date] = Current_Date ),
[Current Trainer Count] & " trainers" )
因此,measure 将返回“2 个培训师”,而不是“多个培训师”,等等。
另一种方法是将所有培训师连接到一个分隔列表中。例如:
Current Trainer=
VAR Current_Date = [Most Recent]
RETURN
CONCATENATEX (
CALCULATETABLE ( Customer, Customer[Date] = Current_Date ),
Customer[Trainer], "," )
这种措施不需要保护,因为如果有多个培训师,它会简单地列出它们:
Current Trainer
"Mario, Yoshi"
当然,您也可以在报告中使用“Current Trainer Count”,快速查看此类场景。
相同的逻辑适用于前任培训师。
最后,您需要保护您的 [Category] 度量不受多培训师场景的影响。例如:
Status=
IF([Current Trainer Count] > 1 || [Previous Trainer Count] > 1, "Multiple Trainers",
SWITCH
(TRUE(),
[Current Trainer] = "Mario" && [Previous Trainer] = "Mario", "Same",
[Current Trainer] = "Mario" && [Previous Trainer] <> "Mario","New",
[Current Trainer] <> "Mario" && [Previous Trainer] = "Mario", "Lost"
))