I’m trying to create a table using Arcade that only shows the highest frequency read for each [Sample Date, Site ID] but while also showing the ‘variant_name’ field in the table.
I was able to create something like this but I don’t know how to integrate “Variant_name” in the arcade code below because I’m not using it for any calculations. Can you let me know how to proceed ?
It looks like it got the job done but I need to integrate “Variant_name” I just don’t know where to put it.
Code used:
var fs = FeatureSetByPortalItem(Portal('https://arcgis.com/'), '0f37132f7d4c4a1bb0f0b1b985dbae1b', 0, ['*'], false);
return GroupBy(fs, ['Sample_Date','Site_ID','Site_Name'],
[{name: 'Estimated_Frequency_Reads', expression: 'Estimated_Frequency_Reads', statistic: 'MAX' }] );
I feel like @JohannesLindner would be great with this
Just answered on the Idea you posted. Ideas are for improvements you want to propose. Let's talk about this question here.
You have to join the Variant field to the grouped Featureset. There is no native function to join a field, so you have to do it yourself:
// group by date and site, getting the max frequency
var grouped_fs = GroupBy(fs, ['Sample_Date', 'Site_ID'], [{name: 'Estimated_Frequency_Reads', expression: 'Estimated_Frequency_Reads', statistic: 'MAX' }] )
// create an empty featureset for the join with variant
var join_fields = [
{name: "Variant", type: "esriFieldTypeString"}
]
var out_fs = {
geometryType: "",
fields: Splice(Schema(grouped_fs).fields, join_fields),
features: []
}
// join with the original fs to get the variant
for(var f in grouped_fs) {
var d = f.Sample_Date
var s = f.Site_ID
var f = f.Estimated_Frequency_Reads
var join = First(Filter(fs, "Sample_Date = @d AND Site_ID = @s AND Estimated_Frequency_Reads = @f"))
Push(out_fs.features, {attributes:{Sample_Date: Number(d), Site_ID: s, Estimated_Frequency_Reads: f, Variant: join.Variant}})
}
return Featureset(Text(out_fs))
This expression uses Filter() in a loop, which isn't very performant. If the expression is slow, copy the Memorize() function from this blog post and apply it to your Featureset:
var fs = Memorize(FeaturesetByPortalItem(...))
Hello @JohannesLindner ,
I'm trying a Test execution error and I'm trying to fix it but It hasn't worked yet. I'm wondering if it has to to something at this location. Can you please let me know if I'm in the right track ?
I attached the schema of my data below where the field name that needs to be added is called 'Variant_Name'
Possible error sources:
Try this:
// group by date and site, getting the max frequency
var grouped_fs = GroupBy(fs, ['Sample_Date','Site_ID'], [{name: 'Estimated_Frequency_Reads', expression: 'Estimated_Frequency_Reads', statistic: 'MAX' }] )
// create an ampty featureset for the join with variant
var join_fields = [
{name: "Variant_Name", type: "esriFieldTypeString"}
]
var out_fs = {
geometryType: "",
fields: Splice(Schema(grouped_fs).fields, join_fields),
features: []
}
// join with the original fs to get the variant
for(var f in grouped_fs) {
var d = f.Sample_Date
var s = f.Site_ID
var freq = f.Estimated_Frequency_Reads
var join = First(Filter(fs, "Sample_Date = @d AND Site_ID = @s AND Estimated_Frequency_Reads = @freq"))
Push(out_fs.features, {attributes:{Sample_Date: Number(d), Site_ID: s, Estimated_Frequency_Reads: freq, Variant_Name: join.Variant_Name}})
}
return Featureset(Text(out_fs))
If that still doesn't work, I'd need access to your actual data.