---
title: List.Distinct – ลบค่าซ้ำออกจาก List
url: https://www.thepexcel.com/functions/power-query/list-functions/list-distinct/
type: function-explainer
program: Power Query
syntax: "List.Distinct(list as list, optional equationCriteria as any) as list"
date: 2025-12-03
updated: 2025-12-24
scores:
  popularity: 7
  difficulty: 2
  usefulness: 7
---

# List.Distinct – ลบค่าซ้ำออกจาก List

> List.Distinct จะคืน List ใหม่ที่ไม่มีค่าซ้ำกัน โดยเก็บสมาชิกตัวแรกที่พบไว้ และทิ้งตัวที่ซ้ำกันทิ้งไป

## คำอธิบาย

List.Distinct จะคืน List ใหม่ที่ไม่มีค่าซ้ำกัน โดยเก็บสมาชิกตัวแรกที่พบไว้ และทิ้งตัวที่ซ้ำกันทิ้งไป สามารถกำหนด Comparer เพื่อใช้ Logic การเปรียบเทียบที่แตกต่างกันได้ เช่น ไม่สนใจตัวพิมพ์เล็ก/ใหญ่

## Syntax

```excel
List.Distinct(list as list, optional equationCriteria as any) as list
```

## Arguments

| Name | Required | Type | Default | Description |
| --- | --- | --- | --- | --- |
| list | Yes | list |  | List ที่ต้องการลบค่าซ้ำ |
| equationCriteria | No | function or list | Comparer.Ordinal (Case Sensitive) | กำหนดวิธีเปรียบเทียบ สามารถใช้ Comparer (เช่น Comparer.OrdinalIgnoreCase) หรือ Key Selector function ได้ |

## เคสการใช้งาน

### สร้าง Master List

ดึงรายชื่อลูกค้า หรือรายชื่อสินค้าที่ไม่ซ้ำกันจากฐานข้อมูลขนาดใหญ่

### สร้าง Dropdown List

ใช้เป็น Source สำหรับ Filter หรือ Parameter ที่ต้องการ List ตัวเลือกที่ไม่ซ้ำ

### ทำความสะอาดข้อมูล

ลบค่าที่ซ้ำซ้อนออก เพื่อเตรียมข้อมูลสำหรับการวิเคราะห์

## ตัวอย่าง

### 1. ตัวอย่างที่ 1: ลบค่าซ้ำตัวเลขพื้นฐาน

```excel
= List.Distinct({1, 2, 5, 5, 4, 4, 4, 3, 3})
```

**ผลลัพธ์:** `{1, 2, 5, 4, 3}`

ตัวเลขที่ซ้ำกันถูกลบออก เหลือแต่ค่าที่ไม่ซ้ำ โดยเก็บตัวแรกที่พบไว้

### 2. ตัวอย่างที่ 2: ลบค่าซ้ำข้อความ (Case Sensitive)

```excel
= List.Distinct({"Apple", "apple", "Banana"})
```

**ผลลัพธ์:** `{"Apple", "apple", "Banana"}`

เนื่องจากค่าเริ่มต้นเป็น Case Sensitive "Apple" ไม่เท่ากับ "apple" จึงถือว่าเป็นคนละค่า

### 3. ตัวอย่างที่ 3: ลบค่าซ้ำข้อความ (Case Insensitive)

```excel
= List.Distinct({"Apple", "apple", "Banana"}, Comparer.OrdinalIgnoreCase)
```

**ผลลัพธ์:** `{"Apple", "Banana"}`

ใช้ Comparer.OrdinalIgnoreCase เพื่อไม่สนใจตัวพิมพ์เล็ก/ใหญ่ ทำให้ "Apple" และ "apple" ถือเป็นค่าเดียวกัน เก็บตัวแรก

### 4. ตัวอย่างที่ 4: ลบค่าซ้ำจากคอลัมน์ตาราง

```excel
let
    Sales = Table.FromRows({{"Bangkok", 1500}, {"Chiang Mai", 2000}, {"Bangkok", 1800}}, {"City", "Revenue"}),
    Cities = Table.Column(Sales, "City"),
    UniqueCities = List.Distinct(Cities)
in
    UniqueCities
```

**ผลลัพธ์:** `{"Bangkok", "Chiang Mai"}`

ดึงค่าจากคอลัมน์ City แล้วลบซ้ำออก เหลือเมืองที่ไม่ซ้ำกันเพื่อใช้เป็น Dropdown หรือ List สำหรับ Vlookup

### 5. ตัวอย่างที่ 5: ลบซ้ำตามความยาวข้อความ (Key Selector)

```excel
let
    Fruits = {"Apple", "Banana", "Pear", "Fig", "Cherry"},
    Result = List.Distinct(Fruits, each Text.Length(_))
in
    Result
```

**ผลลัพธ์:** `{"Apple", "Banana", "Pear"}`

ลบค่าซ้ำตามความยาว ถ้าคำว่า "Pear" (4 ตัว) และ "Apple" (5 ตัว) มีความยาวต่างกัน ก็เก็บทั้งสองตัว แต่ "Fig" (3 ตัว) และ "Pear" (4 ตัว) ต่างกัน เลยเก็บไว้

## หมายเหตุเพิ่มเติม

- ถ้าต้องการลบซ้ำแล้วเรียงลำดับ ใช้ List.Sort หลังจาก List.Distinct ครับ

- ถ้าต้องการเก็บลำดับเดิมและลบซ้ำ List.Distinct เหมาะสุด เพราะ List.Union อาจเปลี่ยนลำดับ

- เมื่อใช้ Comparer ให้ระวังว่า Function signature ต้องรับ 2 arguments (ค่า 1 และ ค่า 2) เพื่อเปรียบเทียบ

- สำหรับ Large Lists สามารถใช้ Table เพื่อให้ Performance ดีขึ้น โดยแปลง List เป็น Table ก่อนลบซ้ำ

- Null values จะถูกเก็บไว้เหมือน Distinct value ใดใดหนึ่ง ถ้าต้องการลบ Null ให้ใช้ List.RemoveNulls ก่อน

## คำถามที่พบบ่อย

**Q: List.Distinct เก็บตัวไหนเมื่อมีค่าซ้ำ?**

เก็บตัวแรกที่พบในลำดับเดิม ส่วนตัวที่ซ้ำหลังจากนั้นจะถูกทิ้ง

**Q: List.Distinct กับ List.Union ต่างกันอย่างไร?**

List.Union ใช้รวม 2 List และลบซ้ำ ส่วน List.Distinct ใช้ลบซ้ำจาก List เดียว นอกจากนี้ List.Union ไม่รับประกันลำดับ แต่ List.Distinct เก็บลำดับเดิม

**Q: สามารถลบซ้ำตามหลายคอลัมน์ได้ไหม?**

ไม่ได้โดยตรง แต่สามารถประมวลผล Table ก่อน ถ้าต้องการลบซ้ำตามหลายคอลัมน์ให้ใช้ Table.Distinct แทน

**Q: List.Distinct กับ List.Select ต่างกันอย่างไร?**

List.Distinct ลบค่าซ้ำ ส่วน List.Select ใช้กรองข้อมูลตามเงื่อนไข (เช่น เลือกเฉพาะค่าที่มากกว่า 10)

**Q: Comparer มีตัวไหนบ้าง?**

Comparer.Ordinal (Case Sensitive, ค่าเริ่มต้น) และ Comparer.OrdinalIgnoreCase (ไม่สนใจตัวพิมพ์) ส่วนใหญ่ใช้แค่สองตัวนี้

## ฟังก์ชันที่เกี่ยวข้อง

- [List.IsDistinct – ตรวจสอบว่าสมาชิกใน List ไม่ซ้ำกันเลยหรือไม่](https://www.thepexcel.com/functions/power-query/list-functions/list-isdistinct/)
- [List.RemoveItems – ลบสมาชิกที่กำหนดออกจาก List](https://www.thepexcel.com/functions/power-query/list-functions/list-removeitems/)
- [List.RemoveMatchingItems – ลบสมาชิกที่ตรงกันด้วย Comparer](https://www.thepexcel.com/functions/power-query/list-functions/list-removematchingitems/)
- [List.Sort – เรียงลำดับรายการ](https://www.thepexcel.com/functions/power-query/list-functions/list-sort/)
- [List.Union – ยูเนียน List](https://www.thepexcel.com/functions/power-query/list-functions/list-union/)

## แหล่งข้อมูลเพิ่มเติม

- [Microsoft Learn: List.Distinct](https://learn.microsoft.com/en-us/powerquery-m/list-distinct) _(official)_
- [PowerQuery.how - List Functions](https://powerquery.how/list-distinct/) _(article)_
- [Microsoft Learn: Comparer Functions](https://learn.microsoft.com/en-us/powerquery-m/comparer-functions) _(official)_
- [Microsoft Learn: Table.Distinct](https://learn.microsoft.com/en-us/powerquery-m/table-distinct) _(official)_

---

_Source: [https://www.thepexcel.com/functions/power-query/list-functions/list-distinct/](https://www.thepexcel.com/functions/power-query/list-functions/list-distinct/)_
