Select to view content in your preferred language

Arcade expression to populate field based on values in multiple other fields

1407
10
Jump to solution
11-06-2024 11:54 PM
Labels (2)
AndrewHankinson
Regular Contributor

Hi, 

I have an arcade expression which populates a field base on all the variables of 3 other fields, I have written this in a very long winded way but it works. 

Currently the example is like this.

if($feature.Feat_1 == 'A' && $feature.Feat_2 == 'A' && $feature.3 == 'A')
{return 'Y'}

if($feature.Feat_1 == 'A' && $feature.Feat_2 == 'A' && $feature.3 == 'B')
{return 'Y'}

This repeats with every single possible variation . . . . . 

else {return 'Unknown'}

I was thinking that creating a variable for each of the fields would work but I'm not getting anywhere with my limited knowledge of arcade and after some further reading over similar needs on the forum I'm unsure of if I should be using a When or Expects function??

Any help would be greatly appreciated. 

0 Kudos
1 Solution

Accepted Solutions
KenBuja
MVP Esteemed Contributor

If I understand your logic correctly, this should work

var sizes = ["1m", "1m2", "2m3m", "3m4m", "4m"];
var angles = ["Sv", "V"];

When ($feature.Feat_width == "1m" || $feature.Feat_depth == "4m" || IndexOf(["St", "M"], $feature.Slope_degrees) > -1, "N",
      IndexOf(sizes, $feature.Feat_width) > -1 && IndexOf(sizes, $feature.Feat_depth) > -1 && IndexOf(angles, $feature.Slope_degrees) > -1, "Y",
     "Unknown");

View solution in original post

10 Replies
KenBuja
MVP Esteemed Contributor

What are the possible values of the three fields? Could you go into more detail about what types of returns you'd expect besides "Y" and "Unknown"?

0 Kudos
dgiersz_cuyahoga
Frequent Contributor

It's hard to give a better answer without knowing the data, but there are different ways to do what you're doing.  Here is one using When():

 

var feat1 = $feature.Feat_1
var feat2 = $feature.Feat_2
var feat3 = $feature.Feat_3

var featlist = feat1 + feat2 + feat3

return When(featlist == 'AAA', 'Y',
featlist == 'AAB', 'Y',
featlist == 'ABA', 'N',
.......,
'defaultvalue')

 

 

 

#CLE #sloth
0 Kudos
AndrewHankinson
Regular Contributor

Thanks for the reply, I was wondering what you use to get the screenshot of the expression? I use notebooks in ArcPro, the field calculator or Notepad++ but none of these look like your screenshot.

0 Kudos
dgiersz_cuyahoga
Frequent Contributor

That's not a screenshot. When you post there is an 'Expand toolbar' option (the 3 dots at the end).

From here you can insert code formatting using the '</>' button.

#CLE #sloth
0 Kudos
AndrewHankinson
Regular Contributor

Morning, 

Sorry, there in this current example there are 3 fields each of which have multiple possible data entries.

Field 1 - Width - 1m, 1m2, 2m3m, 3m4m, 4m

Field 2 - Depth - 1m, 1m2, 2m3m, 3m4m, 4m

Field 3 - Side angle - St, M, Sv, V

The returns I'm looking for would ideally be anything that is 1m wide or 4m deep or has a side angle of St or M would be 'N'.

Then any combination of the above would be 'Y' else 'Unknown'.

0 Kudos
KenBuja
MVP Esteemed Contributor

If I understand your logic correctly, this should work

var sizes = ["1m", "1m2", "2m3m", "3m4m", "4m"];
var angles = ["Sv", "V"];

When ($feature.Feat_width == "1m" || $feature.Feat_depth == "4m" || IndexOf(["St", "M"], $feature.Slope_degrees) > -1, "N",
      IndexOf(sizes, $feature.Feat_width) > -1 && IndexOf(sizes, $feature.Feat_depth) > -1 && IndexOf(angles, $feature.Slope_degrees) > -1, "Y",
     "Unknown");
AndrewHankinson
Regular Contributor

Amazing, that's great thank you, just need an "m" for the "1m2m" option and it worked great!


 

0 Kudos
AndrewHankinson
Regular Contributor

Hi again, 

I thought I'd take your example and try to apply it to another field, which has more variables and more outcomes. 

What I have done is clearly wrong and probably oversimplified and so doesn't do what I'd like. It works, as in there are no errors but I only get one output, which is correct, and all the other values are 'Unknown'. As opposed to all fields being populated based on the variables. 

I've attached the expression. Would you be able to advise?

0 Kudos
KenBuja
MVP Esteemed Contributor

When posting code, please use the "Insert/Edit code sample" button instead of attaching it as a text file. It makes reviewing the code easier.

You're using the When function with an implicit return, meaning it will return just the last executable statement. You'll have to combine them into one When function

var PBwidth = ["3m4m","4m"];
var PBdepth = ["1m","1m2m", "2m3m","3m4m","4m"];
var PBbasesub = ["BDP","VDP"];
var SBwidth = ["3m4m","4m"];
var SBdepth = ["1m","1m2m", "2m3m", "3m4m","4m"];
var SBbasesub = ["BM","VM","VSP","VSM","VSP"];
var PDwidth = ["1m","1m2m", "2m3m"];
var PDdepth = ["1m","1m2m", "2m3m", "3m4m","4m"];
var PDbasesub = ["BDP","VDP"];
var TDwidth = ["2m3m"];
var TDdepth = ["2m3m", "3m4m","4m"];
var TDbasesub = ["BDP","VDP"];
var TDflowpres = ["FP"];
var SDwidth = ["1m","1m2m", "2m3m"];
var SDdepth = ["1m","1m2m", "2m3m", "3m4m","4m"];
var SDbasesub = ["VSM","VM","BM"];
var MDwidth = ["1m","1m2m", "2m3m"];
var MDdepth = ["1m","1m2m", "2m3m", "3m4m","4m"];
var MDbasesub = ["VSP","PSS","PSD"];

When (IndexOf(PBwidth, $feature.Feat_width) > -1 && IndexOf(PBdepth, $feature.Feat_depth) > -1 && IndexOf(PBbasesub, $feature.Base_sub) > -1, "PB",
      IndexOf(SBwidth, $feature.Feat_width) > -1 && IndexOf(SBdepth, $feature.Feat_depth) > -1 && IndexOf(SBbasesub, $feature.Base_sub) > -1, "SB", 
      IndexOf(PDwidth, $feature.Feat_width) > -1 && IndexOf(PDdepth, $feature.Feat_depth) > -1 && IndexOf(PDbasesub, $feature.Base_sub) > -1, "PD",
      IndexOf(TDwidth, $feature.Feat_width) > -1 && IndexOf(TDdepth, $feature.Feat_depth) > -1 && IndexOf(TDbasesub, $feature.Base_sub) > -1 && IndexOf(TDflowpres, $feature.flow_pres) > -1, "TD",
      IndexOf(SDwidth, $feature.Feat_width) > -1 && IndexOf(SDdepth, $feature.Feat_depth) > -1 && IndexOf(SDbasesub, $feature.Base_sub) > -1, "SD",
      IndexOf(MDwidth, $feature.Feat_width) > -1 && IndexOf(MDdepth, $feature.Feat_depth) > -1 && IndexOf(MDbasesub, $feature.Base_sub) > -1, "MD",
      "Unknown")

You could also use individual if functions, with a final return "Unknown" when none of the conditions are met, if that makes it easier to read

var PBwidth = ["3m4m","4m"];
var PBdepth = ["1m","1m2m", "2m3m","3m4m","4m"];
var PBbasesub = ["BDP","VDP"];

if (IndexOf(PBwidth, $feature.Feat_width) > -1 && IndexOf(PBdepth, $feature.Feat_depth) > -1 && IndexOf(PBbasesub, $feature.Base_sub) > -1) return "PB";

var SBwidth = ["3m4m","4m"];
var SBdepth = ["1m","1m2m", "2m3m", "3m4m","4m"];
var SBbasesub = ["BM","VM","VSP","VSM","VSP"];

if (IndexOf(SBwidth, $feature.Feat_width) > -1 && IndexOf(SBdepth, $feature.Feat_depth) > -1 && IndexOf(SBbasesub, $feature.Base_sub) > -1) return "SB";

var PDwidth = ["1m","1m2m", "2m3m"];
var PDdepth = ["1m","1m2m", "2m3m", "3m4m","4m"];
var PDbasesub = ["BDP","VDP"];

if (IndexOf(PDwidth, $feature.Feat_width) > -1 && IndexOf(PDdepth, $feature.Feat_depth) > -1 && IndexOf(PDbasesub, $feature.Base_sub) > -1) return "PD";

var TDwidth = ["2m3m"];
var TDdepth = ["2m3m", "3m4m","4m"];
var TDbasesub = ["BDP","VDP"];
var TDflowpres = ["FP"];

if (IndexOf(TDwidth, $feature.Feat_width) > -1 && IndexOf(TDdepth, $feature.Feat_depth) > -1 && IndexOf(TDbasesub, $feature.Base_sub) > -1 && IndexOf(TDflowpres, $feature.flow_pres) > -1) return "TD";

var SDwidth = ["1m","1m2m", "2m3m"];
var SDdepth = ["1m","1m2m", "2m3m", "3m4m","4m"];
var SDbasesub = ["VSM","VM","BM"];

if (IndexOf(SDwidth, $feature.Feat_width) > -1 && IndexOf(SDdepth, $feature.Feat_depth) > -1 && IndexOf(SDbasesub, $feature.Base_sub) > -1) return "SD";

var MDwidth = ["1m","1m2m", "2m3m"];
var MDdepth = ["1m","1m2m", "2m3m", "3m4m","4m"];
var MDbasesub = ["VSP","PSS","PSD"];

if (IndexOf(MDwidth, $feature.Feat_width) > -1 && IndexOf(MDdepth, $feature.Feat_depth) > -1 && IndexOf(MDbasesub, $feature.Base_sub) > -1) return "MD";

return "Unknown";

 

 

0 Kudos