Select to view content in your preferred language

How to get a sum of duration of similar attributes?

780
8
03-02-2023 05:39 AM
Labels (2)
Davec43
Occasional Contributor

I have a table that I need to find a total duration of attributes.

NameDuration
ABCDEFG00:00:11
ABCDEFG00:10:00
ABCDEFG00:01:00

 

How do I calculate the Duration and put the total in a new field?

0 Kudos
8 Replies
Robert_LeClair
Esri Notable Contributor

Dave - I know this tool may not be what you're looking for, but Summary Statistics (Analysis), would provide a sum of the Duration values BUT it creates a new output table in your geodatabase vs. in the existing feature class table.  You could use a Join of the output table to the feature class table though if that would suffice.

Davec43
Occasional Contributor

That looks like the path to go down but I keep getting this error when I try to use Sum for statistic type.

ERROR 000315 Field Duration only supports statistic types MIN, MAX, MEAN, COUNT, FIRST, LAST, UNIQUE.

0 Kudos
DavidPike
MVP Frequent Contributor

Is duration a string?  You'd be best creating a new 'seconds' field and doing a field calculate using python.

Not tested it at all but something along the lines of : (I know it would be less verbose as a code block)

(float(duration.split(":")[0]) * 3600) + (float(duration.split(":")[1]) * 60) +(float(duration.split(":")[2]))

 

0 Kudos
Davec43
Occasional Contributor

Something along the lines of this? Duration is the new field and SUM_Total_Seconds is the seconds field

Sum_Total_Seconds = [f.type for f in arcpy.ListFields(SUM_Total_Seconds) if f.name == sDep][0]
arcpy.management.CalculateField("mStatistics", "Duration", "!Duration!", "PYTHON3", '(float(SUM_Total_Seconds.split(":")[0]) * 3600) + (float(SUM_Total_Seconds.split(":")[1]) * 60) +(float(SUM_Total_Seconds.split(":")[2]))', "TEXT", "NO_ENFORCE_DOMAINS")
0 Kudos
Davec43
Occasional Contributor

I keep getting an error when I try this:

Davec43_0-1677846939073.png

ERROR 002717: Invalid Arcade expression, Arcade error: Close parenthesis expected, Script line: 1

But I don't see where you'd close the parenthesis?

0 Kudos
DavidPike
MVP Frequent Contributor

Change your expression type to Python

0 Kudos
Davec43
Occasional Contributor

Davec43_0-1677848158279.png

 

I get: ERROR 000539: Traceback (most recent call last):
File "<expression>", line 1, in <module>
AttributeError: 'float' object has no attribute 'split'

 

0 Kudos
DavidPike
MVP Frequent Contributor

You're confusing the fields in the tool.  Field Name (existing or new) = the new field you want to create (SUM_total_seconds)

you need to use !Duration! in the script.

at the moment you're trying to use split on a value which doesn't exist and also the wrong field type, and then populate a float into a string field which is all wrong.

Best to just create a new field of type float, then use field calculator as it will be simpler.

 

0 Kudos