DAX DISTINCT Function: Remove Duplicate Rows and Get Unique Values
In this article, we will learn how to use the DISTINCT
function in DAX to retrieve unique values from a column and remove
duplicate rows from a table. We will also understand how
EVALUATE returns the result of each table expression.
📥 Practice Data:
Download Practice Data Free
🎥 Prefer learning through video?
DAX YouTube Playlist
What Is DISTINCT in DAX?
DISTINCT is a DAX function that returns a table containing
unique values from a column or unique rows from a table. It removes
duplicate values or duplicate rows from the result.
The function can be used with a column or a table, depending on the
result you want to retrieve.
Basic Syntax
DISTINCT(<Column>)
Or:
DISTINCT(<Table>)
DISTINCT returns a table, not a single scalar value.
When used with a column, it returns unique values. When used with a table,
it returns unique rows.
1. Retrieving the Complete Product Table
Query
EVALUATE
'Product'
Step-by-step explanation
Step 1: EVALUATE
EVALUATE tells DAX to return the result of a table
expression.
Step 2: ‘Product’
‘Product’ is the table expression. Since we are passing
the entire table, DAX returns all rows and columns from the Product table.
What will you get?
The result contains the complete Product table, including all available
columns and rows.
| ProductKey | Product | Category | Color | List Price |
|---|---|---|---|---|
| 1 | Product A | Components | Red | 100 |
| 2 | Product B | Components | Blue | 200 |
| 3 | Product C | Clothing | Black | 300 |
The example above is only for illustration. Your actual result will
contain the data available in your Product table.
EVALUATE ‘Product’ returns the complete Product table.
No duplicate removal is performed by this query.
2. Getting the Product Category List Using DISTINCT
Query
EVALUATE
DISTINCT('Product'[Category])
Step-by-step explanation
Step 1: EVALUATE
EVALUATE returns the result of the table expression.
Step 2: DISTINCT
DISTINCT removes duplicate values from the specified
column and returns only unique values.
Step 3: ‘Product'[Category]
This is the column from which we want to retrieve unique category values.
What will you get?
If the Product table contains repeated category values, the result will
contain each category only once.
| Category |
|---|
| Components |
| Clothing |
| Bikes |
For example, if the Product table contains the category
Components in 100 different rows, the DISTINCT query
returns Components only once.
DISTINCT(‘Product'[Category]) returns a one-column table
containing unique category values.
3. Removing Duplicate Rows from the Product Table
Query
EVALUATE
DISTINCT('Product')
Step-by-step explanation
Step 1: EVALUATE
EVALUATE returns the result of the table expression.
Step 2: DISTINCT
DISTINCT removes duplicate rows from the table expression.
Step 3: ‘Product’
The entire Product table is passed to DISTINCT. DAX compares the complete
row values and returns only unique rows.
What will you get?
The result contains the Product table without duplicate rows.
| ProductKey | Product | Category | Color | List Price |
|---|---|---|---|---|
| 1 | Product A | Components | Red | 100 |
| 2 | Product B | Components | Blue | 200 |
| 3 | Product C | Clothing | Black | 300 |
If two rows contain exactly the same values in every column, DISTINCT
returns only one of those rows.
DISTINCT(‘Product’) returns unique rows from the complete
Product table.
Understanding the Difference Between DISTINCT Column and DISTINCT Table
| Query | What It Returns |
|---|---|
| DISTINCT(‘Product'[Category]) | Unique values from the Category column |
| DISTINCT(‘Product’) | Unique rows from the complete Product table |
How DISTINCT Works with a Column
Consider the following category values in the Product table:
| Product | Category |
|---|---|
| Product A | Components |
| Product B | Components |
| Product C | Clothing |
| Product D | Clothing |
| Product E | Bikes |
When we execute:
EVALUATE
DISTINCT('Product'[Category])
DAX returns:
| Category |
|---|
| Components |
| Clothing |
| Bikes |
Notice that the repeated category values have been removed.
How DISTINCT Works with a Table
When DISTINCT is applied to a table, DAX compares the complete row values.
For example, consider these rows:
| ProductKey | Product | Category | Color | List Price |
|---|---|---|---|---|
| 1 | Product A | Components | Red | 100 |
| 1 | Product A | Components | Red | 100 |
| 2 | Product B | Components | Blue | 200 |
The first two rows are identical across all columns. Therefore:
EVALUATE
DISTINCT('Product')
Returns:
| ProductKey | Product | Category | Color | List Price |
|---|---|---|---|---|
| 1 | Product A | Components | Red | 100 |
| 2 | Product B | Components | Blue | 200 |
The duplicate row has been removed.
Important Notes
1. DISTINCT returns a table
The result of DISTINCT is always a table expression. It can be used with
EVALUATE to display the result in a DAX query.
2. DISTINCT on a column returns unique values
When you pass a column to DISTINCT, the result contains only unique values
from that column.
3. DISTINCT on a table returns unique rows
When you pass a table to DISTINCT, the result contains unique rows based on
the complete set of columns in the table expression.
4. DISTINCT does not modify the original table
DISTINCT creates a new table expression for the query result. It does not
delete or change rows in the original Product table.
5. DISTINCT can be used with other table functions
DISTINCT is commonly used with functions such as
FILTER, SELECTCOLUMNS, and
SUMMARIZE to create unique and filtered table results.
📝 Practice Exercise
Try writing a query to retrieve the unique values from the
Product[Color] column.
Then write another query to retrieve unique rows from the
Product table.
Expected Query 1: Unique Colors
EVALUATE
DISTINCT('Product'[Color])
Expected Query 2: Unique Product Rows
EVALUATE
DISTINCT('Product')
Conclusion
In this article, we learned how to use the DISTINCT
function in DAX to retrieve unique values and remove duplicate rows.
-
DISTINCT(‘Product'[Category]) returns unique category
values. -
DISTINCT(‘Product’) returns unique rows from the
complete Product table. -
EVALUATE is used to return the table expression as
the query result. - DISTINCT does not modify the original table.
-
DISTINCT is useful when you need to explore unique values or remove
duplicate rows from a table expression.
Understanding DISTINCT is an important step toward working with
unique values, filtering data, and building more advanced DAX queries.
Next: We can learn how to retrieve unique values using
VALUES and understand the difference between
DISTINCT and VALUES.
📥 Practice Data:
Download Practice Data Free
🎥 Prefer learning through video?
DAX YouTube Playlist