我有 15 分钟间隔的气压计压力数据和 5 分钟间隔的 CT2X 压力数据。我想将 CT2X 数据与气压记录器数据尽可能匹配。我的时间序列从 2024 年 9 月 2 日到 2024 年 10 月 17 日,每个结局的具体时间有所不同,因为将它们带出现场下载数据(CT2X 为 11:31,气压记录器为 12:05)。
我已经运行了以下代码,并具有两分钟的允许阈值差异来匹配,它可以工作,但只提供 1,409 行,并且似乎在 2024 年 9 月 18 日提前结束,但它只为我提供直到 2024 年 9 月 18 日 3 的结果:50 由于某种原因。我不相信时间有偏差,因为它连续不间断地运行。
这是我在 Power Query 中使用的代码:
let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Barologger DateTime (24hr)", type datetime}, {"Barologger LEVEL (cm)", type number}, {"CT2X Date / Time (24 hour)", type datetime}, {"CT2X Pressure (psi)", type number}}),
#"Added Custom" =Table.AddColumn(#"Changed Type","Match CT2X", (r)=>
let
//generate a list of differences between each of the times
difs = List.TransformMany(
#"Changed Type"[#"CT2X Date / Time (24 hour)"],
each {_ -r[#"Barologger DateTime (24hr)"]},
(a,b)=> Number.Abs(Number.From(b))),
//determine the row numbers with the matching min times, using 2 minutes (2/1440) as the threshold
pos = if List.Min(difs) < 2/1440
then List.PositionOf(difs,List.Min(difs))
else null
in pos, Int64.Type),
#"Filtered Rows" = Table.SelectRows(#"Added Custom", each [Match CT2X] <> null and [Match CT2X] <> ""),
#"Removed Columns" = Table.RemoveColumns(#"Filtered Rows",{"CT2X Date / Time (24 hour)", "CT2X Pressure (psi)"}),
#"Matching CT2X" = Table.TransformColumns(#"Removed Columns",{"Match CT2X",
each Table.SelectColumns(#"Added Custom",{"CT2X Date / Time (24 hour)", "CT2X Pressure (psi)"}){_},type [#"CT2X Date/Time"=datetime, CT2X Pressure=number]}),
#"Expanded Match CT2X" = Table.ExpandRecordColumn(#"Matching CT2X", "Match CT2X", {"CT2X Date / Time (24 hour)", "CT2X Pressure (psi)"}, {"CT2X Date / Time (24 hour)", "CT2X Pressure (psi)"})
in
#"Expanded Match CT2X"
这是文本形式的数据(运行代码之前):
|BarologgerDateTime|BarologgerLevel (cm)|CT2X DateTime| CT2X压力 (psi)| |2024-09-03 11:05|70.89|2024-09-03 11:41|14.519| |2024-09-03 11:20|71.22|2024-09-03 11:46|14.794| |2024-09-03 11:35|70.91|2024-09-03 11:51|14.84| |2024-09-03 11:50|70.84|2024-09-03 11:56|14.841| |2024-09-03 12:05|70.78|2024-09-03 12:01|14.841| |2024-09-03 12:20|70.74|2024-09-03 12:06|14.842| |2024-09-03 12:35|70.61|2024-09-03 12:11|14.841| |2024-09-03 12:50|70.65|2024-09-03 12:16|14.842| |2024-09-03 13:05|70.51|2024-09-03 12:21|14.841|
这是文本形式的数据(运行代码后)
|BaroDateTime|BaroLevel|CT2X 日期时间|CT2X 压力 (psi)| |2024-09-03 11:50|70.84|2024-09-03 11:51|14.84| |2024-09-03 12:05|70.78|2024-09-03 12:06|14.842| |2024-09-03 12:20|70.74|2024-09-03 12:21|14.841| |2024-09-03 12:35|70.61|2024-09-03 12:36|14.84| |2024-09-03 12:50|70.65|2024-09-03 12:51|14.839| |2024-09-03 13:05|70.51|2024-09-03 13:06|14.838| |2024-09-03 13:20|70.47|2024-09-03 13:21|14.839| |2024-09-03 13:35|70.46|2024-09-03 13:36|14.837| |2024-09-03 13:50|70.26|2024-09-03 13:51|14.835|
看看这是否适合你
let Source = Excel.CurrentWorkbook(){[Name="Table3"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Barologger DateTime (24hr)", type datetime}, {"Barologger LEVEL (cm)", type number}, {"CT2X Date / Time (24 hour)", type datetime}, {"CT2X Pressure (psi)", type number}}),
#"Added Custom" =Table.AddColumn(#"Changed Type","Match CT2X",
each let a=[#"Barologger DateTime (24hr)"],
b= #"Changed Type"[#"CT2X Date / Time (24 hour)"],
c= List.Transform(b, each if Number.Abs(Duration.TotalMinutes(_-a))<2 then Number.Abs(Duration.TotalMinutes(_-a)) else 999999 ),
d = if List.Min(c)=999999 then null else b{List.PositionOf(c,List.Min(c))}
in d, type datetime)
in #"Added Custom"