这有点棘手,但我们可以通过在度量中使用临时计算汇总表来实现:
CountStatusB =
SUMX(
ADDCOLUMNS(
SUMMARIZE(
FILTER(
ALL(Inventory),
Inventory[TimeStamp] <= MAX(Inventory[TimeStamp])
),
Inventory[ID],
"LastTimeStamp",
MAX(Inventory[TimeStamp])
),
"Status",
LOOKUPVALUE(Inventory[Status],
Inventory[ID], Inventory[ID],
Inventory[TimeStamp], [LastTimeStamp])
),
IF([Status] = "B",
1,
0
)
)
首先,我们创建一个汇总表来计算最后的TimeStamp
对于每个ID
价值。为此,我们使用SUMMARIZE
过滤表上的函数,我们只考虑当天或更早的日期,分组依据ID
,并计算出最大值TimeStamp
.
一旦我们有了最大TimeStamp
per ID
今天,我们可以查一下Status
是在那天,并将其作为一列添加到汇总表中。
一旦我们知道了最新的Status
对于每个ID
今天,我们只需要总结一下Status
is "B"
并忽略其他的。
如果我们将其分解为步骤,可能会更容易阅读该措施。这里的逻辑与之前相同,但为了更加清晰而使用变量。
CountB =
VAR CurrDay = MAX(Inventory[TimeStamp])
VAR Summary = SUMMARIZE(
FILTER(
ALL(Inventory),
Inventory[TimeStamp] <= CurrDay
),
Inventory[ID],
"LastTimeStamp",
MAX(Inventory[TimeStamp])
)
VAR LookupStatus = ADDCOLUMNS(
Summary,
"Status",
LOOKUPVALUE(Inventory[Status],
Inventory[ID], Inventory[ID],
Inventory[TimeStamp], [LastTimeStamp]
)
)
RETURN SUMX(LookupStatus, IF([Status] = "B", 1, 0))