I have a hosted feature layer with multiple records per day for a feature. How would I return only the most recent record per day?
Here is my code, but returns multiple records per day:
Solved! Go to Solution.
Here is an example that gives you both. You won't need to change much.
You will need to change:
Decide whether you want firstFeature or lastFeature on line 22
// Sort ascending is important
var fset = OrderBy($layer, 'service_date ASC');
var currentDateGroup;
var _date;
var firstFeatures = [];
var lastFeatures = [];
var popupString;
for (var f in fset) {
_date = Text(f.service_date, 'YMMDD');
if (currentDateGroup != _date) {
currentDateGroup = _date;
Push(firstFeatures, f);
Push(lastFeatures, f);
} else {
Pop(lastFeatures);
Push(lastFeatures, f);
};
};
var features = lastFeatures; // choose to use firstFeatures, or last features here
for (var fIdx in features) {
popupString += Text(features[fIdx].service_date, 'MMM DD Y, hh:mm A') + TextFormatting.NewLine;
};
return popupString;
Output
Newest feature for each day based on datetime field (aka lastFeature)
Feb 28 2023, 05:01 PM
Mar 01 2023, 03:59 PM
Mar 02 2023, 05:03 PM
Mar 08 2023, 04:58 PM
Mar 10 2023, 08:51 AM
Oldest feature for each day based on datetime field(aka firstFeature)
Feb 28 2023, 01:13 PM
Mar 01 2023, 08:13 AM
Mar 02 2023, 10:09 AM
Mar 08 2023, 09:22 AM
Mar 10 2023, 07:36 AM
Hello,
You can do away with the for loop, it is not needed to get first or last record by date.
var dateOfInterest = Today()
// Get First Record by Datetime
var mowhistorytimeSorted = First(Filter(OrderBy(mowhistory, 'lastserviced ASC'), 'lastserviced = @dateOfInterest'))
// Get Last Record by Datetime
var mowhistorytimeSorted = First(Filter(OrderBy(mowhistory, 'lastserviced DESC'), 'lastserviced = @dateOfInterest'))
@JustinReynolds Thank you for your feedback! I am actually looking to get a record back for every day there is a record, but only the last/most recent. ie:
Dec 2 2022 2:30 PM
Dec 1 2022 2:45 PM
Nov 30 2022 4:29 PM
Here is an example that gives you both. You won't need to change much.
You will need to change:
Decide whether you want firstFeature or lastFeature on line 22
// Sort ascending is important
var fset = OrderBy($layer, 'service_date ASC');
var currentDateGroup;
var _date;
var firstFeatures = [];
var lastFeatures = [];
var popupString;
for (var f in fset) {
_date = Text(f.service_date, 'YMMDD');
if (currentDateGroup != _date) {
currentDateGroup = _date;
Push(firstFeatures, f);
Push(lastFeatures, f);
} else {
Pop(lastFeatures);
Push(lastFeatures, f);
};
};
var features = lastFeatures; // choose to use firstFeatures, or last features here
for (var fIdx in features) {
popupString += Text(features[fIdx].service_date, 'MMM DD Y, hh:mm A') + TextFormatting.NewLine;
};
return popupString;
Output
Newest feature for each day based on datetime field (aka lastFeature)
Feb 28 2023, 05:01 PM
Mar 01 2023, 03:59 PM
Mar 02 2023, 05:03 PM
Mar 08 2023, 04:58 PM
Mar 10 2023, 08:51 AM
Oldest feature for each day based on datetime field(aka firstFeature)
Feb 28 2023, 01:13 PM
Mar 01 2023, 08:13 AM
Mar 02 2023, 10:09 AM
Mar 08 2023, 09:22 AM
Mar 10 2023, 07:36 AM
@JustinReynolds Bingo. This worked like a charm. Much appreciated, thank you!
Here is my final and functioning code:
var mowhistory = Overlaps(FeatureSetByPortalItem(Portal('https://www.arcgis.com'),"00871a3866be4ceaa11f7841b43cf82a",0), $feature);
var fset = OrderBy(mowhistory, 'lastserviced DSC');
var currentDateGroup;
var _date;
var firstFeatures = [];
var lastFeatures = [];
var popupString;
for (var f in fset) {
_date = Text(f.lastserviced, 'YMMDD');
if (currentDateGroup != _date) {
currentDateGroup = _date;
Push(firstFeatures, f);
Push(lastFeatures, f);
} else {
Pop(lastFeatures);
Push(lastFeatures, f);
};
};
var features = lastFeatures; // choose to use firstFeatures, or last features here
for (var fIdx in features) {
popupString += Text(features[fIdx].lastserviced, 'MMM DD Y, hh:mm A') + TextFormatting.NewLine;
};
return popupString;
Use First to select the first item from your sorted list
var myfeature = First(Orderby(mowhistory, 'lastserviced DSC'));
return text(myfeature.lastserviced, 'MMM DD Y, hh:mm A')
I am actually looking to get a record back for every day there is a record, but only the last/most recent. ie:
Dec 2 2022 2:30 PM
Dec 1 2022 2:45 PM
Nov 30 2022 4:29 PM