--- title: "DistinctByKey" slug: "distinctbykey" updated: 2022-02-21T17:28:04Z published: 2022-02-21T17:28:04Z canonical: "docs.datajet.app/distinctbykey" --- > ## Documentation Index > Fetch the complete documentation index at: https://docs.datajet.app/llms.txt > Use this file to discover all available pages before exploring further. # DistinctByKey ## General: DistinctByKey ## Create a flag field, set to true for the first occurrence of each unique value of a key field, for the set of records linked to each key in a primary table. ## Purpose Use this function to: - Flag records grouped by their relationship to data in another table ## Return Value | Property | Value | | --- | --- | | **FieldType** | Integer | | **FieldSize** | Low | | **DataType** | Discrete | | **DataSize** | Byte | ```shell Example Return Value: 0 , 1, null ``` ![](https://cdn.document360.io/73b6469b-c1e4-47e9-aa65-986c8ebd17ca/Images/Documentation/Gen_DistinctByKey_Sample.PNG) ## Parameters | Parameter | JSON | Description | | --- | --- | --- | | **Table** | “targetTable”: “MyTableName” | The target table on which the new field will be created | | **Filter** | “dataset”: {DataSet_JSON} | Optional. If a filter is applied, records not in the filter recordset will be returned as null. | | **Function** | "function":"DistinctByKey" | DistinctByKey | | **Key Table** | “p1”: “PrimaryTableName” | Required. The table that provides the join information for grouping the data on the target table. Must be a table that is linked to the target table. If the link [transactions].[cid] → [customer].[cid] exists, and [customer] is specified as the key table, data will be grouped by [cid] when calculating DistinctByKey. | | **D****istinct Key** | “p2”:"DistinctKeyName" | Required. The Field on the target table used to determine if a unique value has been found Within the key group, the first time a value is encountered it is flagged as 1, all other times it is flagged as 0. | ## JSON Sample ![](https://cdn.document360.io/73b6469b-c1e4-47e9-aa65-986c8ebd17ca/Images/Documentation/Gen_DescIndex_JSON.PNG) ## Usage Notes ![](https://cdn.document360.io/73b6469b-c1e4-47e9-aa65-986c8ebd17ca/Images/Documentation/Gen_DistinctByKey_Usage.png) See Also: - Engineering | Expression Field ## Example | Example | Details | | --- | --- | | **Description** | **Flag Each unique instance of product-date-ns-2020, grouped by customer** | | **Input** | - Table: = transactions - Filter:= [No Filter] - **Function: = DistinctByKey** - Key Table: = customer - Distinct Key:=[transactions].[product-date-ns-2020] - Name = distinct_pr-date-ns-2020 | | **Sample** | ![](https://cdn.document360.io/73b6469b-c1e4-47e9-aa65-986c8ebd17ca/Images/Documentation/Gen_DistinctByKey_Example.png) |