Power BI Dax 用复杂的逻辑计算时间间隔
Power BI Dax Calculate Time Intervals With Complicated Logic
我想计算这个table中每个任务的滞后时间。延迟计算一个任务完成和下一个任务开始之间的时间间隔。
有案例(CaseNum),一个案例可以有不同的任务(TaskNum),一个阶段( TaskStage)代表一个任务的当前阶段(但不需要连续,我们可以跳到下一个阶段).
rank 是我创建的参考列,用于显示每个案例下的任务阶段顺序。
Lag的逻辑比较复杂---
- 对于 S1:初始日期 - 批准日期
- For S2: InitDate - S1 最后完成的任务的 CompletedDate
阶段。 (如果在 S1 阶段没有任何任务,它将是 InitDate -
批准日期)
- 对于 S3:InitDate - 最后完成任务的 CompletedDate
S2。 (如果在 S2 阶段没有任何任务,它将是 InitDate -
S1 最后完成任务的 CompletedDate,如果没有
S1 阶段的任务,它将是 InitDate - ApproveDate)
- 对于 S4:InitDate - S3 最后完成的任务的 CompletedDate。 (相同
逻辑:如果S3阶段没有任何任务,它将是InitDate -
S2 最后完成任务的 CompletedDate,如果没有
S2 阶段的任务,它将是最后一个的 InitDate - CompletedDate
S1阶段完成的任务,如果S1阶段没有任何任务,则
是 InitDate - ApproveDate)
- 如果滞后为负,则保持为零。
table 中的背景颜色表示我用来计算延迟的日期对。希望它可以帮助理解逻辑。谢谢!!!
假设您尝试计算计算列,您可以尝试以下操作:
Lag =
-- getting a table with lagged rows (ranks smaller then current rows rank)
VAR lags =
FILTER (
ALL ( 'Table' ),
'Table'[CaseNum]
-- EARLIER() will get the 'outer' row context's CaseNum
-- outer row context: calculated column one
= EARLIER ( 'Table'[CaseNum] )
&& 'Table'[rank]
< EARLIER ( 'Table'[rank] )
)
-- extracting only the direct previous rank
VAR lag_value =
MAXX (
lags,
'Table'[rank]
)
-- checking if there is a previous task (rank value is not blank)
VAR cond =
ISBLANK ( lag_value )
-- getting currents row InitDate (in calculated columns row context)
VAR current_date = 'Table'[InitDate]
-- getting the date from the previous task
VAR last_date =
CALCULATE (
MAX ( 'Table'[CompletedDate] ),
FILTER (
ALL ( 'Table' ),
'Table'[CaseNum]
= EARLIER ( 'Table'[CaseNum] )
&& 'Table'[rank] = lag_value
)
)
RETURN
IF (
cond,
-- if blank, return the days between approval and init
DATEDIFF (
'Table'[ApproveDate],
'Table'[InitDate],
DAY
),
-- else return days between last completed and init,
-- negative values will be set to 0
MAX (
DATEDIFF (
last_date,
current_date,
DAY
),
0
)
)
我想计算这个table中每个任务的滞后时间。延迟计算一个任务完成和下一个任务开始之间的时间间隔。
有案例(CaseNum),一个案例可以有不同的任务(TaskNum),一个阶段( TaskStage)代表一个任务的当前阶段(但不需要连续,我们可以跳到下一个阶段).
rank 是我创建的参考列,用于显示每个案例下的任务阶段顺序。
Lag的逻辑比较复杂---
- 对于 S1:初始日期 - 批准日期
- For S2: InitDate - S1 最后完成的任务的 CompletedDate 阶段。 (如果在 S1 阶段没有任何任务,它将是 InitDate - 批准日期)
- 对于 S3:InitDate - 最后完成任务的 CompletedDate S2。 (如果在 S2 阶段没有任何任务,它将是 InitDate - S1 最后完成任务的 CompletedDate,如果没有 S1 阶段的任务,它将是 InitDate - ApproveDate)
- 对于 S4:InitDate - S3 最后完成的任务的 CompletedDate。 (相同
逻辑:如果S3阶段没有任何任务,它将是InitDate -
S2 最后完成任务的 CompletedDate,如果没有 S2 阶段的任务,它将是最后一个的 InitDate - CompletedDate S1阶段完成的任务,如果S1阶段没有任何任务,则 是 InitDate - ApproveDate) - 如果滞后为负,则保持为零。
table 中的背景颜色表示我用来计算延迟的日期对。希望它可以帮助理解逻辑。谢谢!!!
假设您尝试计算计算列,您可以尝试以下操作:
Lag =
-- getting a table with lagged rows (ranks smaller then current rows rank)
VAR lags =
FILTER (
ALL ( 'Table' ),
'Table'[CaseNum]
-- EARLIER() will get the 'outer' row context's CaseNum
-- outer row context: calculated column one
= EARLIER ( 'Table'[CaseNum] )
&& 'Table'[rank]
< EARLIER ( 'Table'[rank] )
)
-- extracting only the direct previous rank
VAR lag_value =
MAXX (
lags,
'Table'[rank]
)
-- checking if there is a previous task (rank value is not blank)
VAR cond =
ISBLANK ( lag_value )
-- getting currents row InitDate (in calculated columns row context)
VAR current_date = 'Table'[InitDate]
-- getting the date from the previous task
VAR last_date =
CALCULATE (
MAX ( 'Table'[CompletedDate] ),
FILTER (
ALL ( 'Table' ),
'Table'[CaseNum]
= EARLIER ( 'Table'[CaseNum] )
&& 'Table'[rank] = lag_value
)
)
RETURN
IF (
cond,
-- if blank, return the days between approval and init
DATEDIFF (
'Table'[ApproveDate],
'Table'[InitDate],
DAY
),
-- else return days between last completed and init,
-- negative values will be set to 0
MAX (
DATEDIFF (
last_date,
current_date,
DAY
),
0
)
)