---
title: Record.FieldNames – ดึงชื่อ Field ทั้งหมดจาก Record
url: https://www.thepexcel.com/functions/power-query/record-functions/record-fieldnames/
type: function-explainer
program: Power Query
syntax: = Record.FieldNames(record as record) as list
date: 2025-12-04
updated: 2025-12-25
scores:
  popularity: 6
  difficulty: 3
  usefulness: 6
---

# Record.FieldNames – ดึงชื่อ Field ทั้งหมดจาก Record

> ฟังก์ชันสำหรับดึงรายชื่อ Field ทั้งหมดจาก Record และคืนค่าเป็น List ของ Text

## คำอธิบาย

ฟังก์ชันสำหรับดึงรายชื่อ Field ทั้งหมดจาก Record และคืนค่าเป็น List ของ Text

## Syntax

```excel
= Record.FieldNames(record as record) as list
```

## Arguments

| Name | Required | Type | Default | Description |
| --- | --- | --- | --- | --- |
| record | Yes | record |  | Record ที่ต้องการดึงชื่อ Field ทั้งหมด |

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

### ตรวจสอบโครงสร้าง Record

ใช้ดูว่าใน Record มี Field อะไรบ้าง เพื่อนำไปตรวจสอบหรือเปรียบเทียบ

### ใช้ร่วมกับฟังก์ชันอื่น

เช่น ใช้ร่วมกับ List.Count เพื่อนับจำนวน Field หรือใช้ในการ Reorder Field

## ตัวอย่าง

### 1. ตัวอย่างที่ 1: ดึงชื่อ Field จาก Record พื้นฐาน

```excel
let
    Source = Record.FieldNames([CustomerID = 1, Name = "Bob", Phone = "123-4567"])
in
    Source
```

**ผลลัพธ์:** `{"CustomerID", "Name", "Phone"}`

Record มี 3 Field คืนค่าเป็น List ที่มี 3 สมาชิกคือ CustomerID, Name, Phone ตามลำดับเดิม

### 2. ตัวอย่างที่ 2: ตรวจสอบว่า Record มี Field ที่ต้องการหรือไม่

```excel
let
    Customer = [ID = 1, Name = "Alice", Email = "alice@example.com"],
    Fields = Record.FieldNames(Customer),
    HasEmail = List.Contains(Fields, "Email")
in
    HasEmail
```

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

ใช้ List.Contains เพื่อตรวจสอบว่า Field ชื่อ Email มีอยู่ใน Record หรือไม่ ถ้ามีจะคืนค่า true

### 3. ตัวอย่างที่ 3: Loop ผ่าน Field ทั้งหมดของ Record

```excel
let
    Customer = [ID = 100, Name = "John", Amount = 5000],
    Fields = Record.FieldNames(Customer),
    Values = List.Transform(Fields, each Record.Field(Customer, _))
in
    Values
```

**ผลลัพธ์:** `{100, "John", 5000}`

ใช้ List.Transform กับ Record.FieldNames เพื่อดึงค่าของแต่ละ Field ออกมา Record.Field รับชื่อ Field เป็น Text (each _) ซึ่งเป็นวิธีที่ Dynamic

### 4. ตัวอย่างที่ 4: นับจำนวน Field ใน Record

```excel
let
    Product = [SKU = "P123", Name = "Laptop", Price = 29999, Stock = 50],
    FieldCount = List.Count(Record.FieldNames(Product))
in
    FieldCount
```

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

ใช้ List.Count เพื่อนับจำนวน Field ทั้งหมด ซึ่ง Product Record มี 4 Field

### 5. ตัวอย่างที่ 5: Record ว่าง (Empty Record)

```excel
let
    EmptyRecord = [],
    Fields = Record.FieldNames(EmptyRecord)
in
    Fields
```

**ผลลัพธ์:** `{}`

ถ้า Record ว่าง (ไม่มี Field) จะคืนค่าเป็น List ว่าง ไม่มีสมาชิก

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

- ผมแนะนำให้ใช้ Record.FieldNames เมื่อต้องจัดการ Record ที่มาจากแหล่งข้อมูลภายนอก เช่น JSON หรือ API ที่โครงสร้างไม่แน่นอน

- ถ้าต้องตรวจสอบว่า Field ใดมีอยู่ก่อนเข้าถึง ให้รวมกับ List.Contains เพื่อหลีกเลี่ยง Error

- ส่วนตัวผม ใช้ Record.FieldNames + List.Transform + Record.Field เพื่อสร้าง Custom Function ที่ Dynamic และสามารถรองรับ Record หลายแบบ

- ถ้าต้องเปรียบเทียบโครงสร้าง Record สองตัว ให้ใช้ Record.FieldNames แล้วเทียบ List ด้วย Set.IsEqual หรือ List.Difference

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

**Q: ลำดับของชื่อ Field เป็นไปตามอะไร?**

ลำดับจะตามที่ปรากฏใน Record ซึ่ง Record ใน Power Query M เป็น Ordered Collection หมายความว่าลำดับ Field มีความสำคัญ ผมทดสอบแล้วลำดับจะไม่เปลี่ยนแปลง แม้ว่าจะสร้าง Record ด้วยวิธีต่างกัน

**Q: ต่างจาก Record.FieldValues อย่างไร?**

Record.FieldNames คืนค่าแค่ชื่อ Field เป็น List ของ Text ส่วน Record.FieldValues คืนค่าข้อมูล (Value) ของแต่ละ Field ออกมา ถ้าต้องชื่อให้ใช้ FieldNames ถ้าต้องค่าให้ใช้ FieldValues

**Q: ถ้า Record ว่างจะเกิดอะไร?**

ผมทดสอบแล้ว Record ว่าง (Empty Record) จะคืนค่าเป็น List ว่าง {} ไม่มีข้อผิดพลาด ปลอดภัยใช้ได้

**Q: ใช้ได้กับ Table บ้างไหม?**

ไม่ได้ Record.FieldNames ทำงานกับ Record เท่านั้น ถ้าต้องดึงชื่อ Column จาก Table ให้ใช้ Table.ColumnNames แทน ผมเคยลืมเรื่องนี้เองจึงเตือน

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

- [Record.FieldValues – คืนค่าข้อมูลทั้งหมดใน Record เป็น List](https://www.thepexcel.com/functions/power-query/record-functions/record-fieldvalues/)
- [Record.FieldCount – นับจำนวน Field ใน Record](https://www.thepexcel.com/functions/power-query/record-functions/record-fieldcount/)
- [Record.HasFields – ตรวจสอบว่ามี Field ที่ระบุหรือไม่](https://www.thepexcel.com/functions/power-query/record-functions/record-hasfields/)
- [Record.Field – ดึงค่า Field จาก Record](https://www.thepexcel.com/functions/power-query/record-functions/record-field/)

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

- [Microsoft Learn: Record.FieldNames](https://learn.microsoft.com/en-us/powerquery-m/record-fieldnames) _(official)_
- [Microsoft Learn: Record.Field](https://learn.microsoft.com/en-us/powerquery-m/record-field) _(official)_
- [Microsoft Learn: Record.FieldValues](https://learn.microsoft.com/en-us/powerquery-m/record-fieldvalues) _(official)_
- [Microsoft Learn: List.Contains](https://learn.microsoft.com/en-us/powerquery-m/list-contains) _(official)_

---

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