Sum values from two arrays

I have two arrays:

allocatedHours: {{id:"", totalHours:12, week:fn!date(2020, 07, 31)}; {id:"", totalHours:12, week:fn!date(2020, 08, 7)}; {id:"", totalHours:48, week:fn!date(2020, 08, 14)}; {id:"", totalHours:48, week:fn!date(2020, 08, 21)}}

proposedHours: {{id:"", totalHours:36, week:fn!date(2020, 07, 31)}; {id:"", totalHours:42, week:fn!date(2020, 08, 7)}; {id:"", totalHours:48, week:fn!date(2020, 08, 14)}; {id:"", totalHours:54, week:fn!date(2020, 08, 21)}}

I am wanting to return TotalHours where allocatedHours.week = proposedHours.week.

When I do a forEach on weeks, it only ever returns the first value for each week, ie {31/7/2020, 48}, {7/8/2020, 48}, {14/8/2020,48}, {21/8/2020, 48).

How do I make if return the correct value for each week, ie {31/7/2020, 48}, {7/8/2020, 54}, {14/8/2020,96}, {21/8/2020, 102).

  Discussion posts and replies are publicly visible

Parents
  • 0
    Certified Lead Developer

    Does this work for you:

    a!localVariables(
    local!proposedHours: {{id:"", totalHours:36, week:fn!date(2020, 07, 31)}; {id:"", totalHours:42, week:fn!date(2020, 08, 7)}; {id:"", totalHours:48, week:fn!date(2020, 08, 14)}; {id:"", totalHours:54, week:fn!date(2020, 08, 21)}},

    local!allocatedHours: {{id:"", totalHours:12, week:fn!date(2020, 07, 31)}; {id:"", totalHours:12, week:fn!date(2020, 08, 7)}; {id:"", totalHours:48, week:fn!date(2020, 08, 14)}; {id:"", totalHours:48, week:fn!date(2020, 08, 21)}},

    local!uniqueDates: union(
    local!proposedHours.week,
    local!allocatedHours.week
    ),

    a!forEach(

    items:local!uniqueDates,
    expression: a!localVariables(
    local!proposedHours: index(local!proposedHours, whereContains(fv!item, toDate(local!proposedHours.week))).totalHours,
    local!allocatedHours: index(local!allocatedHours, whereContains(fv!item, toDate(local!allocatedHours.week))).totalHours,
    {week: fv!item, totalHours: sum(local!allocatedHours, local!proposedHours)}
    )
    )
    )

Reply
  • 0
    Certified Lead Developer

    Does this work for you:

    a!localVariables(
    local!proposedHours: {{id:"", totalHours:36, week:fn!date(2020, 07, 31)}; {id:"", totalHours:42, week:fn!date(2020, 08, 7)}; {id:"", totalHours:48, week:fn!date(2020, 08, 14)}; {id:"", totalHours:54, week:fn!date(2020, 08, 21)}},

    local!allocatedHours: {{id:"", totalHours:12, week:fn!date(2020, 07, 31)}; {id:"", totalHours:12, week:fn!date(2020, 08, 7)}; {id:"", totalHours:48, week:fn!date(2020, 08, 14)}; {id:"", totalHours:48, week:fn!date(2020, 08, 21)}},

    local!uniqueDates: union(
    local!proposedHours.week,
    local!allocatedHours.week
    ),

    a!forEach(

    items:local!uniqueDates,
    expression: a!localVariables(
    local!proposedHours: index(local!proposedHours, whereContains(fv!item, toDate(local!proposedHours.week))).totalHours,
    local!allocatedHours: index(local!allocatedHours, whereContains(fv!item, toDate(local!allocatedHours.week))).totalHours,
    {week: fv!item, totalHours: sum(local!allocatedHours, local!proposedHours)}
    )
    )
    )

Children
No Data