Removing Duplicates with uniqBy

  • Updated

This function allows you to filter out duplicates when creating a list by using the uniqBy function. For more information on filtering, please see Pre-defined and Ad Hoc Filters.

Filtering to Remove Duplicates

When creating certain lists or tables, you might run into duplicates. For example, generating a table of officers with titles, where individuals hold multiple officer titles in the company.

Sample Dataset

Leslie Knope - Secretary (confirmed)
Ron Swanson - President (confirmed)
Tom Haverford - Chief Technology Officer (incoming)
Leslie Knope - Treasurer (confirmed)

Without using the uniqBy function, the list of officers would appear in the order they were entered into the system and the following would result:

 

Input

{#officers}{name}

{office}{/}

Output

Leslie Knope

Secretary

Ron Swanson

President

Tom Haverford

Chief Technology Officer

Leslie Knope

Treasurer

To capture only unique officers by name and remove duplicates, we will use uniqBy:'name', which will result in the following:

 

Input

{#officers | uniqBy:'name'}{name}

{office}{/}

Output

Leslie Knope

Secretary

Ron Swanson

President

Tom Haverford

Chief Technology Officer

Note that in the above example, only the first officer title held by a unique individual is captured. This is because the {office} variable does not loop through all of the offices held. To capture all offices held by a unique individual, an additional collection {#officesHeld} would have to be included. For more information on collection loops and nested collections, please see Collection Variables: Tables.

Note on Syntax

The uniqBy function needs to be used after status filters. This will allow the system to first sort by status or filter, then loop through duplicates. In the above example, this allows us to list or count re-elected directors or re-appointed officers that had been inactive once. Using uniqBy in the beginning of the syntax stops looping across the records if the code finds a duplicate record. For more information on the ordering of functions within syntax, please see Order of Functions.

When we filter by status first to capture only confirmed officers, before removing duplicates with uniqBy, the list is narrowed even further:

 

Input

{#officers | status:'confirmed' | uniqBy:'name'}{name}

{office}{/}

Output

Leslie Knope

Secretary

Ron Swanson

President