使用 DAX 计算包含时间戳的日期时间间隔
Count datetime intervals which contains timestamp with DAX
我有两个 table:
具有日期和时间的日历 table。
还有一个包含事件的 table,除了一组属性(只有一个,incident_code,在示例 table 中),还有开始时间和结束时间。
对于特定日期范围,我想显示每小时发生了多少事件。
以下措施有效,但当事件 table 变大并且有多个切片器时会遇到困难
incidentCnt =
CALCULATE(
COUNTROWS(incidents);
FILTER(
incidents;
incidents[start_datetime] < MAX(date_hour[datetime]) &&
incidents[end_datetime] > MAX(date_hour[datetime])
)
)
在 Power BI 中使用 DAX 计算此值的更有效方法是什么?
编辑:此表达式将与上面提供的数据一起使用(对已接受的答案进行少量编辑)。
Expanded_Incidents =
GENERATE(
incidents;
SELECTCOLUMNS(
GENERATESERIES(
DATE(
YEAR(incidents[start_datetime]);
MONTH(incidents[start_datetime]);
DAY(incidents[start_datetime])
) + TIME(HOUR(incidents[start_datetime]);0;0);
incidents[end_datetime];
TIME(1;0;0)
);
"Expanded_incident_time"; [Value]
)
)
更有效的方法是扩展事件 table,以便每个事件在开始和结束日期时间之间的每个时间发生。您可以像这样使用计算出的 table 来做到这一点:
Expanded_Incidents =
GENERATE(
incidents;
SELECTCOLUMNS(
GENERATESERIES(
incidents[start_date];
incidents[end_date];
TIME(h;m;s)
);
"Expanded_incident_time"; [Value]
)
)
将 TIME(h;m;s)
更改为您选择的时间分辨率。
然后在 dateTime dim table 和 [Expanded_incident_time]
列之间建立关系(这应该是 1:*)。然后你只需要做一个计算新 table 中的行数的度量。这将为您提供特定时间内活动事件的数量,我相信这比每次重复 table 更快。
我有两个 table:
具有日期和时间的日历 table。
还有一个包含事件的 table,除了一组属性(只有一个,incident_code,在示例 table 中),还有开始时间和结束时间。
对于特定日期范围,我想显示每小时发生了多少事件。
以下措施有效,但当事件 table 变大并且有多个切片器时会遇到困难
incidentCnt =
CALCULATE(
COUNTROWS(incidents);
FILTER(
incidents;
incidents[start_datetime] < MAX(date_hour[datetime]) &&
incidents[end_datetime] > MAX(date_hour[datetime])
)
)
在 Power BI 中使用 DAX 计算此值的更有效方法是什么?
编辑:此表达式将与上面提供的数据一起使用(对已接受的答案进行少量编辑)。
Expanded_Incidents =
GENERATE(
incidents;
SELECTCOLUMNS(
GENERATESERIES(
DATE(
YEAR(incidents[start_datetime]);
MONTH(incidents[start_datetime]);
DAY(incidents[start_datetime])
) + TIME(HOUR(incidents[start_datetime]);0;0);
incidents[end_datetime];
TIME(1;0;0)
);
"Expanded_incident_time"; [Value]
)
)
更有效的方法是扩展事件 table,以便每个事件在开始和结束日期时间之间的每个时间发生。您可以像这样使用计算出的 table 来做到这一点:
Expanded_Incidents =
GENERATE(
incidents;
SELECTCOLUMNS(
GENERATESERIES(
incidents[start_date];
incidents[end_date];
TIME(h;m;s)
);
"Expanded_incident_time"; [Value]
)
)
将 TIME(h;m;s)
更改为您选择的时间分辨率。
然后在 dateTime dim table 和 [Expanded_incident_time]
列之间建立关系(这应该是 1:*)。然后你只需要做一个计算新 table 中的行数的度量。这将为您提供特定时间内活动事件的数量,我相信这比每次重复 table 更快。