PowerQuery,以下代码,用来获得工单的上一次创建时间。
功能已经实现,但当数据量大时,性能比较差。
- let
- 获得上次数据 = (curList as list, curValue as datetime)=>
- let
- thisIndex = List.PositionOf(curList, curValue),
- thisValue = if thisIndex > 0 then curList{thisIndex-1} else "第一次创建"
- in
- thisValue,
- 源 = #table({"工单号","创建时间"},{{"A14981",#datetime(2022,7,18,1,16,38)},{"A23369",#datetime(2022,7,18,4,22,36)},{"A66258",#datetime(2022,7,18,7,27,55)},{"A38457",#datetime(2022,7,18,7,35,11)},{"A43493",#datetime(2022,7,18,7,48,42)},{"A72556",#datetime(2022,7,18,8,3,15)},{"A73891",#datetime(2022,7,18,8,9,42)},{"A67268",#datetime(2022,7,18,8,10,20)},{"A23369",#datetime(2022,7,16,8,18,0)},{"A57730",#datetime(2022,7,18,8,19,22)},{"A80632",#datetime(2022,7,18,8,24,11)},{"A23369",#datetime(2022,7,18,8,32,27)},{"A85477",#datetime(2022,7,18,8,42,57)},{"A66258",#datetime(2022,7,18,8,54,37)},{"A74534",#datetime(2022,7,18,8,54,44)},{"A10964",#datetime(2022,7,18,9,0,37)},{"A69654",#datetime(2022,7,18,9,3,20)},{"A76752",#datetime(2022,7,18,9,3,36)},{"A33742",#datetime(2022,7,18,9,3,37)},{"A16170",#datetime(2022,7,18,9,3,54)},{"A76123",#datetime(2022,7,18,9,8,39)},{"A31725",#datetime(2022,7,18,9,10,3)},{"A18579",#datetime(2022,7,18,9,15,46)},{"A84359",#datetime(2022,7,18,9,15,47)},{"A39633",#datetime(2022,7,18,9,17,4)},{"A87179",#datetime(2022,7,18,9,17,33)},{"A58260",#datetime(2022,7,18,9,17,40)},{"A11409",#datetime(2022,7,18,9,17,47)},{"A54634",#datetime(2022,7,18,9,22,7)},{"A82306",#datetime(2022,7,18,9,22,11)}}),
- 分组.工单号分组排序 = Table.Group(源, {"工单号"}, {{"工单号分组排序", each Table.Sort(_, {"创建时间"})[创建时间], type table [工单号=text, 创建时间=datetime]}}),
- 添加列.创建时间列表 = Table.AddColumn(源,"创建时间列表",each Table.SelectRows(分组.工单号分组排序,(x)=>x[工单号]=[工单号])[工单号分组排序]{0}),
- 添加列.上次创建时间 = Table.AddColumn(添加列.创建时间列表, "上次创建时间", each 获得上次数据([创建时间列表],[创建时间])),
- 删除列.创建时间列表 = Table.RemoveColumns(添加列.上次创建时间,{"创建时间列表"})
- in
- 删除列.创建时间列表
复制代码
所以,如果根据工单号和创建时间,获得其上次创建时间,怎么写更简洁高效?请各位大佬指教,感谢。
|