我已经在PowerQuery中围绕公司机队的利用率构建了一个数据模型。数据模型中有许多不同的列。具体来说,是里程,VIN,开始日期和结束日期(请参见下面的示例表)。
Num a => [a]
我要完成的工作是,如果一个月的里程数等于0,则将其归类为标记为0-30天的存储桶;如果连续两个月的里程数等于0,则将其归类为31-60天,并且连续3个月超过0英里的里程将被分类为> 60天。在上面的示例中,该车辆将归类为“> 60天”时段。是否有使用DAX在数据模型中执行此操作的简便方法?如果您有任何后续问题,请告诉我。谢谢!
答案 0 :(得分:1)
尝试作为计算列:
Buckets =
VAR rowDate = 'myTable'[ Start Date ]
VAR previousDate =
CALCULATE (
MAX ( 'myTable'[ Start Date ] ),
FILTER (
ALLEXCEPT ( myTable, myTable[ VIN ] ),
'myTable'[ Start Date ] < rowDate
)
)
VAR prePreviousDate =
CALCULATE (
MAX ( 'myTable'[ Start Date ] ),
FILTER (
ALLEXCEPT ( myTable, myTable[ VIN ] ),
'myTable'[ Start Date ] < previousDate
)
)
VAR PreviousMileage =
CALCULATE (
MAX ( 'myTable'[Mileage ] ),
ALLEXCEPT ( 'myTable', 'myTable'[ VIN ] ),
'myTable'[ Start Date ] = previousDate
)
VAR PrePreviousMileage =
CALCULATE (
MAX ( 'myTable'[Mileage ] ),
ALLEXCEPT ( 'myTable', 'myTable'[ VIN ] ),
'myTable'[ Start Date ] = prePreviousDate
)
RETURN
SWITCH (
TRUE (),
'myTable'[Mileage ] + IF ( ISBLANK ( PreviousMileage ), 1, PreviousMileage )
+ IF ( ISBLANK ( PrePreviousMileage ), 1, PrePreviousMileage )
= 0, "> 60 Days",
'myTable'[Mileage ] + IF ( ISBLANK ( PreviousMileage ), 1, PreviousMileage )
= 0, "31 to 60 Days",
'myTable'[Mileage ] = 0, "0 to 30 Days",
"No Days"
)
结果看起来像这样。我添加了一些测试值。