Select to view content in your preferred language

Dashboard use of FeatureSetByPortalItem

2461
12
Jump to solution
02-08-2023 07:28 AM
PSGeo
by
Regular Contributor

Dear all,

I am trying to do a dashboard in AGOL. Using the example 2 of this link:

However, to work I have to do something that I really don't know.

I need to to clean the "name" from unnecessary code, using this:

Trim(Split(DomainName($feature, 'name'), '_')[-1])

However, it does not accept global variables... therefore $feature - does not work. so after some changes

I end up with this:

var fs = FeatureSetByPortalItem(Portal('https://xxxxx.maps.arcgis.com'), 'a73a2a04e8b24aa7a5a514193a3057a0', 0, [Trim(Split( 'name', '_')[-1]),'DELIV_DATE','sqKm'], false);

 

But I have no idea how this can work for the remaining code of that link.

Can someone suggest me some fixing.

cheers

PS

0 Kudos
1 Solution

Accepted Solutions
jcarlson
MVP Esteemed Contributor

You're not putting it into it, you're wrapping the filter around the existing expression.

var fs = Filter(
    FeatureSetByPortalItem(
        Portal('https://xxxxx.maps.arcgis.com'),
        'a73a2a04e8b24aa7a5a514193a3057a0',
        0,
        ['name','DELIV_DATE','sqKm'],
        false
    ),
    "local_type = 'some value'"
);
- Josh Carlson
Kendall County GIS

View solution in original post

12 Replies
jcarlson
MVP Esteemed Contributor

The FeatureSetByPortalItem only takes a list of field names as defined in the underlying table. So while you can submit your Trim(...) function, unless there happens to be a column in the table with the resulting string, it won't return anything. You'll have to simply request the name column and work with it further.

var fs = FeatureSetByPortalItem(
    Portal('https://xxxxx.maps.arcgis.com'),
    'a73a2a04e8b24aa7a5a514193a3057a0',
    0,
    ['name','DELIV_DATE','sqKm'],
    false
);

Once you have your FeatureSet, you can use other functions that output FeatureSets, like Filter, GroupBy, and Distinct. If these functions cannot accomplish what you need, then you have to iterate over your FeatureSet using a for loop, in which you do whatever you need to each feature individually, and then build a new FeatureSet "from scratch".

The former, using functions that output FeatureSets, are much faster, and should be used if you can. Generally, these will make use of SQL expressions that are submitted to the feature service.

If I'm reading this right, you have a domain name with underscores, and you'd like the text that follows the last underscore? It would be helpful to have some idea of the contents of this field. If there's consistent formatting (which, with a domain, may be the case), then you ought to be able to get something like Distinct to give you your results.

- Josh Carlson
Kendall County GIS
PSGeo
by
Regular Contributor

Hi Josh,

in fact there is no domains. it is just text: v321_Paris; v258_Madrid; a5741_Berlin; g9865_Paris; etc.

I understood is that after the:

var fs = FeatureSetByPortalItem(
    Portal('https://xxxxx.maps.arcgis.com'),
    'a73a2a04e8b24aa7a5a514193a3057a0',
    0,
    ['name','DELIV_DATE','sqKm'],
    false
);

 I have to add something like this:

return GroupBy(fs, [Trim(Split( 'name', '_')[-1])], [ 
 {name: 'total_cites', expression: Trim(Split( 'name', '_')[-1]), statistic: 'COUNT' }
 ]);

 

Or not really.

cheers

PS

0 Kudos
jcarlson
MVP Esteemed Contributor

You're on the right track, but the expression part of GroupBy needs to be SQL, not Arcade. Will there ever be more than 1 underscore in the name field?

- Josh Carlson
Kendall County GIS
Kepa
by Esri Contributor
Esri Contributor

Hi there, just to add up... Wouldn't it be easier to add a new field, calculate (aka clean it up) with the Trim/Split functions and then just apply GroupBy function in Dashboard's new expression Arcade console?

Regards,

0 Kudos
PSGeo
by
Regular Contributor
Hi,
I cannot touch the original table. Pity that is not possible to add a virtual column for these cases.

Still possible to do, using just arcade or I really need to try sql?


0 Kudos
jcarlson
MVP Esteemed Contributor

Note that to use GroupBy, Distinct, Filter, etc., you are using SQL, just sort of "inside" of Arcade.

I'll assume that there's only a single underscore in the field. Try this:

 

var fs = FeatureSetByPortalItem(
    Portal('https://xxxxx.maps.arcgis.com'),
    'a73a2a04e8b24aa7a5a514193a3057a0',
    0,
    ['name','DELIV_DATE','sqKm'],
    false
);

var name_sql = `SUBSTRING(
    name,
    POSITION('_', name) + 1,
    CHAR_LENGTH(name) - POSITION('_', name)
)`

return Distinct(
    fs,
    [
        {name: 'DELIV_DATE', expression: 'DELIV_DATE'},
        {name: 'sqKm', expression: 'sqKm'},
        {name: 'name_cleaned', expression: name_sql}
    ]
)

 

 

If you went the Arcade-only route, it would look like this:

 

var fs = FeatureSetByPortalItem(
    Portal('https://xxxxx.maps.arcgis.com'),
    'a73a2a04e8b24aa7a5a514193a3057a0',
    0,
    ['name','DELIV_DATE','sqKm'],
    false
);

var out_dict = {
    fields: [
        {name: 'DELIV_DATE', type: 'esriFieldTypeDate'},
        {name: 'sqKm', type: 'esriFieldTypeInteger'},
        {name: 'name', type: 'esriFieldTypeString'}
    ],
    geometryType: '',
    features: []
}

for (var f in fs) {
    var clean_name = Trim(Split(f['name'], '_')[-1])

    Push(
        out_dict['features'],
        {
            attributes: {
                name: clean_name,
                sqKm: f['sqKm'],
                DELIV_DATE: f['DELIV_DATE']
            }
        }
    )
}

return FeatureSet(Text(out_dict))

But keep in mind that this option will be slower, especially if there are a large number of features in the layer.

 

- Josh Carlson
Kendall County GIS
PSGeo
by
Regular Contributor

Hi,

Just tried both parts. Not a good result. 

"Feature layer must be created with either a url or a source"

 

Sadly today this is the max I can do it... Something urgent come in. Tomorrow I will try to understand the error.

thx for the support

cheers

Pedro

0 Kudos
PSGeo
by
Regular Contributor

Hi it works, more or less.

indeed it retrieves the fields, but for some reason the DELIV_DATE is empty (in the original table is filled and it is a date field).

I also had to add an extra field named local_type with this domains: City, Town, Village, Hamlet. Because I just want to show the Cities and filter the others out. And I was hoping to use the "filter" tool in the "list" configurator for it, but this field also appears empty. I have add the next line but maybe should be something else because it is a domain field?:

{name: 'local_type', type: 'esriFieldTypeString'} 

 

cheers

PS

0 Kudos
jcarlson
MVP Esteemed Contributor

Ah, right. In Data Expressions, dates are handled funny. Try

DELIV_DATE: Number(f['DELIV_DATE'])

instead. That should bring the correct date values into the output.

For the other field, it really depends on what the domain is, whether the codes are truly strings, etc. But if you just want to filter the data on the way in, you can wrap the initial FeatureSet function in a Filter:

var fs = Filter(
    FeatureSetByPortalItem(...),
    "local_type = 'some value'"
)

Just replace 'some value' with the appropriate code for that field.

- Josh Carlson
Kendall County GIS