具有多个日期的 Tableau 日期数学

Tableau date math with multiple dates

我有 table 保单,其中包含帐号、生效日期、取消日期和到期日期。我想知道一个帐户的有效政策持续了多长时间

原始 Table

id, account_id, effective_date, cancellation_date, expiration_date
1, a, 2020-01-01, null, 2020-06-01
2, b, 2020-02-01, null, 2020-07-01
3, b, 2020-03-01, null, 2020-08-01
4, a, 2020-04-01, null, 2020-09-01
5, b, 2020-04-01, 2020-08-15, 2020-09-01

理想输出

account_id, active_date, inactive_date, active_time
a, 2020-01-01, 2020-09-01, 9 months
b, 2020-02-01, 2020-08-15, 7 months 15 days

到目前为止,我已经制作了一个 table,其中 account_id 作为左侧栏。然后我有 MIN(effective_date) 让我知道第一个保单的有效日期。

那我有Policy_Inactive_Date = MIN(cancellation_date, expiration_date)。但这给了我第一个保单过期或取消的时间。

感觉我需要做 MAX(Policy_Inactive_Date) 但那会抛出错误。

我想知道是否首先我需要在策略级别获得 Policy_Inactive_Date,然后在帐户级别获得最大值。

尝试MAX(MIN(cancellation_date, expiration_date))

有两种形式的 MIN() -- 和两种形式的 MAX()。对于一个参数,MIN() 是一个聚合函数,为一组记录返回该参数的最小 non-null 值。对于两个参数,MIN() 计算每个参数和 returns 这两个值中的最小值。

那样做

active_dt 这样的字段

{
    Fixed [account_id]: MIN([effective_date])
}

inactive_dt这样

{
    Fixed [account_id]:MAX(IF ISNULL(MIN([cancellation_date],[expiration_date])) then [expiration_date] else MIN([cancellation_date],[expiration_date]) END)
}