Power BI 将文本 mm:ss 转换为时间

huangapple go评论68阅读模式
英文:

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.PositionOfList.Count),并添加适当的前缀(使用 Text.End 来确保前导零):

现在,你可以更改新添加列的数据类型为 DurationTime

英文:

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):

Power BI 将文本 mm:ss 转换为时间

Now you can change the data type of the newly added column to Duration or Time.

Power BI 将文本 mm:ss 转换为时间

huangapple
  • 本文由 发表于 2023年5月25日 01:42:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/76326153.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定