具有多个日期计算的 Tableau table

Tableau table with calculations on multiple dates

我有 table 份保单,其中包含生效日期和到期日期。我想知道每个月有多少政策生效。

原始 Table

id, effective_date, expiration_date
1, 2020-01-01, 2020-06-01
2, 2020-02-01, 2020-07-01
3, 2020-03-01, 2020-08-01
4, 2020-04-01, 2020-09-01
5, 2020-04-01, 2020-09-01

理想输出

Month, Count
Jan, 1
Feb, 2
March, 3
April, 5
May, 5
June, 5
July, 4
August, 3
Sept, 2

我想我可以做类似下面的事情

Effective_Count

RUNNING_COUNT(COUNT([policies]))

Expired_Count

RUNNING_COUNT(COUNT(
  IF(policy.expiration_date < RowMonth)
  THEN 1
  ELSE 0
  END 
))

然后 policies_in_effect 就是

Effective_Count - Expired_Count

虽然我无法获得 RowMonth 值。

旋转会有帮助。 还有一件事,即使保单在每月的第一天到期,您也认为这些保单在当天有效,因此您必须在到期月份增加一天。

第 1 步:在数据选项卡中,select 日期字段和 select pivot

根据您的喜好重命名这两个字段。然后添加一个计算字段以将额外的 day/month 添加为 DATEADD('month', 1, [Date])。然后添加另一个带有 IF THEN ELSE condition.

的计算字段
If [Event] = 'effective_date' then 1 else -1 END

在行上添加 calcF 1 --> 右键单击​​ select 月 --> 将其更改为离散 -> 再次右键单击并选中显示缺失值

在文本标记上添加计算 F2 --> 右键单击​​ --> 添加 table 计算 --> 像这样编辑为 运行 总数

!Preview2]2

这会给你想要的结果