How it works...

In this case, our Duration column is expressed in the number of milliseconds. We aggregate these milliseconds using the SUM function and store the value in a variable called __Duration. We can now calculate the hours by dividing __Duration by 3600000 milliseconds in an hour. We use the INT function to return just the integer portion of this division. We store this value in the __Hours variable. 

Now that we know how many hours are represented in __Duration, we can compute the remainder of the duration in milliseconds without those hours. We do this by subtracting __Hours multiplied by 3600000 milliseconds in an hour from our initial __Duration variable and then finding the remainder of this value divided by 3600000 milliseconds in an hour. We subtract this number of hours multiplied by 3600000 milliseconds in an hour and store this value in the __HoursRemainder variable.

Once we have our __HoursRemainder variable calculated, if we take the remainder of dividing __HoursRemainder by the 3600000 milliseconds that are in an hour and take the integer portion using INT, and then divide that remainder by 60000 milliseconds in a minute, this provides us with the number of minutes, __Minutes. We can calculate the number of seconds and milliseconds, __Seconds and __Milliseconds, respectively, via similar math. 

We now need to return our __Hours, __Minutes, __Seconds, and __Milliseconds as a number with each component in its proper "place." So, if we know that we are going to be using a format of HH:mm:ss:000, then the milliseconds start in the ones place, seconds start in the thousandths place, minutes start in the hundred thousandths place, and the hours start in the 10 millionths place (ignore the colons). Hence, we multiply __Hours by 10 million, __Minutes by one hundred thousand, __Seconds by one thousand, and __Milliseconds by nothing. Hence, if we have 2 hours, 3 minutes, 5 seconds, and 23 milliseconds, the return value would be 20,305,023 (20,000,000 + 300,000 + 5,000 + 23).

The magic then comes when we apply the custom display formatting developed by Chelsie Eiden. We specify a custom format of 00:00:00:000. The specification of 00 means to use a leading zero to pad our number if it is a single digit. Hence, for our example of 2 hours, 3 minutes, 5 seconds, and 23 milliseconds, that results in the number 20,305,023, which is displayed as 02:03:05:023—or 2 hours, 3 minutes, 5 seconds, and 23 milliseconds!