Powerquery 识别属于跨越两个连续日期的班次的时间戳
Posted
技术标签:
【中文标题】Powerquery 识别属于跨越两个连续日期的班次的时间戳【英文标题】:Powerquery to recognise timestamps belonging to shift that straddles two consecutive dates 【发布时间】:2018-05-20 04:38:19 【问题描述】:我有一个excel表格中的数据,由我们在现场使用的一些软件生成,报告的时间戳是这样的
2018 年 6 月 5 日上午 6:23:00
由于我们是 24 小时运营,我需要 PowerQuery 能够识别从 12:00:00 AM 到 7:00:00 AM 的时间戳属于前一天的夜班。
我面临的问题是,虽然 PowerQuery 可以将日期/时间作为数据类型处理,但导入数据时似乎会截断时间,因此上述示例在我的查询中的结果(显然在我的输出)是
2018 年 6 月 5 日上午 12:00:00
我在网上能找到的大部分内容都是关于如何消磨时间——我想保留它!!!
这样做的目的是让我可以在数据透视表中按时间顺序显示生产夜班的记录。目前我不得不单独添加另一列,这会导致从午夜到早上 7:00 的数据先于从晚上 7:00 到午夜的数据 - 实际上它发生在之后。
干杯,
垫子
编辑:添加我的问题的图片,当我输入时,我看不到线程中的图片,所以我希望它们在正确的位置!
我的源数据示例,时间戳在“时间”列中。右侧的另一个日期列是我正在工作,所以至少我有轮班日期和实际时间。
Source data
以下是我的查询,这里有很多将源位置附加到一组预定义的属性,基本上我正在努力的是“时间”字段被导入,但在小数点所以我只是得到一个日期。我想将该时间附加到该日期,并具有另一个字段,即如上所述的班次日期。
let
// Removes unwanted characters.
CharsToRemove = List.Transform(33..45,47,58..126, each Character.FromNumber(_)),
// The query is all based on the current month's portion of the current 13wk.
Source = Location_Data,
// Set some data fields, not all are changed here as it affects later calculations.
#"Changed Type" = Table.TransformColumnTypes(Source,"Group Name", type text, "Name", type text, "Waste tonnes", type number, "Total Ore Tonnes", Int64.Type, "Dil cu_pct", type number, "Dil au", type number, "Dil ag", type number, "Dil fe_pct", type number, "Dil zn_pct", type number, "Density", type number, "Material", type text, "Type", type text, "Active from", Int64.Type, "Active to", Int64.Type, "Comments", type text),
// This steps add the MTD trucking data, and correlates it with our claim grades and density based on two fields, "Name" and "Material".
#"Merged Queries" = Table.NestedJoin(Source,"Name", "Material",LoadTrak_Data,"Load Origin", "Material","NewColumn",JoinKind.LeftOuter),
// This step expands the trucking data so we can work with the each column individually, such as truck ID, Date/time, Load Volume, etc.
#"Expanded NewColumn" = Table.ExpandTableColumn(#"Merged Queries", "NewColumn", "Record", "Time", "Dir.", "Operator", "Truck ID", "Load (m3)", "Truck Operator", "Crew", "Shift", "Material", "Load Origin", "Dumped At", "Day", "Shift Time", "Calc Shift", "Record", "Time", "Dir.", "Operator", "Truck ID", "Load (m3)", "Truck Operator", "Crew", "Shift", "Material.1", "Load Origin", "Dumped At", "Day", "Shift Time", "Calc Shift"),
#"Changed Type3" = Table.TransformColumnTypes(#"Expanded NewColumn","Time", type number),
#"Sorted Record low to high" = Table.Sort(#"Changed Type3","Record", Order.Ascending),
#"Added Error Volume Column" = Table.AddColumn(#"Sorted Record low to high", "Error Volume", each if [#"Load (m3)"] = null then "28.2" else null ),
#"Changed Error Volume to decimal number" = Table.TransformColumnTypes(#"Added Error Volume Column","Error Volume", type number),
#"Added Custom1" = Table.AddColumn(#"Changed Error Volume to decimal number", "DMT", each if [#"Dir."] = null then null else if [#"Load (m3)"] = null then ([Error Volume]*[Density] * 0.7) else [#"Load (m3)"] * [Density] * 0.7),
#"Added Custom2" = Table.AddColumn(#"Added Custom1", "Level Loaded", each if [Load Origin] = "Empty" then 0 else (Text.Start([Load Origin],4))),
#"Creates shift date" = Table.AddColumn(#"Added Custom2", "Shift Date", each if [Shift Time] is null then null else if [Shift Time] < 0.2916 then [Time] -1 else [Time]),
#"Added Custom" = Table.AddColumn(#"Creates shift date", "Correct location", each if [#"Dir."] = null then null else if ([Shift Date]) < ([Active from]) or ([Shift Date]) > (([Active to])+0.999999) then "No" else "Yes"),
#"Changed Type1" = Table.TransformColumnTypes(#"Added Custom","Level Loaded", type number),
#"Level Dumped" = Table.AddColumn(#"Changed Type1", "Level Dumped", each if [Dumped At] = "ROM" then 5270 else if [Dumped At] = "PAF" then 5170 else if [Dumped At] = "Paste" then 5270 else if [Dumped At] = "Waste" then 5270 else Text.Start([Dumped At], 4)),
#"Change Level Dumped to decimal number" = Table.TransformColumnTypes(#"Level Dumped","Level Dumped", type number),
#"Added TKMs column" = Table.AddColumn(#"Change Level Dumped to decimal number", "TKMs", each if [Dumped At] = "Waste" then (((([Level Dumped] - [Level Loaded])*7)+300)/1000 * [DMT]) else if [Dumped At] = "ROM" then (((([Level Dumped] - [Level Loaded])*7)+150)+300)/1000 * [DMT] else if [Dumped At] = "PAF" then (((([Level Dumped] - [Level Loaded])*7)+150)+300)/1000 * [DMT] else if [Dumped At] = "Paste" then (((([Level Dumped] - [Level Loaded])*7)+150)+300)/1000 * [DMT] else (([Level Dumped] - [Level Loaded])*7)/1000 * [DMT]),
#"Changed TKMs to decimal number" = Table.TransformColumnTypes(#"Added TKMs column","TKMs", type number),
#"Filtered Correct Location to remove incorrect duplicates" = Table.SelectRows(#"Changed TKMs to decimal number", each [Correct location] <> "No"),
#"Added load count helper column" = Table.AddColumn(#"Filtered Correct Location to remove incorrect duplicates", "Load", each if [Correct location] = "Yes" then 1 else ""),
#"Filtered Rows1" = Table.SelectRows(#"Added load count helper column", each true),
#"Filtered non-null Group Name rows" = Table.SelectRows(#"Filtered Rows1", each [Group Name] <> null and [Group Name] <> ""),
#"Converts date fields to type date" = Table.TransformColumnTypes(#"Filtered non-null Group Name rows","Active from", type date, "Active to", type date, "Time", type date, "Shift Date", type date),
#"Merged with Sched_13wk" = Table.NestedJoin(#"Converts date fields to type date","Name", "Material",Sched_13wk,"Name", "Material","Sched_13wk",JoinKind.LeftOuter),
#"Expanded Sched_13wk" = Table.ExpandTableColumn(#"Merged with Sched_13wk", "Sched_13wk", "Dil cu_pct", "Material", "Scheduled Tonnes", "Sched_13wk.Dil cu_pct", "Sched_13wk.Material", "Sched_13wk.Scheduled Tonnes"),
#"Changed Type2" = Table.TransformColumnTypes(#"Expanded Sched_13wk","Shift Time", type time, "Day", type date, "Time", type date)
in
#"Changed Type2"
Urgghhh 看起来很可怕,但不确定如何更好地包含它。大声笑,里面的注释是为了提醒我当我必须编辑它时我在做什么。还没有完成,想增加一个负载,这样如果我改变位置,坐在我椅子上的巢穴人就会知道查询中发生了什么!
这就是数据在查询输出中的样子,看到所有时间值都消失了。
Output with no time values.
好的,我真的希望这会有所帮助。正如你已经猜到的那样,我不是专业的程序员!
复制到我的 Excel 工作簿中的报告数据,输入附加信息然后查询 Report generated by software which is copied and pasted into workbook containing query.
【问题讨论】:
只要包含在单个值中的日期时间,应该没有问题。 2 个日期时间(次 24 )之间的差异是之间的小时数。也许datediff
函数将是 Poiwer Query 中的最佳方式。如果不了解您所拥有的和问题出在哪里,就不可能给您一个具体的答案。另外,m 建模语言与问题有何关联?
干杯,我正要下班,但明天会上传一些信息。还将研究 datediff 函数。
本站还有其他关于此的问题 - 需要搜索:前 4 天内类似的问题....
嗨,迈克,很遗憾您没有在评论中包含链接,因为我显然在发布之前没有找到该主题。我会尝试重新搜索,看看会发生什么。如果您有兴趣提供帮助,如果您能指导我,那就太好了!
我以为你写的数据来自CSV
文件。您的 Source Data
似乎是 Excel 表格。使用您在 csv 文件中显示的格式的日期/时间戳,我无法使用 Power Query 重现您的截断问题。您在Source Data
的屏幕截图中的Time
列中显示的是格式为General
的日期/时间戳,而不是日期/时间格式之一。请澄清您的问题,因为它的原因根本不清楚。
【参考方案1】:
问题可以通过使用 Power Query 从原始文本文件导入来解决。您应该能够解决时间戳问题,以及更轻松地格式化结果。
复制/粘贴通常不是处理此类问题的最佳方法。
【讨论】:
甜心,对于那些不把这类事情作为日常工作的人来说,让一个你素未谋面的人来帮忙,而不是只顾自己,真是太好了告诉您您做错的所有事情,然后急于按下投票按钮,甚至没有让您知道下一步该做什么!竖起大拇指成为一个 rad 伙计!以上是关于Powerquery 识别属于跨越两个连续日期的班次的时间戳的主要内容,如果未能解决你的问题,请参考以下文章
如何使用 PowerQuery 中的高级编辑器设置动态开始日期和结束日期
如果开始日期和结束日期跨越一个或多个月,则插入表格时日期格式会发生变化