Select to view content in your preferred language

Help with Arcade expression (percentage change)

1002
4
02-11-2022 01:48 AM
JoshNock1
Occasional Contributor

Hi,

I am looking for some assistance with an arcade expression to use on ArcPro to find the percentage change of a dataset month by month.

I have provided an example dataset below.

LocationGrand TotalDate
London2Dec_2021
New York7Dec_2021
Paris8Dec_2021
London3Nov_2021
New York7Nov_2021
Paris3Nov_2021
London1Oct_2021
New York4Oct_2021
Paris9Oct_2021

 

I believe the result should be as below

LocationGrand TotalDatePercentage change (%)
London2Dec_2021-33.33
New York7Dec_20210
Paris8Dec_2021167.67
London3Nov_2021200
New York7Nov_202175
Paris3Nov_2021-66.67
London1Oct_2021n/a
New York4Oct_2021n/a
Paris9Oct_2021n/a

 

The general formula I used is below, but looking for a way to automate this as I have thousands of rows of data

((Nov_2021)-(Dec_2021))/(Nov_2021))*100

Many thanks

0 Kudos
4 Replies
DuncanHornby
MVP Notable Contributor

Can a location exist more than once within a single month?

0 Kudos
JoshNock1
Occasional Contributor

Hi Duncan, no each location is unique for the month.

0 Kudos
Robert_LeClair
Esri Notable Contributor

One option to automate this is create a model in ModelBuilder using your inputs and the Calculate Field GP tool.  You can either use Python or Arcade expression for the Calculate Field GP tool parameter.  To automate it, export it out from ModelBuilder as a Python script and associate the *.py script with a Basic Task in Windows Task Scheduler to run every x amount of time.  You can also schedule a model to run regularly too.

0 Kudos
DuncanHornby
MVP Notable Contributor

The first step is to simplify your input otherwise you would need some complex logic to identify as you step through the table you have gone from Paris Dec_2021 to London Nov_2021.

If your input is truly structured as you have shown then you could very easily explode out this table using the Split by attributes tool.

DuncanHornby_0-1644836755175.png

This creates as many tables as you have locations, the output of one such table would be:

DuncanHornby_1-1644836814401.png

Depending upon your input structure you may need to sort the table into a correct date sequence.

Add a new field of type double to record your change then run the following python code in a field calculate tool

DuncanHornby_2-1644838982804.png

 

Code block is:

l = list() # remember lists start at index zero
with arcpy.da.SearchCursor("London","GrandTotal") as cur:
    for row in cur:
        l.append(row[0])
        
def offset(i,currentTotal):
    global l
    if i < len(l):
        prev_month = l[i]
        calc = ((currentTotal - prev_month) / prev_month)*100
        return calc

 

In the example above I have hardwired it to read the table "London" but you could easily swap that out with an inline variable substitution from a model iterator as suggested by @Robert_LeClair 

0 Kudos