Thep Excel

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

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

= Record.FieldNames(record as record) as list

By ThepExcel AI Agent
4 December 2025

Function Metrics


Popularity
6/10

Difficulty
3/10

Usefulness
6/10

Syntax & Arguments

= Record.FieldNames(record as record) as list

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

How it works

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

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

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

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

Examples

ตัวอย่างที่ 1: ดึงชื่อ Field จาก Record พื้นฐาน
let Source = Record.FieldNames([CustomerID = 1, Name = "Bob", Phone = "123-4567"]) in Source
Record มี 3 Field คืนค่าเป็น List ที่มี 3 สมาชิกคือ CustomerID, Name, Phone ตามลำดับเดิม
Power Query Formula:

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

Result:

{"CustomerID", "Name", "Phone"}

ตัวอย่างที่ 2: ตรวจสอบว่า Record มี Field ที่ต้องการหรือไม่
let Customer = [ID = 1, Name = "Alice", Email = "alice@example.com"], Fields = Record.FieldNames(Customer), HasEmail = List.Contains(Fields, "Email") in HasEmai…
ใช้ List.Contains เพื่อตรวจสอบว่า Field ชื่อ Email มีอยู่ใน Record หรือไม่ ถ้ามีจะคืนค่า true
Power Query Formula:

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

Result:

true

ตัวอย่างที่ 3: Loop ผ่าน Field ทั้งหมดของ Record
let Customer = [ID = 100, Name = "John", Amount = 5000], Fields = Record.FieldNames(Customer), Values = List.Transform(Fields, each Record.Field(Customer, _)) i…
ใช้ List.Transform กับ Record.FieldNames เพื่อดึงค่าของแต่ละ Field ออกมา Record.Field รับชื่อ Field เป็น Text (each _) ซึ่งเป็นวิธีที่ Dynamic
Power Query Formula:

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

Result:

{100, "John", 5000}

ตัวอย่างที่ 4: นับจำนวน Field ใน Record
let Product = [SKU = "P123", Name = "Laptop", Price = 29999, Stock = 50], FieldCount = List.Count(Record.FieldNames(Product)) in FieldCount
ใช้ List.Count เพื่อนับจำนวน Field ทั้งหมด ซึ่ง Product Record มี 4 Field
Power Query Formula:

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

Result:

4

ตัวอย่างที่ 5: Record ว่าง (Empty Record)
let EmptyRecord = [], Fields = Record.FieldNames(EmptyRecord) in Fields
ถ้า Record ว่าง (ไม่มี Field) จะคืนค่าเป็น List ว่าง ไม่มีสมาชิก
Power Query Formula:

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

Result:

{}

FAQs

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

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

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

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

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

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

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

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

Resources & Related

Additional Notes

Record.FieldNames คืนค่ารายชื่อ Field ทั้งหมดจาก Record ที่กำหนด โดยเรียงลำดับตามที่ปรากฏใน Record ต่างจาก Record.FieldValues ที่คืนค่าข้อมูล Record.FieldNames ให้เฉพาะชื่อของ Field เท่านั้น

ที่เจ๋งคือสามารถใช้ฟังก์ชันนี้เพื่อตรวจสอบโครงสร้างของ Record แบบ Dynamic ก่อนเข้าถึงข้อมูล เหมาะสำหรับการ Loop ผ่าน Field ทั้งหมด หรือสร้าง Custom Function ที่รองรับ Record ที่มีโครงสร้างไม่แน่นอน

ส่วนตัวผมใช้ Record.FieldNames บ่อยมากเวลาต้องดึงข้อมูลจาก API ที่ Return Structure ไม่แน่นอน หรือต้องเปรียบเทียบว่า Record ขาดฟิลด์อะไรไป

Leave a Reply

Your email address will not be published. Required fields are marked *