Select to view content in your preferred language

Creating a new field using Data Expression in Dashboard

355
2
Jump to solution
09-10-2024 01:45 PM
LainieMcGannon
New Contributor

Hello! 

I am not sure this is even possible, but...

I am using a client's dataset which I can't edit, otherwise I would just create a new field in the actual data. I need to multiply one field by the percentage of another and display a table that shows GroupedBy status elements. Below is the code that I have so far...please let me know if there are any ideas. 

 

var fs = FeatureSetByPortalItem(
Portal('https://xyz'),
'05b99d65195e41x5b1f49436eea53bfb',
0,
['PermitCurrentStatus', 'TotalLand', 'UndividedInterest'],
false
);

var field1 = 'TotalLand',
var field2 = 'UndividedInterest',
var trueacreage = field2/100 * field1

return GroupBy(fs, ['PermitCurrentStatus', 'UndividedInterest','TotalLand'],
[{name: 'TotalLand_count', expression: 'TotalLand', statistic: 'SUM'},
{name: 'trueacres', expression: 'trueacreage', statistic: 'SUM'},
{name: 'UndividedInterestCount', expression: 'UndividedInterest', statistic: 'SUM'}]);

0 Kudos
2 Solutions

Accepted Solutions
MobiusSnake
MVP Regular Contributor

It's possible but I'm not sure if it can be done as concisely as your group-by function there.

I think you might need to iterate through your feature set, derive each feature's new attribute one-by-one, construct a new feature and add it to a list, then convert your list to a feature set.

Deriving a new feature and adding it to a list would look like this:

var featureList = [];
for (var feature in featureSet) {
  var newFeature = {
    attributes: {
      value1: feature.value1,
      value2: feature.value2,
      derived: feature.value1 / feature.value2
    }
  }
  Push(featureList, newFeature);
}

Once you've got your new features in a list, you can return your new feature set by defining its schema like this:

var returnFeatureSetDict = {
  fields: [
        { name: "value1", type: "esriFieldTypeInteger" },
        { name: "value2", type: "esriFieldTypeInteger" },
        { name: "derived", type: "esriFieldTypeDouble" }
    ],
    "geometryType": "",
    "features": featureList
};
return FeatureSet(Text(returnFeatureSetDict));

 

(I'm not actually testing this code as I'm writing it, I'm just typing it freehand, so it might have some bugs but should get the pattern across.)

There very well could be a quicker way to do this, but I think this should work at least.

View solution in original post

0 Kudos
LainieMcGannon
New Contributor

Thank you for your help! I played around with what you had told me and came up with something that actually worked! See script below. I really appreciate you taking the time to respond 🙂 

 

var fs = FeatureSetByPortalItem(
Portal('https://xyz/portal'),
'05b99d65195e41x5b1f49436eea53bfb',
0,
['PermitCurrentStatus', 'TotalLand', 'UndividedInterest'], 
false
);

// Define expressions for aggregation
var result = GroupBy(
fs,
['PermitCurrentStatus'], 
[
{
name: 'TotalLandSum',
expression: 'TotalLand',
statistic: 'SUM'
},
{
name: 'UndividedInterestSum',
expression: 'UndividedInterest',
statistic: 'SUM'
},
{
name: 'CalculatedValue',
expression: 'UndividedInterest / 100 * TotalLand',
statistic: 'SUM'
}
]
);

return result;

View solution in original post

0 Kudos
2 Replies
MobiusSnake
MVP Regular Contributor

It's possible but I'm not sure if it can be done as concisely as your group-by function there.

I think you might need to iterate through your feature set, derive each feature's new attribute one-by-one, construct a new feature and add it to a list, then convert your list to a feature set.

Deriving a new feature and adding it to a list would look like this:

var featureList = [];
for (var feature in featureSet) {
  var newFeature = {
    attributes: {
      value1: feature.value1,
      value2: feature.value2,
      derived: feature.value1 / feature.value2
    }
  }
  Push(featureList, newFeature);
}

Once you've got your new features in a list, you can return your new feature set by defining its schema like this:

var returnFeatureSetDict = {
  fields: [
        { name: "value1", type: "esriFieldTypeInteger" },
        { name: "value2", type: "esriFieldTypeInteger" },
        { name: "derived", type: "esriFieldTypeDouble" }
    ],
    "geometryType": "",
    "features": featureList
};
return FeatureSet(Text(returnFeatureSetDict));

 

(I'm not actually testing this code as I'm writing it, I'm just typing it freehand, so it might have some bugs but should get the pattern across.)

There very well could be a quicker way to do this, but I think this should work at least.

0 Kudos
LainieMcGannon
New Contributor

Thank you for your help! I played around with what you had told me and came up with something that actually worked! See script below. I really appreciate you taking the time to respond 🙂 

 

var fs = FeatureSetByPortalItem(
Portal('https://xyz/portal'),
'05b99d65195e41x5b1f49436eea53bfb',
0,
['PermitCurrentStatus', 'TotalLand', 'UndividedInterest'], 
false
);

// Define expressions for aggregation
var result = GroupBy(
fs,
['PermitCurrentStatus'], 
[
{
name: 'TotalLandSum',
expression: 'TotalLand',
statistic: 'SUM'
},
{
name: 'UndividedInterestSum',
expression: 'UndividedInterest',
statistic: 'SUM'
},
{
name: 'CalculatedValue',
expression: 'UndividedInterest / 100 * TotalLand',
statistic: 'SUM'
}
]
);

return result;

0 Kudos