Forum Discussion
Convert Text Duration to Type Duration
- 4 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
Anonymous I realize this is DAX but you should be able to use the same pattern replacing the DAX functions with equivalent Power Query functions. I will work on the conversion if I find the time. Also, could you paste that data as text?
Column =
VAR __Duration = [Ticket Duration]
VAR __DayLocation = SEARCH("day",__Duration,,0)
VAR __HourLocation = SEARCH("hour",__Duration,,0)
VAR __MinuteLocation = SEARCH("minute",__Duration,,0)
VAR __Days = IF(__DayLocation,VALUE(LEFT(__Duration,__DayLocation - 1)),0)
VAR __DaySeparator = IF(__DayLocation<>0,SEARCH(" ",__Duration,__DayLocation,0),1)
VAR __HourSeparator =
SWITCH(TRUE(),
__DayLocation<>0,SEARCH(" ",__Duration,__DaySeparator+1,1),
__HourLocation=0,1,
SEARCH(" ",__Duration)
)
VAR __Hours = IF(__HourLocation=0,0,VALUE(MID(__Duration,__DaySeparator,__HourSeparator - __DaySeparator + 1)))
VAR __MinuteSeparator = IF(__HourSeparator=1,1,SEARCH(" ",__Duration,__HourSeparator+1,0))
VAR __LastSeparator = SEARCH(" ",__Duration,__MinuteSeparator+1,0)
VAR __Minutes = VALUE(MID(__Duration,__MinuteSeparator,__LastSeparator - __MinuteSeparator + 1))
RETURN
__Days * 24 + __Hours + __Minutes/100
Also, this was for day, hour and minute but not seconds so there will need to be some adjustments.
Hi Greg_Deckler, thanks for your help. I'll await your response. As requested, a sample in text format:
1 Day 8 Hours 30 Minutes 25 seconds
13 Minutes 46 seconds
5 Minutes 47 seconds
3 Hours 57 Minutes 9 seconds
1 Hour 37 Minutes 14 seconds
9 Minutes 12 seconds