Select to view content in your preferred language

Calculation Rule FOR(VAR F IN .....)

372
2
Jump to solution
02-28-2024 07:28 AM
Labels (3)
Robswann
Occasional Contributor

Can someone please help me with the following below, I am not sure what I am doing wrong, I have tested this in the arcade playground, but cant get it to work with my data. 

I am trying pull data from a table and determine the percentage contacted

 

VAR Owners = FEATURESETBYNAME($DATASTORE,"Owners",["TractID","Contacted"])
VAR ID = $feature.TractID
VAR OwnerID = Filter(owners,"TractID=@ID")

VAR Cont = 0
VAR NCont =0

FOR(VAR F in OwnerID){
If(OwnerID[F] == 'Yes'){
Cont +=1}
}
FOR(VAR F in OwnerID){
If(OwnerID[F] == 'No'){
NCont +=1}
}

Return CONT/(Cont+NCont)

0 Kudos
1 Solution

Accepted Solutions
KenBuja
MVP Esteemed Contributor

Your loops (and you only need one instead of two) aren't getting the attribute from the feature correctly. It should be like this

var Owners = FEATURESETBYNAME($DATASTORE,"Owners",["TractID","Contacted"])
var ID = $feature.TractID
var OwnerID = Filter(Owners,"TractID=@ID")

var Cont = 0
var NCont =0

for(var f in OwnerID) {
  if(f.Contracted == 'Yes') {
    Cont +=1
  } else if(f.Contracted == 'No') {
    NCont +=1
  }
}
if (Cont == 0 && NCont == 0) return null
return Cont/(Cont+NCont)

I also added a check to make sure you're not dividing by zero.

View solution in original post

0 Kudos
2 Replies
KenBuja
MVP Esteemed Contributor

Your loops (and you only need one instead of two) aren't getting the attribute from the feature correctly. It should be like this

var Owners = FEATURESETBYNAME($DATASTORE,"Owners",["TractID","Contacted"])
var ID = $feature.TractID
var OwnerID = Filter(Owners,"TractID=@ID")

var Cont = 0
var NCont =0

for(var f in OwnerID) {
  if(f.Contracted == 'Yes') {
    Cont +=1
  } else if(f.Contracted == 'No') {
    NCont +=1
  }
}
if (Cont == 0 && NCont == 0) return null
return Cont/(Cont+NCont)

I also added a check to make sure you're not dividing by zero.

0 Kudos
Robswann
Occasional Contributor

Thank you very much!

0 Kudos