ตัวอย่างที่ 1: หาตำแหน่งสมาชิก
=List.PositionOf({"A", "B", "C"}, "B")
1
หาตำแหน่ง Index ของสมาชิกใน List
List.PositionOf จะคืนค่า Index แรกสุด (0-based) ของ value ที่พบใน list ถ้าไม่พบจะคืนค่า -1 สามารถระบุ occurrence เพื่อหาตำแหน่งของตัวที่ซ้ำกัน และใช้ comparer เพื่อกำหนดวิธีการเปรียบเทียบ (Case Sensitive/Insensitive)
=List.PositionOf(list as list, value as any, optional occurrence as nullable number, optional comparer as nullable function) as any
=List.PositionOf(list as list, value as any, optional occurrence as nullable number, optional comparer as nullable function) as any
| Argument | Type | Required | Default | Description |
|---|---|---|---|---|
| list | list | Yes | List ที่ต้องการค้นหา | |
| value | any | Yes | ค่าที่ต้องการค้นหาใน List | |
| occurrence | number | Optional | 0 | ตำแหน่งที่ต้องการหา (0=ตัวแรก, 1=ตัวที่สอง, …) |
| comparer | function | Optional | null | ฟังก์ชัน Comparer สำหรับกำหนดวิธีการเปรียบเทียบ |
หาว่าสินค้าชิ้นนี้อยู่ลำดับที่เท่าไหร่ในรายการสั่งซื้อ
เมื่อ Text.Split แยกข้อความเป็น List แล้ว สามารถหาตำแหน่งของคำบางคำได้
=List.PositionOf({"A", "B", "C"}, "B")
1
=List.PositionOf({"A", "B", "C"}, "D")
-1
=List.PositionOf({"A", "B", "A", "C"}, "A", 1)
2
หมายถึงการนับตำแหน่งเริ่มต้นที่ 0 ไม่ใช่ 1 เหมือน Excel Worksheet Function เช่น สมาชิกตัวแรกอยู่ Index 0, ตัวที่สองอยู่ Index 1
ฟังก์ชัน List.PositionOf ใน Power Query ใช้สำหรับหาตำแหน่ง (Index) ของสมาชิกที่ต้องการค้นหาใน List โดยจะคืนค่าเป็นตัวเลข (0-based index) ถ้าพบ