Getting data group by field and the latest one

Certified Lead Developer

Hi Champs,

I need to fetch only unique value from a dictionary with the latest data. Below is the sample of the output

Here you can see color highlighted ones are same value of training name. So instead of 5 dictionary I want only 3 with unique training name . For unique data I want to fetch the latest of duplicate data.

  Discussion posts and replies are publicly visible

  • 0
    Certified Lead Developer

    Did you take a look at the union function?

    https://docs.appian.com/suite/help/23.4/fnc_set_union.html

    union(
        local!trainingData.trainingName,
        local!trainingData.trainingName,
      )

  • 0
    Certified Senior Developer

    Hello  

    I hope this is what you have asked. (Sorry for the old reply. Didn't catch the last part)

    a!localVariables(
      local!data: a!queryRecordType(
        recordType: 'recordType!{813c8d12-c70d-4373-b9d3-bbb60dbb1d56}Employee Detail',
        fields: {},
        pagingInfo: a!pagingInfo(startIndex: 1, batchSize: 100)
      ).data,
      /*Replace with your rule*/
      local!lastName: a!map(
        lastName: index(
          local!data,
          'recordType!{813c8d12-c70d-4373-b9d3-bbb60dbb1d56}Employee Detail.fields.{c9452bf3-7a5b-4464-bdea-a86ec4c64484}lastname',
          null
        ),
        id: index(
          local!data,
          'recordType!{813c8d12-c70d-4373-b9d3-bbb60dbb1d56}Employee Detail.fields.{a79c1c0e-7077-44ca-85f6-de6f4b0d969c}id',
          null
        ),
        
      ),
      /*Replace the record and reference type*/
      local!uniqueLastName: union(
        local!lastName.lastName,
        local!lastName.lastName
      ),
      local!latestIds: a!flatten(
        a!forEach(
          items: local!uniqueLastName,
          expression: {
            max(
              index(
                local!lastName.id,
                wherecontains(
                  tostring(fv!item),
                  touniformstring(local!lastName.lastName)
                ),
                null
              )
            )
          }
        )
      ),
      a!forEach(
        items: local!latestIds,
        expression: index(
          local!data,
          wherecontains(
            tointeger(fv!item),
            tointeger(
              index(
                local!data,
                'recordType!{813c8d12-c70d-4373-b9d3-bbb60dbb1d56}Employee Detail.fields.{a79c1c0e-7077-44ca-85f6-de6f4b0d969c}id',
                null
              )
            )
          ),
          null
        )
      )
    )
    /*Tried out in my apprach a better and efficient way might also be available. Till then this might do the job*/


    Please update the data and the indexes to work according to your data.

  • 0
    Certified Associate Developer

    Hello  ,
    I think this piece of code can fetch the latest of the duplicate data.

    a!localVariables(
      local!data: {
        {
          trainigNotes: "testt",
          trainingDoc: 48195,
          trainingName: "SPPT1",
          id: 2,
          hcpId: 4
        },
        {
          trainigNotes: "testingDoubtData",
          trainingDoc: 48196,
          trainingName: "SPPT1",
          id: 3,
          hcpId: 4
        },
        {
          trainigNotes: "unique",
          trainingDoc: 132595,
          trainingName: "SPPT5",
          id: 8509,
          hcpId: 4
        },
        {
          trainigNotes: "cep2",
          trainingDoc: 132594,
          trainingName: "SPPT2",
          id: 8510,
          hcpId: 4
        },
        {
          trainigNotes: "cep1",
          trainingDoc: 132593,
          trainingName: "SPPT2",
          id: 8511,
          hcpId: 4
        },
    
      },
      local!trainingNames: index(local!data, "trainingName", ""),
      local!uniqueNames: union(local!trainingNames, local!trainingNames),
      local!indexes: a!forEach(
        items: local!uniqueNames,
        expression: index(
          wherecontains(
            fv!item,
            touniformstring(index(local!data, "trainingName", ""))
          ),
          1,
          {}
        )
      ),
      index(local!data, local!indexes, {})
    )