Forum Discussion
Convert Text Duration to Type Duration
- 5 years ago
Anonymous Ah well, couple folks beat me to the fun. I personally like Jakinta's solution mainly because it doesn't use regex which is like, why is that still a thing? Honestly though, CNENFRNL it is pretty cool that you can use regex in Power Query it's just I thought regex went the way of Perl and that everyone was happy about that fact.
Anyway, not to be outdone and left with a hacky DAX solution, I created one that is just as functional and flexible as the elegant solutions by Jakinta and CNENFRNL:
Duration = VAR __Separator = " " VAR __SearchText = MAX('Table'[Text Duration]) VAR __Len = LEN(__SearchText) VAR __Count = __Len - LEN(SUBSTITUTE(__SearchText,__Separator,"")) + 1 VAR __Table = ADDCOLUMNS( ADDCOLUMNS( GENERATESERIES(1,__Count,1), "__Word", VAR __Text = SUBSTITUTE(__SearchText,__Separator,"|",IF([Value]=1,1,[Value]-1)) VAR __Start = SWITCH(TRUE(), __Count = 1,1, [Value] = 1,1, FIND("|",__Text)+1 ) VAR __End = SWITCH(TRUE(), __Count = 1,__Len, [Value] = 1,FIND("|",__Text) - 1, [Value] = __Count,__Len, FIND(__Separator,__Text,__Start)-1 ) VAR __Word = MID(__Text,__Start,__End - __Start + 1) RETURN __Word ), "__Key", SWITCH(TRUE(), SEARCH("day",[__Word],,0)>0,"day", SEARCH("hour",[__Word],,0)>0,"hour", SEARCH("minute",[__Word],,0)>0,"minute", SEARCH("second",[__Word],,0)>0,"second", BLANK() ) ) VAR __Days = MAXX(FILTER(__Table,[Value] = MAXX(FILTER(__Table,[__Key]="day"),[Value]) - 1),[__Word])+0 VAR __Hours = MAXX(FILTER(__Table,[Value] = MAXX(FILTER(__Table,[__Key]="hour"),[Value]) - 1),[__Word])+0 VAR __Minutes = MAXX(FILTER(__Table,[Value] = MAXX(FILTER(__Table,[__Key]="minute"),[Value]) - 1),[__Word])+0 VAR __Seconds = MAXX(FILTER(__Table,[Value] = MAXX(FILTER(__Table,[__Key]="second"),[Value]) - 1),[__Word])+0 RETURN __Days * 1000000 + __Hours * 10000 + __Minutes * 100 + __SecondsMost of the code is just Text to Table, which I should have thought about to begin with. And posted to the gallery here: Text Duration Conversion - Microsoft Power BI Community
A generic solution regardless of single/plural forms or lower/upper cases
let
RE = (regex as text, str as text) =>
let
html = "<script>var regex = " & regex & "; var str = """ & str & """; var res = str.match(regex); document.write(res)</script>",
res = Web.Page(html)[Data]{0}[Children]{0}[Children]{1}[Text]{0}
in res,
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("VYyxDkAwFEV/5aazwWtV2Q0Wk1E6CBIGJMrg71FN6XrPuadpGKFoT2Qo12MzEDGqaTn2wYBLmKFbl94wHd2e8CRJAyI/oFD/gHBRqbyRh00rQCjMjlMSCLk/Eg/SnGO0bXLzu6Jvz2ezWXJVpvUF", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Text_Dur = _t]),
RegEx = Table.AddColumn(Source, "Num_Dur", each RE("/\d+ [A-Z]/gi", [Text_Dur])),
#"Converted Duration" = Table.TransformColumns(RegEx, {"Num_Dur", each let pair = List.Zip(List.Transform(Text.Split(_, ","), each Text.Split(_, " "))), rd = Record.FromList(List.Transform(pair{0}, Number.From), List.Transform(pair{1}, Text.Upper)) in #duration(Record.FieldOrDefault(rd, "D", 0), Record.FieldOrDefault(rd, "H", 0), Record.FieldOrDefault(rd, "M", 0), Record.FieldOrDefault(rd, "S", 0))})
in
#"Converted Duration"