Select to view content in your preferred language

Create a Table with Inspection Year and Count based on Date Field

124
2
Jump to solution
a week ago
MattStevens2
New Contributor

Hi,

I'm trying to create a table that looks like the table below based on a date field called Inspect_Date. I just want to extract the year from the record and then have a count of those records. It would look like below.

I know the arcade code to pull the layer data and then code to turn it into a dictionary. Where I'm struggling is turning that dictionary into the table below.

 

Inspection YearInspection Count
2024442
20258

 

 

Code so far after getting the data:

var yearCounts = {}

// Count inspections by year
for (var f in fs) {
    var inspDate = f["Inspect_Date"]
    if (!IsEmpty(inspDate)) {
        var year_1 = Text(Year(inspDate))
        if (HasKey(yearCounts, year_1)) {
            yearCounts[year_1] += 1
        } else {
            yearCounts[year_1] = 1
        }
    }
}

// Construct output features using Feature()
var features = []
for (var y in yearCounts) {
    var attrs = {
        "Year": y,
        "Inspection_Count": yearCounts[y]
    }
    Push(features, Feature(attrs))
}

// Return a proper FeatureSet
var schem = {
    fields: [
        { name: "Year", type: "esriFieldTypeString" },
        { name: "Inspection_Count", type: "esriFieldTypeInteger" }
    ],
    geometryType: "",
    features: features
}

return FeatureSet(schem)
0 Kudos
1 Solution

Accepted Solutions
KenBuja
MVP Esteemed Contributor

You just need to make a minor change on line 23

var yearCounts = {}

// Count inspections by year
for (var f in fs) {
    var inspDate = f["Inspect_Date"]
    if (!IsEmpty(inspDate)) {
        var year_1 = Text(Year(inspDate))
        if (HasKey(yearCounts, year_1)) {
            yearCounts[year_1] += 1
        } else {
            yearCounts[year_1] = 1
        }
    }
}

// Construct output features using Feature()
var features = []
for (var y in yearCounts) {
    var attrs = {
        "Year": y,
        "Inspection_Count": yearCounts[y]
    }
    Push(features, {attributes: attrs})
}

// Return a proper FeatureSet
var schem = {
    fields: [
        { name: "Year", type: "esriFieldTypeString" },
        { name: "Inspection_Count", type: "esriFieldTypeInteger" }
    ],
    geometryType: "",
    features: features
}

return FeatureSet(schem)

 

View solution in original post

0 Kudos
2 Replies
KenBuja
MVP Esteemed Contributor

You just need to make a minor change on line 23

var yearCounts = {}

// Count inspections by year
for (var f in fs) {
    var inspDate = f["Inspect_Date"]
    if (!IsEmpty(inspDate)) {
        var year_1 = Text(Year(inspDate))
        if (HasKey(yearCounts, year_1)) {
            yearCounts[year_1] += 1
        } else {
            yearCounts[year_1] = 1
        }
    }
}

// Construct output features using Feature()
var features = []
for (var y in yearCounts) {
    var attrs = {
        "Year": y,
        "Inspection_Count": yearCounts[y]
    }
    Push(features, {attributes: attrs})
}

// Return a proper FeatureSet
var schem = {
    fields: [
        { name: "Year", type: "esriFieldTypeString" },
        { name: "Inspection_Count", type: "esriFieldTypeInteger" }
    ],
    geometryType: "",
    features: features
}

return FeatureSet(schem)

 

0 Kudos
MattStevens2
New Contributor

Thank you!!!

0 Kudos