英文:
Power BI convert text mm:ss to time
问题
将文本从分钟:秒转换为时间数据类型。
我有一个包含时间(运行时间)的列,格式为(h:)mm:ss,
如何将其转换为数据类型"time",因为给定的函数总是期望有小时部分,而我大多数情况下没有。
我尝试过使用duration.FromText,但输入格式不适用。自动的时间解析也不行... 它在mm:ss部分失败。
= Table.AddColumn(#"Added Custom", "Parse", each Time.From(DateTimeZone.From([Tijd])), type time)
会产生许多错误。
英文:
Convert a text from minutes:seconds to datatype time
I have a column with time (running time) in format (h:)mm:ss
how can i convert this to a datatype "time" because given functions always expect the hours which i most of the time not have.
i've tried duration.FromText but the input format doesnot apply. Nor the automatic Time parse... it fails on the mm:ss part.
= Table.AddColumn(#"Added Custom", "Parse", each Time.From(DateTimeZone.From([Tijd])), type time)
gives a lot of errors
答案1
得分: 2
以下是已翻译的内容:
你没有展示数据的样子,所以我会假设每个值都可以是以下之一:
- h:mm:ss - 小时、分钟和秒,其中 mm 和 ss 介于 0 和 59 之间,对于单个数字的值,前面有一个前导零(例如 1:01:05);
- m:ss - 分钟和秒,其中 ss 介于 0 和 59 之间,对于单个数字的值,前面有一个前导零(例如 1:05);
- s - 仅秒钟(例如 5)。
在这种情况下,你可以在更改数据类型之前重新格式化数据。添加一个自定义列,如下所示:
if List.Count(Text.PositionOf([Time], ":", Occurrence.All)) = 0 then
"0:00:" & Text.End("0" & [Time], 2)
else if List.Count(Text.PositionOf([Time], ":", Occurrence.All)) = 1 then
"0:" & Text.End("0" & [Time], 5)
else
[Time]
它将计算输入字符串中 :
出现的次数(使用 Text.PositionOf 和 List.Count),并添加适当的前缀(使用 Text.End 来确保前导零):
现在,你可以更改新添加列的数据类型为 Duration
或 Time
。
英文:
You didn't showed how your data looks like, so I will assume each value can be one of these:
- h:mm:ss - hours, minutes and seconds, where mm and ss are between 0 and 59, with a leading zeros for the single digit values (e.g. 1:01:05);
- m:ss - minutes and seconds, where ss is between 0 and 59, with a leading zeros for the single digit values (e.g. 1:05);
- s - only seconds (e.g. 5).
In this case, one thing you can do, is to re-format the data, before changing its type. Add a custom column like this:
if List.Count(Text.PositionOf([Time], ":", Occurrence.All)) = 0 then
"0:00:" & Text.End("0" & [Time], 2)
else if List.Count(Text.PositionOf([Time], ":", Occurrence.All)) = 1 then
"0:" & Text.End("0" & [Time], 5)
else
[Time]
which will count how many times :
occurs in the input string (using Text.PositionOf and List.Count), and add suitable prefix (taking care for the leading zeroes with Text.End):
Now you can change the data type of the newly added column to Duration
or Time
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论