DAX 发出给定时间段和过滤器的 Start- 和 EndDates 计数记录
DAX issue counting records with Start- and EndDates for a given time period and filter
我在 PBI 中有以下 table ("Services"):
table 包含提供给客户的所有服务。服务有 Start- 和 EndDate。
我想要做的是创建一个度量,对于给定的日期或日期范围,returns distinct[=43 的数量=] CustomerID:s 在此期间接受服务.
一些例子:
鉴于上面的 table 以及 2019-01-01 和 2019-04-01 之间的日期范围,该度量将 return 不同的值 3(匹配行 #2,# 4 和 #5).
给出 2019-07-01 的单个日期,该度量将 return 不同的值 3(因为行 #1、#2、#3 和 #4 有一个由 Start- 和EndDate 匹配此日期)。
在我的报告中,我还需要能够按 ServiceTypeID.
进行过滤
table的定义是这样的:
Services =
DATATABLE (
"CustomerID"; INTEGER;
"ServiceTypeID"; INTEGER;
"ServiceStartDate"; DATETIME;
"ServiceEndDate"; DATETIME;
{
{ 1; 10; "2019-06-03"; "2019-09-01" };
{ 2; 12; "2019-01-01"; "2019-12-31" };
{ 2; 10; "2019-05-01"; "2019-09-01" };
{ 3; 8; "2019-02-01"; "2019-08-01" };
{ 4; 10; "2019-03-30"; "2019-06-01" }
}
)
我已经尝试像下面的代码一样定义度量,但是我很难通过 ServiceTypeID 进行过滤(度量只显示一个值,就好像我没有应用过滤器一样服务类型 ID).
Number of active services =
CALCULATE (
DISTINCTCOUNT ( 'Services'[CustomerID] );
FILTER (
ALLSELECTED ( 'Services' );
(
MIN ( 'DateTable'[Date] ) >= 'Services'[ServiceStartDate]
&& MIN ( DateTable[Date] ) <= 'Services'[ServiceEndDate]
)
|| (
MAX ( DateTable[Date] ) >= 'Services'[ServiceStartDate]
&& MAX ( DateTable[Date] ) <= 'Services'[ServiceEndDate]
)
)
)
有人知道我做错了什么吗?非常感谢任何帮助。
亲切的问候,
彼得
我提出这个解决方案:
首先是模型:
那么你可以写这个新的措施:
NumActservices =
// Retrieve the current date
Var VrCurrentDate = MAX(DateTable[Date])
// Retrieve active Acts filter per ServiceID Within the current date
Var VrServicesInPeriode =
CALCULATETABLE(
Services;
FILTER(
ALLEXCEPT(Services;Service[ServiceTypeID]);
Services[ServiceStartDate]<VrCurrentDate
&&
Services[ServiceEndDate]>=VrCurrentDate
)
)
// Count Disctinct customer belonging to the previous table
Var VrResult = CALCULATE(
DISTINCTCOUNT(Services[CustomerID]);
VrServicesInPeriode
)
RETURN
VrResult
结果在此处可见:
我在 PBI 中有以下 table ("Services"):
table 包含提供给客户的所有服务。服务有 Start- 和 EndDate。
我想要做的是创建一个度量,对于给定的日期或日期范围,returns distinct[=43 的数量=] CustomerID:s 在此期间接受服务.
一些例子:
鉴于上面的 table 以及 2019-01-01 和 2019-04-01 之间的日期范围,该度量将 return 不同的值 3(匹配行 #2,# 4 和 #5).
给出 2019-07-01 的单个日期,该度量将 return 不同的值 3(因为行 #1、#2、#3 和 #4 有一个由 Start- 和EndDate 匹配此日期)。
在我的报告中,我还需要能够按 ServiceTypeID.
进行过滤table的定义是这样的:
Services =
DATATABLE (
"CustomerID"; INTEGER;
"ServiceTypeID"; INTEGER;
"ServiceStartDate"; DATETIME;
"ServiceEndDate"; DATETIME;
{
{ 1; 10; "2019-06-03"; "2019-09-01" };
{ 2; 12; "2019-01-01"; "2019-12-31" };
{ 2; 10; "2019-05-01"; "2019-09-01" };
{ 3; 8; "2019-02-01"; "2019-08-01" };
{ 4; 10; "2019-03-30"; "2019-06-01" }
}
)
我已经尝试像下面的代码一样定义度量,但是我很难通过 ServiceTypeID 进行过滤(度量只显示一个值,就好像我没有应用过滤器一样服务类型 ID).
Number of active services =
CALCULATE (
DISTINCTCOUNT ( 'Services'[CustomerID] );
FILTER (
ALLSELECTED ( 'Services' );
(
MIN ( 'DateTable'[Date] ) >= 'Services'[ServiceStartDate]
&& MIN ( DateTable[Date] ) <= 'Services'[ServiceEndDate]
)
|| (
MAX ( DateTable[Date] ) >= 'Services'[ServiceStartDate]
&& MAX ( DateTable[Date] ) <= 'Services'[ServiceEndDate]
)
)
)
有人知道我做错了什么吗?非常感谢任何帮助。
亲切的问候, 彼得
我提出这个解决方案:
首先是模型:
那么你可以写这个新的措施:
NumActservices =
// Retrieve the current date
Var VrCurrentDate = MAX(DateTable[Date])
// Retrieve active Acts filter per ServiceID Within the current date
Var VrServicesInPeriode =
CALCULATETABLE(
Services;
FILTER(
ALLEXCEPT(Services;Service[ServiceTypeID]);
Services[ServiceStartDate]<VrCurrentDate
&&
Services[ServiceEndDate]>=VrCurrentDate
)
)
// Count Disctinct customer belonging to the previous table
Var VrResult = CALCULATE(
DISTINCTCOUNT(Services[CustomerID]);
VrServicesInPeriode
)
RETURN
VrResult
结果在此处可见: