1 of 1 people found this helpful
Paul Webb wrote:
Just wanted to see if there is a better/shorter way to write it and if there are any gotchas I might be missing
If it looks ugly, it can almost always be improved … e.g.
Let ( [
~i = 1827 ; // your initial response field
~d = SerialIncrement ( "0" ; Div ( ~i ; 1440 ) ) ;
~dR = Mod ( ~i ; 1440 ) ;
~h = SerialIncrement ( "00" ; Div ( ~dR ; 60 ) ) ;
~m = SerialIncrement ( "00" ; Mod ( ~dR ; 60 ) )
Case ( ~d ; ~d & ":" ) & Case ( ~h or ~d ; ~h & ":" ) & ~m
Even if you don't use Div() and Mod(), you should definitely start getting familiar with Let().
Thanks erolst. I had cleaned it up some with the Let function. I try to use the Let function on just about every calculation. I've never used the Mod function before. I'll give your setup a try. Thanks!
1 of 1 people found this helpful
As Erolst says, at the very least make use of the Let ( ) function. In your calculation FM makes at least five and possibly as many as ten calls on the SR::INITIAL_RESPONSE_MIN field. By declaring this field as a variable within a Let ( ) statement reduces this to one call. If that field happens to be itself a calculation (eg. a subtraction of start and finish timestamps) this change alone will make the calculation faster.
If you study your current calc further you can see that there are ten instances of the same sum—SR::INITIAL_RESPONSE_MIN / 60—so you could add further elegance and clarity, not to mention ease of maintenance, by declaring this as a second variable.
Convert the number to h:mm:ss and remove preceding 0:
Let ( s = GetAsTime ( Div ( SR::INITIAL_RESPONSE_MIN ; 24*60 ) * 60*60 + Mod ( SR::INITIAL_RESPONSE_MIN ; 24*60 ) ) ;
Case ( Left ( s ; 2 ) = "0:" ; Replace ( s ; 1 ; 2 ; "" ) ; s )
Thanks guys. I think I originally missunderstood the variables of the LET function. I originally thought that when the variable was applied later in the function it input exactly what you typed in and not the result of the variable. My understanding now is that it applies the result. So if you set a variable to @math = 1+1 then later in the function when it calls for @math it applies 2 and not 1+1. Is that correct or make sense?
I cleaned up the function as best I could with the below. Seems to work just fine but I'm only pushing 120K records through it currently.
Let ( [
@Field = dash_SR__SRsByDateRangeCreate::zsAvgInitResponseMin ;
@TimeField = Round ( @Field ; 0 ) ;
@Convert = ( @TimeField / 60 ) ;
@Days = Int ( @Convert / 24 ) ;
@Hours = Int ( ( ( @Convert / 24 ) - @Days ) * 24 ) ;
@Minutes = Round ( ( ( @Convert - Int ( @Convert ) ) * 60 ) ; 1 ) ] ;
// DAYS SECTION //
@Days < 1 ; "" ;
@Days & GetAsText ( ":" )
// HOURS SECTION //
@Hours < 10 ; 0 ; "" ) &
& ":" &
// MINUTES SECTION //
@Minutes < 10 ; "0" ; "" ) &
Thanks but this looks like you are calculating H:M:S and I need D:H:M
GetAsText ( ":" )
Which is the difference with a simple ":" ?
If you have time in minutes, I'd leverage the FileMaker date/time engine as below. People tend to forget that both Date ( month ; day ; year ) and Time ( hours ; minutes ; seconds ) automatically convert to internal date engine values and can then be easily manipulated after that. (i.e. Date ( 2 ; 1 + 28 ; someYear ) will give either Feb 29 or Mar 1 as expected. The same goes for times like Time ( 12 + 16 ; 30 + 45 ; 0 ) = 29:15:00. TimeStamps work like this too.
; theTime= Time ( 0 ; initialValue ; 0 ) // that's Time ( hours ; minutes ; seconds ) and in this case will yield 30:27:00
; theDays= Int ( Hour ( theTime ) / 24 )
; remainingTime= theTime - ( theDays * 60 * 60 * 24 ) // or you could use MOD ( theTime ; 60 * 60 * 24 )
; remainingHours= Right ( "00" & Hour ( remainingTime ) ; 2 ) // the zero-padded hours
; remainingMinutes= Right ( "00" & Minute ( remainingTime ) ; 2 ) // the zero-padded minutes
; finalResult= theDays & ":" & remainingHours & ":" & remainingMinutes
for the above, the result is : "1:06:27"
One could condense this calculation into a single line, but it would be harder for me to read and debug. That's why I love Let()
-- Drew Tenenholz
The calculation 'converts' the D part of the value to H part in H:M:S for simplize text conversion.
(GetAsText(GetAsTime(H;M;S) ) automatically add preceding '0' on M and S part.)
I tested it on FM13 that the result is same as your expression in first post.
I like it! Simple enough I almost understand it. I need to study it a bit more.
Thanks for the education from everyone. Hope to meet you guys at DevCon this year.
I know I'm late here, but
Int ( m / 24 ) & " days " & Left ( Time ( 0 ; Mod ( m ; 24 ) ; 0 ) ; 5 )
where m = 5000 yields "208 days 00:08".
I don't like 208:00:08 because it looks confusing for me - at least.
Huslik Verlag GmbH • Bgm.-Widmeier-Str. 42 • 86179 Augsburg, DE
CEO Winfried Huslik - HRB Augsburg 12386 - VAT-Id. DE127485099
Phone +49 821 565606, Fax +49 821 565001, Email email@example.com
Verify your FileMaker Pro files with FMDiff - http://fmdiff.com
FileMaker Developer Conference July 28-31 2014 • San Antonio TX