我正在使用 power bi (M) 中的电源查询编辑器。
我想将基于如图所示格式的一列文本的持续时间转换为以天为单位的十进制数。
作为示例,假设该列有 2 行:
我尝试过的:
let
durationText = [Full Total Downtime],
splitText = Text.Split(durationText, " "),
totalDays =
List.Sum(
List.Transform(
splitText,
each
let
numericValue = try Number.FromText(Text.BeforeDelimiter(_, " ")) otherwise null, // Extract numeric value
unit = Text.AfterDelimiter(_, " "), // Extract unit (e.g., "Day", "Hour", "Minute", "Second")
multiplier =
if unit = "Day" then 1
else if unit = "Hour" then 1/24
else if unit = "Minute" then 1/(24*60)
else if unit = "Second" then 1/(24*60*60)
else 0
in
if numericValue <> null then numericValue * multiplier else 0
)
)
in
totalDays
为了制作独特的格式,但它没有给我正确的结果。
有趣的问题
这会拉出格式中两行之间的持续时间
let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Convert" = Table.TransformColumns(Source,{{"Column1",
each let a=_,
b = Text.Replace(a,"Days","*24*60*60+"),
c = Text.Replace(b,"Day","*24*60*60+"),
d = Text.Replace(c,"Hours","*60*60+"),
e = Text.Replace(d,"Hour","*60*60+"),
f = Text.Replace(e,"Minutes","*60+"),
g = Text.Replace(f,"Minute","*60+"),
h = Text.Replace(g,"Seconds","*1+"),
i = Text.Replace(h,"Second","*1+"),
j = i &"0"
in Expression.Evaluate(j), type number}}),
#"Added Custom" = Table.AddColumn(#"Convert", "Duration", each #duration(0,0,0,List.Max(#"Convert"[Column1])-List.Min(#"Convert"[Column1]))),
#"CleanUp" = Table.FirstN(#"Added Custom",1),
#"Removed Columns" = Table.RemoveColumns(CleanUp,{"Column1"})
in #"Removed Columns"