---
title: Occurrence.Type – ระบุการเกิดซ้ำ (ครั้งที่) ของการค้นหา
url: https://www.thepexcel.com/functions/power-query/enumerations/occurrence-type/
type: function-explainer
program: Power Query
syntax: "Text.PositionOf(text, substring, Occurrence.First|Last) / Text.PositionOfAny(text, characters, Occurrence.All)"
date: 2025-12-04
updated: 2025-12-17
scores:
  popularity: 5
  difficulty: 2
  usefulness: 5
---

# Occurrence.Type – ระบุการเกิดซ้ำ (ครั้งที่) ของการค้นหา

> ชุดค่าคงที่สำหรับเลือกครั้งแรก/ครั้งสุดท้าย/ทั้งหมดของการค้นหา

## คำอธิบาย

Occurrence.Type เป็นชุดค่าคงที่ที่ใช้ระบุการเกิดซ้ำของค่าที่ค้นพบ เช่น Occurrence.First (ครั้งแรก), Occurrence.Last (ครั้งสุดท้าย) และ Occurrence.All (ทุกตำแหน่ง) โดยมักใช้เป็นพารามิเตอร์ occurrence ในฟังก์ชันอย่าง Text.PositionOf และ Text.PositionOfAny

## Syntax

```excel
Text.PositionOf(text, substring, Occurrence.First|Last) / Text.PositionOfAny(text, characters, Occurrence.All)
```

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

### หา Index ของข้อความที่พบครั้งสุดท้าย

เวลาเจอสตริงซ้ำหลายครั้งและต้องการตำแหน่งสุดท้าย ให้ส่ง Occurrence.Last เป็นพารามิเตอร์ occurrence

_เหมาะกับ:_ last-occurrence-index

## ตัวอย่าง

### 1. ตัวอย่างที่ 1: ค่าเริ่มต้นคือครั้งแรก

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

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

ถ้าไม่ระบุ occurrence จะถือว่าเอาครั้งแรก (เหมือน Occurrence.First) จึงได้ตำแหน่ง 7 ของคำว่า World ในข้อความ

### 2. ตัวอย่างที่ 2: เลือกครั้งสุดท้ายด้วย Occurrence.Last

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

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

ระบุ Occurrence.Last เพื่อให้คืนค่าตำแหน่งของคำที่พบครั้งสุดท้าย ทำให้ได้ตำแหน่ง 21 แทนที่จะเป็นครั้งแรก

### 3. ตัวอย่างที่ 3: เอาทุกตำแหน่งด้วย Occurrence.All

```excel
Text.PositionOfAny("Hello, World!", {"H", "W"}, Occurrence.All)
```

**ผลลัพธ์:** `{0, 7}`

ใช้ Occurrence.All เพื่อให้คืนค่าเป็นรายการ (list) ของตำแหน่งทั้งหมดที่พบอักขระในชุดที่กำหนด เช่น H และ W

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

ถ้าคุณต้องการควบคุมว่าเอาผลลัพธ์ที่พบครั้งที่เท่าไหร่ ให้ดูว่าในฟังก์ชันนั้นมีพารามิเตอร์ occurrence หรือไม่ และใช้ Occurrence.First/Last/All ให้เหมาะกับงาน

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

**Q: Occurrence.All จะคืนค่าเป็นอะไร?**

โดยทั่วไปถ้าฟังก์ชันรองรับ Occurrence.All จะคืนค่าเป็น list ของตำแหน่งทั้งหมดที่พบ ไม่ใช่ตัวเลขค่าเดียว

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

- [Text.PositionOf – หาตำแหน่งข้อความ](https://www.thepexcel.com/functions/power-query/text-functions/text-positionof/)
- [Text.PositionOfAny – ค้นหาตำแหน่งของอักขระใดๆ](https://www.thepexcel.com/functions/power-query/text-functions/text-positionofany/)

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

- [เอกสาร Microsoft Learn: Occurrence.Type](https://learn.microsoft.com/en-us/powerquery-m/occurrence-type) _(documentation)_

---

_Source: [https://www.thepexcel.com/functions/power-query/enumerations/occurrence-type/](https://www.thepexcel.com/functions/power-query/enumerations/occurrence-type/)_
