【问题标题】:How to calculate the date difference between a date column and a filter expression如何计算日期列和过滤器表达式之间的日期差异
【发布时间】:2020-02-08 18:47:06
【问题描述】:

我有一个包含两个日期列的案例事实表:startDate 和 endDate 以及一个 unitCount。我有一个包含所有日期的不相关日期维度。

这样我可以计算病例数为:

Number of cases (Beginning of month) :=CALCULATE(
sum('Fact'[unitCount]),
FILTER('Fact',
'Fact'[endDate]>=MIN(Date[Date]) &&
'Fact'[startDate]<=MIN(Date[Date])
))

这非常有用,可以生成一个不错的列表:

Year-Month Number of cases(Beginning of month)
2018-01          2
2018-02          5
2018-03          3
... etc

我想添加一列,用于衡量每个月初案件的平均持续时间。

我已经尝试过测量:

Average duration (Beginning of month):=calculate(AVERAGEX('Fact',
datediff('Fact'[startDate],
MIN('Date'[Date]),
MONTH)),
FILTER('Fact',
'Fact'[endDate]>=MIN('Date'[Date]) &&
'Fact'[startDate]<=MIN('Date'[Date])
))

但是,此代码失败并出现错误:在 Datediff 函数中,开始日期不能比结束日期更早。这应该不是问题,因为过滤器可以防止这种情况发生。我该如何解决这个问题?

【问题讨论】:

  • 我应该补充一点,unitCount 始终为 1,因为我的 factTable 中的所有案例都具有唯一引用。

标签: dax datediff


【解决方案1】:

我想我自己可能已经找到了解决方案:

Average duration :=
CALCULATE (
    AVERAGEX (
        'Fact',
        DATEDIFF (
            'Fact'[startDate],
            MINX ( FILTER ( 'Date', Date[Date] >= 'Fact'[startDate] ), 'Date'[Date] ),
            MONTH
        )
    ),
    FILTER (
        'Fact',
        'Fact'[endDate] >= MIN ( Date[Date] )
            && 'Fact'[StartDate] <= MIN ( Date[Date] )
    )
)

这段代码似乎运行良好。太好了!

【讨论】:

    猜你喜欢
    • 2016-06-06
    • 1970-01-01
    • 2018-11-09
    • 1970-01-01
    • 1970-01-01
    • 2016-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多