Thep Excel

Table.Distinct – ลบแถวที่ซ้ำกันออกจากตาราง

Table.Distinct ลบแถวที่ซ้ำกันออกจากตารางตามคอลัมน์ที่กำหนด หรือทั้งตารางถ้าไม่ระบุคอลัมน์

=Table.Distinct(table as table, optional equationCriteria as any) as table

By ThepExcel AI Agent
3 December 2025

Function Metrics


Popularity
8/10

Difficulty
2/10

Usefulness
8/10

Syntax & Arguments

=Table.Distinct(table as table, optional equationCriteria as any) as table

Argument Type Required Default Description
table Table Yes ตารางที่ต้องการลบแถวซ้ำกัน
equationCriteria Text or List Optional ทั้งแถว คอลัมน์ที่ต้องการใช้ในการเช็คการซ้ำกัน (ถ้าไม่ระบุจะเช็คทั้งแถว)

Examples

ตัวอย่างพื้นฐาน – ลบแถวซ้ำทั้งหมด
let Sales = Table.FromRecords({ [ProductID = 1, Product = "Laptop", Price = 999], [ProductID = 2, Product = "Mouse", Price = 25], [ProductID = 1, Product = "Lap…
ตั้งแต่ไม่ระบุ equationCriteria ก็จะเช็คแถวทั้งหมด ถ้าข้อมูลทั้ง 3 คอลัมน์เหมือนกันแบบเดียวกันจะถือว่าซ้ำกัน
Power Query Formula:

let
    Sales = Table.FromRecords({
        [ProductID = 1, Product = "Laptop", Price = 999],
        [ProductID = 2, Product = "Mouse", Price = 25],
        [ProductID = 1, Product = "Laptop", Price = 999]
    }),
    Unique = Table.Distinct(Sales)
in
    Unique

Result:

ตารางมี 2 แถว (แถวที่ 3 ซ้ำกับแถวที่ 1 ถูกลบออก)

ลบแถวซ้ำตามคอลัมน์เดียว
let Customers = Table.FromRecords({ [CustomerID = "C001", Name = "John", City = "Bangkok"], [CustomerID = "C002", Name = "Jane", City = "Bangkok"], [CustomerID…
ระบุให้ดูเฉพาะคอลัมน์ CustomerID ถ้า CustomerID ซ้ำจะลบแถวที่ 3 ออก แม้ว่า City ต่างกันก็ตาม
Power Query Formula:

let
    Customers = Table.FromRecords({
        [CustomerID = "C001", Name = "John", City = "Bangkok"],
        [CustomerID = "C002", Name = "Jane", City = "Bangkok"],
        [CustomerID = "C001", Name = "John", City = "Chiang Mai"]
    }),
    UniqueByID = Table.Distinct(Customers, "CustomerID")
in
    UniqueByID

Result:

ตารางมี 2 แถว (เก็บแถวแรกของแต่ละ CustomerID)

ลบแถวซ้ำตามหลายคอลัมน์
let Orders = Table.FromRecords({ [OrderID = 1, CustomerID = "C001", Amount = 500], [OrderID = 2, CustomerID = "C002", Amount = 300], [OrderID = 3, CustomerID =…
ใช้ list {"CustomerID", "Amount"} เพื่อเช็คความซ้ำกันตามสองคอลัมน์พร้อมกัน
Power Query Formula:

let
    Orders = Table.FromRecords({
        [OrderID = 1, CustomerID = "C001", Amount = 500],
        [OrderID = 2, CustomerID = "C002", Amount = 300],
        [OrderID = 3, CustomerID = "C001", Amount = 500]
    }),
    UniqueByIDAmount = Table.Distinct(Orders, {"CustomerID", "Amount"})
in
    UniqueByIDAmount

Result:

ตารางมี 2 แถว (ลบแถวที่ซ้ำกันในทั้ง CustomerID และ Amount)

ประยุกต์ใช้ – ข้อมูลอีเมลจากหลายแหล่ง
let ImportedEmails = Table.FromRecords({ [Email = "john@example.com", Source = "LinkedIn"], [Email = "jane@example.com", Source = "Facebook"], [Email = "john@ex…
ดึงข้อมูลอีเมลจากหลายแหล่ง Table.Distinct ลบที่ซ้ำกัน เก็บเฉพาะแถวแรกของแต่ละอีเมล
Power Query Formula:

let
    ImportedEmails = Table.FromRecords({
        [Email = "john@example.com", Source = "LinkedIn"],
        [Email = "jane@example.com", Source = "Facebook"],
        [Email = "john@example.com", Source = "Gmail"],
        [Email = "bob@example.com", Source = "LinkedIn"]
    }),
    UniqueEmails = Table.Distinct(ImportedEmails, "Email"),
    Result = Table.SelectColumns(UniqueEmails, {"Email"})
in
    Result

Result:

ตารางมี 3 แถว (john@example.com, jane@example.com, bob@example.com)

FAQs

Table.Distinct กับ Table.SelectRows ต่างกันอย่างไร?

Table.Distinct ลบแถวที่ซ้ำกัน ส่วน Table.SelectRows กรองแถวตามเงื่อนไข ทั้งสองใช้คนละวัตถุประสงค์

ถ้าข้อมูลมีเยอะ Table.Distinct จะช้าไหม?

ตัว Power Query จะทำให้เร็วที่สุดตามที่เป็นไปได้ ถ้ากังวลใจให้ใช้ Table.Buffer() ก่อน Table.Distinct เพื่อให้มั่นใจในการทำงาน

ข้อมูลซ้ำไป แถวไหนถูกเก็บไว้?

ไม่มีการรับประกัน Power Query จะทำการเลือกแบบที่มันเห็นสมควร ถ้าต้องการความแน่นอน (เช่น ต้องเก็บแถวแรก) ให้เรียง Table.Sort() ก่อน

สามารถลบแถวซ้ำตามคอลัมน์ที่คำนวณได้ไหม?

ได้ สร้างคอลัมน์ใหม่ที่มีค่าที่ต้องการก่อน แล้วใช้ Table.Distinct ตามคอลัมน์นั้น

Resources & Related

Additional Notes

Table.Distinct ใช้สำหรับลบแถวที่ซ้ำกันออกจากตารางตามเงื่อนไขที่คุณกำหนด ถ้าไม่ระบุคอลัมน์ก็จะหาแถวที่ซ้ำกันทั้งแถวแบบเดียวกันหมด

ที่เจ๋งคือ Table.Distinct ทำให้เราได้แถวที่ไม่ซ้ำกันง่ายๆ ไม่ต้องมานั่งสร้างสูตรซับซ้อน

ส่วนตัวผม มักใช้ Table.Distinct เวลามีข้อมูลจากหลายแหล่งที่มีการซ้ำกันเยอะ เช่น ดึงข้อมูลลูกค้าจากระบบต่างๆ แล้วมันซ้ำกันเพราะลูกค้าปรากฏในหลายระบบ ใช้ Table.Distinct แล้วปัญหาหมดไป 😎

Leave a Reply

Your email address will not be published. Required fields are marked *