Select to view content in your preferred language

python expression

1143
3
09-22-2020 03:02 AM
GregorMitzlaff
New Contributor

Hello everybody,

in the picture you can see an attribute table of an heat meter. The number of the heat meter is 11250423. The heat status is given monthly. Now I want to make an expression to get the consumption of every month.

in theory I have to do sth like this:

y_(i+1)-y_(i) = consumption

Month 1 (january): y_(1+1)-y_(1) = consumption of January

Month 2 (feb): y_(2+1)-y_(2) = consumption of February

...

Month 12 (dec): y_(12+1)-y_(12) = consumption of December

does anybody know, how to write an expression in arcgis? I want to make a new field to calculate.

Thanks for helping.

Greets from Germany!

0 Kudos
3 Replies
JoshuaBixby
MVP Esteemed Contributor

What application/environment are you working in?  Is the screenshot an ArcGIS Pro table?

0 Kudos
DavidPike
MVP Frequent Contributor

Id say order it descending then have a codeblock that gets the value for the row in WAERMEMENGE, stores it then does the calculation in the next row. I've not tested the below, and its been ages since ive done this so it's very likely horrifically wrong, but it may give you an idea.

##pre-logic:

def calculate_stuff(field):

    global previous_value

    

    result = field - previous value

    previous_value = field

    return result

##code window:

calculate_stuff(!WAERMEMENGE!)

0 Kudos
Arne_Gelfert
Frequent Contributor

Gregor,

I'm sure what you're trying to accomplish can be done in field calculator. In my experience, the more massaging I have to do with data, the more I want to do it near the source. Usually that's fastest and I find it more transparent. Don't know where you're getting your data. But this would be easy to do in SQL:

SELECT month(Datum) AS [MONTH], year(Datum) AS [YEAR], SUM(Wärmemenge) AS TOTAL
FROM <YOUR DATA SOURCE>
GROUP BY month(Datum), year(Datum)
-- ORDER BY [MONTH], [YEAR]

So admittedly, this may not work in in your situation but if it does and you can create a query layer or database view, I bet it would perform better.

Schöne Grüße

0 Kudos