List.Distinct Function in Power Query M

Sumit Bansal
Written by

Sumit Bansal is the founder of TrumpExcel.com and a 13-time Microsoft Excel MVP. He started this site in 2013 to share his passion for Excel through easy tutorials, tips, and training videos, helping you master Excel, boost productivity, and maybe even enjoy spreadsheets!

The List.Distinct function in Power Query removes the duplicate values from a list. It keeps the first time each value shows up and leaves the values in their original order.

Since any column of a table can be turned into a list, this is how you get the unique values from a column, count them, or build a clean list of names, cities, or categories without touching the rest of the table.

Follow along with the example file

List.Distinct Function Examples.xlsx

Download

List.Distinct Function Syntax

List.Distinct(list as list, optional equationCriteria as any) as list
  • list – the list you want to remove the duplicates from. It can be a list you type in curly brackets, such as {"A","B","A"}, or a column of a table.
  • equationCriteria (optional) – tells List.Distinct how to decide that two values are the same. It can be a comparer such as Comparer.OrdinalIgnoreCase, which ignores upper and lower case (Example 4). It can also be a function that works out a key from each value, so List.Distinct compares the keys instead of the values themselves (Example 9). You can give both together in a list.

What it returns: A new list with the duplicates removed. If the list you give it is empty, you get an empty list back.

The List.Distinct function is part of the Power Query M language, so it works wherever Power Query does in Excel: Excel 365, 2024, 2021, 2019, and 2016 for Windows, and Excel for Microsoft 365 on Mac.

When to Use the List.Distinct Function

Use this function when you need to:

  • Get the unique values from a column as a list
  • Count how many different values a column has
  • Remove duplicates that differ only in upper and lower case
  • Clean up values with extra spaces before removing duplicates
  • Build a sorted list of unique values
  • Get the unique values from two columns at once
  • Count the different values for each group, like the number of products each region sold
  • Keep only the first item for each month, category, or other key

Example 1: Remove Duplicates from a List

Let’s start with the simplest use. Suppose a shop got orders for shirts in sizes M, L, S, M, XL, L, and M, and you want each size only once.

In Excel, go to Data > Get Data > From Other Sources > Blank Query. In the Power Query Editor that opens, type this formula in the formula bar:

= List.Distinct({"M","L","S","M","XL","L","M"})

Result: A list with M, L, S, and XL

List.Distinct keeps one of each size: M, L, S and XL

Here, the curly brackets create a list of seven sizes, and List.Distinct removes the repeats. M shows up three times and L twice, but each one is kept only once.

Notice the order of the result. List.Distinct keeps the first time each value shows up and doesn’t sort anything, so M stays first because it was the first size in the list.

In the example file, Example 1 types its values right into the query, so it doesn’t have a sheet. You can open it from Data > Queries & Connections.

Example 2: Get the Unique Values from a Column

Here’s the most common use of List.Distinct. Below is a log of support tickets in an Excel Table named SupportTickets, and I want a list of the agents who handled them, with each name only once.

SupportTickets table with the agent who handled each ticket

In the code below, the Source line uses the Excel.CurrentWorkbook function to pull the SupportTickets table into Power Query by its name.

If you try this on your own data, format it as a table (Ctrl + T) and put your table’s name in place of “SupportTickets”.

Create a new blank query (as in Example 1), open the Advanced Editor (Home > Advanced Editor), and use this code:

let
Source = Excel.CurrentWorkbook(){[Name="SupportTickets"]}[Content],
Agents = List.Distinct(Source[Agent])
in
Agents

Result: A list with Connor Roberts, Elizabeth Taylor, Charles Nelson, and Rachel Davis

List.Distinct on the Agent column returns the four different agents

Here, Source is the name of the first step, and it holds the SupportTickets table. Source[Agent] takes the Agent column of that table as a list of nine names, and List.Distinct brings it down to the four different agents.

The result is a list, not a table, which is what you want when you’ll use the names in another step. If you want it back as a table, click the To Table button (List Tools > Transform) in the Power Query Editor.

List.Distinct works on one list at a time. If you want to remove the rows of a table that are exact repeats of each other, use the Table.Distinct function on the table instead.

Example 3: Count the Unique Values in a Column

This one comes up a lot in reports. Below is a table named WebOrders with the customer ID for each online order, and I want to know how many different customers placed these orders.

WebOrders table with the customer ID for each order

Create a new blank query, open the Advanced Editor, and use this code:

let
Source = Excel.CurrentWorkbook(){[Name="WebOrders"]}[Content],
UniqueCustomers = List.Count(List.Distinct(Source[Customer ID]))
in
UniqueCustomers

Result: 5

List.Count of the distinct customer IDs returns 5

Here, List.Distinct turns the eight customer IDs into a list of the five different ones (C-1042 placed three orders and C-1187 placed two). Then the List.Count function counts the items in that list.

So whenever you need a distinct count, wrap List.Distinct in List.Count.

If the column has blank cells, they come in as null, and List.Distinct keeps null as one more value, so your count would be one too high. Wrap the distinct list in the List.RemoveNulls function before you count it, as Example 7 does.

And if you only want to know whether a column has any duplicates at all, the List.IsDistinct function gives you a TRUE or FALSE answer instead of a list.

Example 4: Remove Duplicates While Ignoring Upper and Lower Case

By default, List.Distinct treats “Denver” and “denver” as two different values. Below is a table named Registrations with the city each person typed in when signing up for an event, and I want the list of cities, no matter how they were typed.

Registrations table with cities typed in different cases

If you use List.Distinct(Source[City]) here, you get six cities back, because Denver, denver, and DENVER all count as different text. So add a comparer that ignores case.

Create a new blank query, open the Advanced Editor, and use this code:

let
Source = Excel.CurrentWorkbook(){[Name="Registrations"]}[Content],
Cities = List.Distinct(Source[City],Comparer.OrdinalIgnoreCase)
in
Cities

Result: A list with Denver, Austin, and Boise

Comparer.OrdinalIgnoreCase returns Denver, Austin and Boise

Here, the second argument, Comparer.OrdinalIgnoreCase, tells List.Distinct to compare the cities without looking at upper and lower case. So Denver, denver, and DENVER are treated as the same city.

List.Distinct still keeps the first one it finds, in the way it was typed. Denver is first in the column, so you get “Denver”.

If “denver” had been the first one in the column, the result would show it in lowercase.

Follow along with the example file

List.Distinct Function Examples.xlsx

Download

Example 5: Remove Duplicates Caused by Extra Spaces and Case

This next one is a very common data-cleaning problem. Below is a table named SignUps with the email addresses people used to join a newsletter, and I want a clean list of unique emails.

SignUps table with emails typed with different capitals and spaces

Some emails were typed with capital letters, and the last one has a space at the end that you can’t see in the cell. Plain List.Distinct(Source[Email]) returns eight emails, because it only removes the one exact repeat (hailey.webb@example.com).

So clean the emails first, and then remove the duplicates. Create a new blank query, open the Advanced Editor, and use this code:

let
Source = Excel.CurrentWorkbook(){[Name="SignUps"]}[Content],
Emails = List.Distinct(List.Transform(Source[Email],each Text.Lower(Text.Trim(_))))
in
Emails

Result: A list with five emails (jason.marshall, courtney.porter, lori.edwards, hailey.webb, and ryan.allen, all @example.com)

Trimming and lowercasing first leaves five unique emails

Here, the List.Transform function runs on every email in the column. The Text.Trim function removes the spaces at the start and end, and the Text.Lower function turns the text into lowercase, so each email gets one clean spelling.

The each part runs this cleaning once for every email in the list, and the _ stands for the email being cleaned.

Once they’re cleaned, the different spellings of the same email become exact matches, and List.Distinct keeps one of each.

You could also put the cleaning inside List.Distinct as a key, like List.Distinct(Source[Email],each Text.Lower(Text.Trim(_))). That removes the same duplicates, but it returns the emails as they were typed (such as Courtney.Porter@example.com), so you’d still have the messy versions in your list.

Example 6: Get a Sorted List of Unique Values

Since List.Distinct keeps the original order, you’ll often want to sort the result too. Below is a table named OfficeSupplies with the category of each item, and I want the unique categories in A to Z order.

OfficeSupplies table with the category of each item

Create a new blank query, open the Advanced Editor, and use this code:

let
Source = Excel.CurrentWorkbook(){[Name="OfficeSupplies"]}[Content],
Categories = List.Sort(List.Distinct(Source[Category]))
in
Categories

Result: A list with Electronics, Furniture, Kitchen, and Stationery

List.Sort on the distinct categories returns them in A to Z order

Here, List.Distinct first gives you the four categories in the order they first show up (Stationery, Furniture, Electronics, and Kitchen). The List.Sort function then sorts them in ascending order.

A sorted unique list like this is handy as the source for a drop-down list, or as a list of values you want to check other data against.

Example 7: Get the Unique Values from Two Columns

Sometimes the values you want are spread over more than one column. Below is a table named Workshop with the people who attended the morning and the afternoon sessions, and I want one list of everyone who came, with each name only once.

Workshop table with the morning and afternoon attendees and a blank (null) cell

Create a new blank query, open the Advanced Editor, and use this code:

let
Source = Excel.CurrentWorkbook(){[Name="Workshop"]}[Content],
AllNames = List.Combine({Source[Morning Session],Source[Afternoon Session]}),
Attendees = List.RemoveNulls(List.Distinct(AllNames))
in
Attendees

Result: A list with six names (Tyler Tucker, Olivia Miller, Holly Allen, Calvin Adams, Andrea Bell, and Shane Wells)

One list of the six people who attended, with each name once

Here, the List.Combine function joins the two columns into one list of ten values, and List.Distinct keeps each name only once, so the people who came to both sessions show up once in the result.

The afternoon session had one person fewer, so its last cell is blank, and a blank cell comes into Power Query as null. List.Distinct keeps null as a value of its own, so without List.RemoveNulls, your list would end with a null.

Example 8: Count the Unique Values for Each Group

Now let’s use List.Distinct inside a group. Below is a table named RegionSales with the product sold in each order, and I want to know how many different products each region sold, in a Products Sold column.

RegionSales table with the region and product of each order

Create a new blank query, open the Advanced Editor, and use this code:

let
Source = Excel.CurrentWorkbook(){[Name="RegionSales"]}[Content],
#"Grouped Rows" = Table.Group(Source,{"Region"},{{"Products Sold",each List.Count(List.Distinct([Product])),Int64.Type}})
in
#"Grouped Rows"

Result: One row per region with its Products Sold (East 3, West 2, and North 2)

Table.Group gives the number of different products each region sold

Here, the Table.Group function groups the orders by Region. For each region, [Product] is the list of products from that region’s rows only, and List.Count(List.Distinct(…)) counts the different ones.

For example, East has four orders (Laptop, Laptop, Mouse, and Monitor), so it sold three different products. The Int64.Type at the end sets the new column as whole numbers.

Example 9: Keep the First Item for Each Month

This last one uses a key instead of a comparer. Below is a table named ServiceVisits with the date of each service visit, and I want only the first visit in each month.

ServiceVisits table with the date of each service visit

Create a new blank query, open the Advanced Editor, and use this code:

let
Source = Excel.CurrentWorkbook(){[Name="ServiceVisits"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Visit Date",type date}}),
FirstVisits = List.Distinct(#"Changed Type"[Visit Date],each Date.StartOfMonth(_))
in
FirstVisits

Result: A list with 1/6/2026, 2/3/2026, and 3/2/2026

The key selector keeps the first visit date in each month

Here, the Changed Type step sets the Visit Date column as dates. Dates from an Excel Table come into Power Query as date and time values, so without this step, your result would hold date and time values instead of plain dates.

The second argument, each Date.StartOfMonth(_), works out a key for every date, which is the first day of its month. So 1/6/2026, 1/14/2026, and 1/27/2026 all get the same key (1/1/2026), and List.Distinct treats them as duplicates.

List.Distinct keeps the first date for each key, and it returns the date itself, not the key. That’s why you get 1/6/2026 and not 1/1/2026.

Since it keeps whichever date comes first in the list, this works because the visits are in date order. If your dates aren’t sorted, sort them before List.Distinct sees them.

In the code above, replace the FirstVisits line with this one:

FirstVisits = List.Distinct(List.Sort(#"Changed Type"[Visit Date]),each Date.StartOfMonth(_))

This is the opposite order from Example 6. There, List.Sort sorted the result, but here the list has to be sorted first, so List.Distinct finds the earliest date of each month first.

Other Excel Articles You May Also Like:

  • List.Select – Use List.Select in Power Query M to keep only the list items that meet a condition.
  • List.Reverse – Use List.Reverse in Power Query M to reverse the order of a list.
  • List.IsEmpty – Use List.IsEmpty in Power Query M to check whether a list has values or is empty.
  • List.Contains – Use List.Contains in Power Query M to check if a value exists in a list.
  • Table.SelectColumns – Use Table.SelectColumns in Power Query M to keep only the columns you need.
  • Table.Join – Use Table.Join in Power Query M to join two tables.
  • How to Undo in Power Query? – How to undo a step in Power Query.

All Power Query Functions

Sumit Bansal

Sumit Bansal

13x Microsoft Excel MVP

Hey! I'm Sumit Bansal, founder of trumpexcel.com and a Microsoft Excel MVP. I started this site in 2013 because I genuinely love Microsoft Excel (yes, really!) and wanted to share that passion through easy Excel tutorials, tips, and Excel training videos. My goal is straightforward: help you master Excel skills so you can work smarter, boost productivity, and maybe even enjoy spreadsheets along the way!

Get the FREE 51 Excel Tips Ebook

Enter your details and the free PDF is on its way to your inbox.

Hmm, that didn't go through. Please check your email and try again.

No spam. You'll also get my weekly Excel newsletter. Unsubscribe anytime.

Check your inbox!

The ebook is on its way to your email. It usually lands within a couple of minutes.