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

  • I should point out that these are actually CDTs

    Allocated Hours

    Proposed Hours

  • 0
    Certified Senior Developer
    in reply to ireneb

    Hi,

    Please try and check if the below code works. I have created separate locals for Actual Hours and Proposed Hours. You can replace them as per your requirement.

    load(
      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!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!checkIfAllocatedAndPropEqual: a!forEach(
        items: local!allocatedHours,
        expression: local!allocatedHours.week[fv!index] = local!proposedHours.week[fv!index]
      ),
      local!sumIfEqual:a!forEach(
        items:local!checkIfAllocatedAndPropEqual,
        expression:if(
          local!checkIfAllocatedAndPropEqual[fv!index],
        concat(local!allocatedHours.week[fv!index]," , ",sum(
          tointeger(local!allocatedHours.totalHours[fv!index])+
          tointeger(local!proposedHours.totalHours[fv!index]))),
          {})
      ),
      local!sumIfEqual
    )

  • Thanks for that.  Unfortunately it doesn't work when passing the data as a CDT.

    Thanks

    Irene

  • 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)}
    )
    )
    )

  • 0
    Certified Lead Developer
    in reply to ireneb
    Unfortunately it doesn't work

    Can you be more specific / provide some detail about what you mean by "doesn't work"?  Does it throw an error?  Return the wrong data?  And what modification did you make to the example code (which to me, looks to be working)?  When you run just the sample code as-written, are the results as you'd expect?

  • Thanks all for your suggestions.  The answer was simply [fv!index].