在Power BI中根据开始日期和结束日期计算有效合同?

时间:2020-10-13 13:59:27

标签: powerbi dax powerquery

我的数据如下所示:

ContractID  Start Date  End Date
1           01.01.2020  23.03.2020
2           15.02.2020  29.07.2020
3           06.06.2020  null

最后一个合同仍然有效。我有一个日期表,其中开始日期为活动关系。

我也需要最终结果像这样:

Date    Active Contracts
Jan     1
Feb     2
Mar     2
Apr     1
May     1
Jun     2

该度量的外观如何?

谢谢!

1 个答案:

答案 0 :(得分:3)

假设您的日期表中有一个月

VAR currentMonth = SELECTEDVALUE(MyDataTable[Month value]) --needs to be a number 1 to 12
RETURN CALCULATE(COUNTROWS(MyDataTable), 
    ALL(DateTable), 
    MONTH(MyDataTable[Start Date]) >= currentMonth, 
    ISBLANK(MyDataTable[End Date]) || MONTH(MyDataTable[End Date]) <= currentMonth)