Thep Excel

Table.ToRecords – แปลงตารางเป็น List ของ Record

Table.ToRecords แปลงตารางให้เป็น List โดยแต่ละแถวกลายเป็น Record พร้อมชื่อคอลัมน์เป็น Field Name ทำให้สะดวกในการวนลูปแบบมีโครงสร้าง

=Table.ToRecords(table as table) as list

By ThepExcel AI Agent
3 December 2025

Function Metrics


Popularity
6/10

Difficulty
4/10

Usefulness
7/10

Syntax & Arguments

=Table.ToRecords(table as table) as list

Argument Type Required Default Description
table table Yes ตารางต้นทางที่ต้องการแปลงเป็น List ของ Record

How it works

ประมวลผลข้อมูลทีละแถว

ใช้ List.Transform ร่วมกับ Table.ToRecords เพื่อประมวลผลข้อมูลแต่ละแถวด้วย Logic ที่ซับซ้อน

การสร้างข้อมูล JSON

โครงสร้าง Record ที่ได้จากฟังก์ชันนี้ใกล้เคียงกับ Object ใน JSON ทำให้ง่ายต่อการ Export เป็น JSON

Examples

ตัวอย่างที่ 1: แปลงตารางพื้นฐาน
let SourceTable = Table.FromRecords({ [ProductID = 1, ProductName = "Laptop", Price = 25000], [ProductID = 2, ProductName = "Mouse", Price = 500], [ProductID =…
ตาราง 3 แถว ถูกแปลงเป็น List ของ 3 Records โดยแต่ละ Record มี Field ProductID, ProductName, Price ตามชื่อคอลัมน์เดิม
Power Query Formula:

let
    SourceTable = Table.FromRecords({
        [ProductID = 1, ProductName = "Laptop", Price = 25000],
        [ProductID = 2, ProductName = "Mouse", Price = 500],
        [ProductID = 3, ProductName = "Keyboard", Price = 2000]
    }),
    AsRecordList = Table.ToRecords(SourceTable)
in
    AsRecordList

Result:

{
[ProductID = 1, ProductName = "Laptop", Price = 25000],
[ProductID = 2, ProductName = "Mouse", Price = 500],
[ProductID = 3, ProductName = "Keyboard", Price = 2000]
}

ตัวอย่างที่ 2: ใช้ร่วมกับ List.Transform เพื่อคำนวณแต่ละแถว
let Source = Table.FromRecords({ [Quantity = 10, UnitPrice = 100], [Quantity = 5, UnitPrice = 200], [Quantity = 3, UnitPrice = 500] }), RecordList = Table.ToRec…
ใช้ Table.ToRecords แปลงเป็น List แล้วทำ List.Transform เพื่อเพิ่ม Field Total ให้แต่ละ Record ด้วยการคูณ Quantity × UnitPrice
Power Query Formula:

let
    Source = Table.FromRecords({
        [Quantity = 10, UnitPrice = 100],
        [Quantity = 5, UnitPrice = 200],
        [Quantity = 3, UnitPrice = 500]
    }),
    RecordList = Table.ToRecords(Source),
    WithTotal = List.Transform(RecordList, each _ & [Total = [Quantity] * [UnitPrice]])
in
    WithTotal

Result:

{
[Quantity = 10, UnitPrice = 100, Total = 1000],
[Quantity = 5, UnitPrice = 200, Total = 1000],
[Quantity = 3, UnitPrice = 500, Total = 1500]
}

ตัวอย่างที่ 3: แยกเอา Field ที่ต้องการจาก Record List
let Source = Table.FromRecords({ [Name = "Alice", Email = "alice@example.com", Department = "Sales"], [Name = "Bob", Email = "bob@example.com", Department = "IT…
ใช้ Table.ToRecords + List.Transform ทำให้เราเลือกเฉพาะ Field Name และ Email ที่ต้องการ โดยตัดทิ้ง Department
Power Query Formula:

let
    Source = Table.FromRecords({
        [Name = "Alice", Email = "alice@example.com", Department = "Sales"],
        [Name = "Bob", Email = "bob@example.com", Department = "IT"]
    }),
    RecordList = Table.ToRecords(Source),
    NameAndEmail = List.Transform(RecordList, each [Name = [Name], Email = [Email]])
in
    NameAndEmail

Result:

{
[Name = "Alice", Email = "alice@example.com"],
[Name = "Bob", Email = "bob@example.com"]
}

ตัวอย่างที่ 4: ใช้ List.Select เพื่อกรองข้อมูลจาก Record List
let Source = Table.FromRecords({ [StudentName = "John", Score = 85], [StudentName = "Jane", Score = 92], [StudentName = "Mike", Score = 78], [StudentName = "Sar…
แปลงตารางเป็น Record List แล้วใช้ List.Select เพื่อกรองเฉพาะ Record ที่ Score ≥ 85 ออกมา
Power Query Formula:

let
    Source = Table.FromRecords({
        [StudentName = "John", Score = 85],
        [StudentName = "Jane", Score = 92],
        [StudentName = "Mike", Score = 78],
        [StudentName = "Sarah", Score = 88]
    }),
    RecordList = Table.ToRecords(Source),
    HighScores = List.Select(RecordList, each [Score] >= 85)
in
    HighScores

Result:

{
[StudentName = "John", Score = 85],
[StudentName = "Jane", Score = 92],
[StudentName = "Sarah", Score = 88]
}

FAQs

ผลลัพธ์ที่ได้เป็น List ของ Record ต่างจาก Table อย่างไร?

List ของ Record ไม่มีชื่อคอลัมน์แสดง แต่ข้อมูลแต่ละ Record ยังมี Field Name ที่สามารถเข้าถึงแบบ [FieldName] ได้อยู่ ส่วนตาราง (Table) มีโครงสร้างแสดงชื่อคอลัมน์ชัดเจน ถ้าต้องการแสดงผลลัพธ์เป็นตารางอีกครั้งก็ใช้ Table.FromRecords กลับไป

Table.ToRecords ต่างจาก Table.ToRows อย่างไร?

Table.ToRecords คืน List ของ Record (มีชื่อ Field) ส่วน Table.ToRows คืน List ของ List (เป็นเพียงค่าโดยไม่มีชื่อคอลัมน์) เมื่อใช้ Table.ToRows ต้องจำว่าสมาชิกลำดับที่ 1 คือคอลัมน์แรก ลำดับที่ 2 คือคอลัมน์ที่สอง เป็นต้น แต่ Table.ToRecords ทำให้สะดวกกว่าเพราะเข้าถึงแบบชื่อ

เมื่อไหร่ที่ต้องใช้ Table.ToRecords?

ใช้เมื่อต้องการประมวลผลแต่ละแถวแบบ Custom Logic ที่ซับซ้อน เช่นคำนวณค่า, สร้าง Field ใหม่, เปลี่ยนชื่อ Field, หรือกรองข้อมูลแบบมีเงื่อนไขที่ต้องเข้าถึง Field หลายตัว เมื่อใช้ร่วมกับ List.Transform, List.Select, List.Select ทำให้สะดวกมากครับ

Resources & Related

Additional Notes

ฟังก์ชัน Table.ToRecords ใน Power Query ใช้สำหรับแปลงข้อมูลตารางให้เป็น List ของ Record โดยแต่ละ Record แทนหนึ่งแถวในตาราง และ Field Name ใน Record คือชื่อคอลัมน์ของตารางเดิม

ที่เจ๋งคือการแปลงนี้ช่วยให้เราสามารถทำงานกับข้อมูลแบบมีโครงสร้างได้ อย่างเช่นใช้ List.Transform เพื่อประมวลผลแต่ละแถว หรือใช้กับ Record.FieldNames เพื่อเข้าถึง Field ต่างๆ ได้ง่ายๆ ไม่ต้องจำหมายเลขคอลัมน์

ส่วนตัวผมใช้ Table.ToRecords บ่อยมากตอนต้องการทำงานที่ซับซ้อน เช่น สร้าง Custom Calculation ต่อแถว หรือจำเป็นต้อง Access ข้อมูลแบบมีชื่อ Field 😎

Leave a Reply

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