---
title: Text.PositionOf – หาตำแหน่งข้อความ
url: https://www.thepexcel.com/functions/power-query/text-functions/text-positionof/
type: function-explainer
program: Power Query
syntax: "Text.PositionOf(text as text, substring as text, optional occurrence as nullable number, optional comparer as nullable function) as any"
date: 2025-12-03
updated: 2025-12-24
scores:
  popularity: 6
  difficulty: 3
  usefulness: 6
---

# Text.PositionOf – หาตำแหน่งข้อความ

> หาตำแหน่งของข้อความย่อยในข้อความหลัก (0-indexed) โดยคืนค่า -1 หากไม่พบ

## คำอธิบาย

หาตำแหน่งของข้อความย่อยในข้อความหลัก (0-indexed) โดยคืนค่า -1 หากไม่พบ

## Syntax

```excel
Text.PositionOf(text as text, substring as text, optional occurrence as nullable number, optional comparer as nullable function) as any
```

## Arguments

| Name | Required | Type | Default | Description |
| --- | --- | --- | --- | --- |
| text | Yes | text |  | ข้อความหลักที่ต้องการค้นหา |
| substring | Yes | text |  | ข้อความย่อยที่ต้องการหาตำแหน่ง |
| occurrence | No | number or Occurrence value | Occurrence.First | Occurrence.First (ค่าเริ่มต้น) = หาตำแหน่งแรก, Occurrence.Last = หาตำแหน่งสุดท้าย |
| comparer | No | function | Comparer.Ordinal (case-sensitive) | ฟังก์ชันสำหรับควบคุมการเปรียบเทียบ เช่น Comparer.Ordinal (case-sensitive), Comparer.OrdinalIgnoreCase (case-insensitive) |

## ตัวอย่าง

### 1. หาตำแหน่งแรกของข้อความ

```excel
Text.PositionOf("Hello, World!", "o")
```

**ผลลัพธ์:** `4`

ตัว 'o' ปรากฏครั้งแรกที่ตำแหน่ง Index 4 (H=0, e=1, l=2, l=3, o=4)

### 2. หาตำแหน่งเมื่อไม่พบข้อความ

```excel
Text.PositionOf("Hello, World!", "xyz")
```

**ผลลัพธ์:** `-1`

ไม่พบ 'xyz' ในข้อความจึงคืนค่า -1

### 3. หาตำแหน่งสุดท้าย (Occurrence.Last)

```excel
let
    Text = "apple apple apple",
    Position = Text.PositionOf(Text, "apple", Occurrence.Last)
in
    Position
```

**ผลลัพธ์:** `12`

หา "apple" ตัวสุดท้ายที่ตำแหน่ง 12 (ครั้งแรกที่ 0, ครั้งที่สองที่ 6, ครั้งที่สามที่ 12)

### 4. ค้นหา Case-Insensitive

```excel
let
    Text = "Hello World",
    Position = Text.PositionOf(Text, "HELLO", Occurrence.First, Comparer.OrdinalIgnoreCase)
in
    Position
```

**ผลลัพธ์:** `0`

ค้นหา "HELLO" โดยไม่สนใจขนาดตัวอักษร พบที่ตำแหน่ง 0

### 5. รวมกับ Text.Range เพื่อแยกข้อความ

```excel
let
    FullText = "Name: John Doe",
    DelimiterPos = Text.PositionOf(FullText, ": "),
    Name = Text.Range(FullText, DelimiterPos + 2)
in
    Name
```

**ผลลัพธ์:** `"John Doe"`

หาตำแหน่งของ ": " จากนั้นแยกข้อความหลังจากตัวคั่นออกมา

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

- ตรวจสอบค่า -1 เพื่อหรือใช้ if statement ตรวจสอบว่ากลับมา -1 ก่อนใช้ผลลัพธ์ในการคำนวณต่อไป

- รวม Text.PositionOf กับ Text.Range เพื่อแยก (extract) ส่วนของข้อความอย่างมีประสิทธิภาพ

- หากต้องการหาตำแหน่งทั้งหมดของข้อความย่อย ใช้ Text.PositionOfAny หรือสร้าง Custom Function ด้วย List.Generate

- ใช้ Comparer.OrdinalIgnoreCase เมื่อทำงานกับข้อมูลที่ใหญ่และต้องการค้นหาแบบ case-insensitive

- Index นับจากซ้ายไปขวา ตำแหน่งแรก = 0, ตำแหน่งที่สอง = 1 เป็นต้น

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

**Q: ความแตกต่างระหว่าง Text.PositionOf และ Text.Contains คืออะไร?**

Text.Contains เพียงแล้วบอกว่าข้อความย่อยอยู่ในข้อความหรือไม่ (true/false) ส่วน Text.PositionOf บอกตำแหน่งที่แน่นอน ถ้าไม่พบ Text.PositionOf คืนค่า -1

**Q: ทำไมการเรียกฟังก์ชันคืนค่า -1?**

ข้อความย่อยที่คุณค้นหาไม่พบในข้อความหลัก ลองตรวจสอบการสะกดและพิจารณาใช้ Comparer.OrdinalIgnoreCase หากต้องการค้นหาแบบไม่สนใจขนาดตัวอักษร

**Q: Index 0 หมายความว่าอะไร?**

Index 0 คือตำแหน่งของตัวอักษรตัวแรก ดังนั้น Index 0 = ตัวแรก, Index 1 = ตัวที่สอง เป็นต้น (0-indexed system)

**Q: ฉันต้องการหาตำแหน่งที่สอง ทำไงดี?**

ปัจจุบัน Text.PositionOf รองรับเฉพาะ Occurrence.First และ Occurrence.Last หากต้องการตำแหน่งที่สองให้ใช้ Text.PositionOfAny หรือสร้าง Custom Function ด้วย List.FindIndex

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

- [Text.Contains – ตรวจสอบว่าข้อความมีส่วนประกอบที่ต้องการหรือไม่](https://www.thepexcel.com/functions/power-query/text-functions/text-contains/)
- [List.PositionOf – หาตำแหน่งของสมาชิกใน List](https://www.thepexcel.com/functions/power-query/list-functions/list-positionof/)
- [Text.PositionOfAny – ค้นหาตำแหน่งของอักขระใดๆ](https://www.thepexcel.com/functions/power-query/text-functions/text-positionofany/)

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

- [Microsoft Learn - Text.PositionOf](https://learn.microsoft.com/en-us/powerquery-m/text-positionof) _(official)_
- [PowerQuery.how - Text.PositionOf](https://powerquery.how/text-positionof/) _(article)_
- [Microsoft Learn - Text.Range](https://learn.microsoft.com/en-us/powerquery-m/text-range) _(official)_
- [Microsoft Learn - Occurrence](https://learn.microsoft.com/en-us/powerquery-m/occurrence) _(official)_

---

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