Table.Distinct ลบแถวที่ซ้ำกันออกจากตารางตามคอลัมน์ที่กำหนด หรือทั้งตารางถ้าไม่ระบุคอลัมน์
=Table.Distinct(table as table, optional equationCriteria as any) as table
=Table.Distinct(table as table, optional equationCriteria as any) as table
| Argument | Type | Required | Default | Description |
|---|---|---|---|---|
| table | Table | Yes | ตารางที่ต้องการลบแถวซ้ำกัน | |
| equationCriteria | Text or List | Optional | ทั้งแถว | คอลัมน์ที่ต้องการใช้ในการเช็คการซ้ำกัน (ถ้าไม่ระบุจะเช็คทั้งแถว) |
let Sales = Table.FromRecords({ [ProductID = 1, Product = "Laptop", Price = 999], [ProductID = 2, Product = "Mouse", Price = 25], [ProductID = 1, Product = "Lap…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
ตารางมี 2 แถว (แถวที่ 3 ซ้ำกับแถวที่ 1 ถูกลบออก)
let Customers = Table.FromRecords({ [CustomerID = "C001", Name = "John", City = "Bangkok"], [CustomerID = "C002", Name = "Jane", City = "Bangkok"], [CustomerID…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
ตารางมี 2 แถว (เก็บแถวแรกของแต่ละ CustomerID)
let Orders = Table.FromRecords({ [OrderID = 1, CustomerID = "C001", Amount = 500], [OrderID = 2, CustomerID = "C002", Amount = 300], [OrderID = 3, CustomerID =…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
ตารางมี 2 แถว (ลบแถวที่ซ้ำกันในทั้ง CustomerID และ Amount)
let ImportedEmails = Table.FromRecords({ [Email = "john@example.com", Source = "LinkedIn"], [Email = "jane@example.com", Source = "Facebook"], [Email = "john@ex…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
ตารางมี 3 แถว (john@example.com, jane@example.com, bob@example.com)
Table.Distinct ลบแถวที่ซ้ำกัน ส่วน Table.SelectRows กรองแถวตามเงื่อนไข ทั้งสองใช้คนละวัตถุประสงค์
ตัว Power Query จะทำให้เร็วที่สุดตามที่เป็นไปได้ ถ้ากังวลใจให้ใช้ Table.Buffer() ก่อน Table.Distinct เพื่อให้มั่นใจในการทำงาน
ไม่มีการรับประกัน Power Query จะทำการเลือกแบบที่มันเห็นสมควร ถ้าต้องการความแน่นอน (เช่น ต้องเก็บแถวแรก) ให้เรียง Table.Sort() ก่อน
ได้ สร้างคอลัมน์ใหม่ที่มีค่าที่ต้องการก่อน แล้วใช้ Table.Distinct ตามคอลัมน์นั้น
Table.Distinct ใช้สำหรับลบแถวที่ซ้ำกันออกจากตารางตามเงื่อนไขที่คุณกำหนด ถ้าไม่ระบุคอลัมน์ก็จะหาแถวที่ซ้ำกันทั้งแถวแบบเดียวกันหมด
ที่เจ๋งคือ Table.Distinct ทำให้เราได้แถวที่ไม่ซ้ำกันง่ายๆ ไม่ต้องมานั่งสร้างสูตรซับซ้อน
ส่วนตัวผม มักใช้ Table.Distinct เวลามีข้อมูลจากหลายแหล่งที่มีการซ้ำกันเยอะ เช่น ดึงข้อมูลลูกค้าจากระบบต่างๆ แล้วมันซ้ำกันเพราะลูกค้าปรากฏในหลายระบบ ใช้ Table.Distinct แล้วปัญหาหมดไป 😎