var my_portal = Portal("https://www.arcgis.com/"); // Step 1: get the FMT table as a FeatureSet var FMT = FeatureSetByPortalItem( my_portal, "", 11, ["*"], false ); // Step 2: Group FMT by fmt_id and sum the budget var FMT_Grouped = GroupBy( FMT, ['fmt_FY'], [ { name: 'FMT_Totals', expression: 'fmt_budget', statistic: 'SUM' } ] ); // Step 3: get the MPF table as a FeatureSet var MPF = FeatureSetByPortalItem( my_portal, "", 2, ["mpf_p_mp_prj_id","mpf_fund_id","mpf_fund_amount"], false ); // Step 4: Group MPF by mpf_fund_id and sum the budget var MPF_Grouped = GroupBy( MPF, ['mpf_fund_id'], [ { name: 'MPF_Totals', expression: 'mpf_fund_amount', statistic: 'SUM' } ] ); //Step 5: create a dictionary to store the values var combinedDict = { fields: [ { name: "FY", type: "esriFieldTypeString" }, { name: "FMT_Budget", type: "esriFieldTypeDouble" } { name: "MPF_Budget", type: "esriFieldTypeDouble" } ], geometryType: "", features: [], }; //Step 6: iterate through the Grouped feature sets and add to dictionary var i = 0; for (var cur_f_FY in FMT_Grouped) { var FMT_FY = cur_f_FY["fmt_FY"] var FMT_B = cur_f_FY["FMT_Totals"] for (var cur_m_FY in MPF_Grouped) { var MPF_FY = Concatenate("FY",Right(cur_m_FY['mpf_fund_id'],2)) Filter(MPF_Grouped, "MPF_FY = '" + FMT_FY + "'")){ combinedDict.features[i++] = { attributes: { FY: Concatenate("FY",Right(f['fmt_id'],2)), FMT_Budget: f['FMT_Totals'], } } } //Step 7: return the dictionary as a Feature Set return FeatureSet(Text(combinedDict));