It's because you iterate over tbl, not over the filtered inspections. Your expressions returns the date of the last entry in the inspection table.
var tbl = FeatureSetByName($datastore,"TestPoints", ['UNIQUE_ID', 'Inspection_Date'])
var uniqueid = $feature["Unique_ID"]
var sql = "UNIQUE_ID= @uniqueid"
Console(sql)
// Filter by UNIQUE_ID and sort the result by date
var inspections = OrderBy(Filter(tbl, sql), "Inspection_Date")
var cnt = Count(inspections)
// return early if there are no inspections
if(cnt == 0) {
return "No inspections"
}
// get all inspections in a list
var history = [
`${cnt} inspections`
]
for (var inspection in inspections) {
var insp_date = Text(inspection.Inspection_Date, 'MM/DD/YYYY')
Push(history, `Inspected: ${insp_date}`)
}
// return the concatenated list
return Concatenate(history, TextFormatting.NewLine)
If you only want to return the latest inspection, you can do it like this:
var tbl = FeatureSetByName($datastore,"TestPoints", ['UNIQUE_ID', 'Inspection_Date'])
var uniqueid = $feature["Unique_ID"]
var sql = "UNIQUE_ID= @uniqueid"
Console(sql)
// Filter by UNIQUE_ID and sort the result by date DESCENDING
var inspections = OrderBy(Filter(tbl, sql), "Inspection_Date DESC")
var cnt = Count(inspections)
// return early if there are no inspections
if(cnt == 0) {
return "No inspections"
}
// get the last inspection date
var insp_date = Text(First(inspections).Inspection_Date, 'MM/DD/YYYY')
var history = [
`${cnt} inspections`,
`Last inspection: ${insp_date}`
]
return Concatenate(history, TextFormatting.NewLine)
Have a great day!
Johannes