Thep Excel

Table.FromRecords – สร้างตารางจากรายการ Record

แปลง List ของ Record เป็นตาราง โดยแต่ละ Record กลายเป็นหนึ่งแถว เหมาะสำหรับข้อมูลจาก API หรือ JSON

=Table.FromRecords(records as list, optional columns as any, optional missingField as nullable number) as table

By ThepExcel AI Agent
3 December 2025

Function Metrics


Popularity
6/10

Difficulty
4/10

Usefulness
7/10

Syntax & Arguments

=Table.FromRecords(records as list, optional columns as any, optional missingField as nullable number) as table

Argument Type Required Default Description
records List Yes รายการ Record ที่ต้องการแปลงเป็นตาราง เช่น {[ID=1, Name=”Alice”], [ID=2, Name=”Bob”]}
columns Any Optional null ระบุชื่อคอลัมน์ (List of Text) หรือ Type ของตาราง (type table […]) เพื่อกำหนดลำดับคอลัมน์ตามที่ต้อง
missingField Number Optional MissingField.Error วิธีการจัดการฟิลด์ที่หายไปในบาง Record – MissingField.Error (ค่า default) หรือ MissingField.UseNull (เติม null)

Examples

สร้างตารางพื้นฐาน
let Source = Table.FromRecords({ [ID = 1, Name = "Alice", Sales = 500], [ID = 2, Name = "Bob", Sales = 750], [ID = 3, Name = "Charlie", Sales = 600] }) in Sourc…
แปลง List ของ Record 3 ตัวเป็นตาราง โดยไม่ระบุ columns parameter ลำดับคอลัมน์อาจขึ้นอยู่กับลำดับการปรากฏตัวของฟิลด์
Power Query Formula:

let
    Source = Table.FromRecords({
        [ID = 1, Name = "Alice", Sales = 500],
        [ID = 2, Name = "Bob", Sales = 750],
        [ID = 3, Name = "Charlie", Sales = 600]
    })
in
    Source

Result:

Table 3 rows, 3 columns (ID, Name, Sales)

กำหนดลำดับคอลัมน์และ Type
let Source = Table.FromRecords({ [ID = 1, Name = "Alice"], [ID = 2, Name = "Bob"] }, type table [Name = text, ID = number]) in Source
ใช้ columns parameter ด้วย type table เพื่อบังคับลำดับคอลัมน์ให้ Name มาก่อน ID และกำหนด Type ของแต่ละคอลัมน์
Power Query Formula:

let
    Source = Table.FromRecords({
        [ID = 1, Name = "Alice"],
        [ID = 2, Name = "Bob"]
    }, type table [Name = text, ID = number])
in
    Source

Result:

Table (Name, ID) - ชื่อคอลัมน์อยู่ก่อน ID

จัดการฟิลด์ที่หายไป
let Source = Table.FromRecords({ [CustomerID = 1, FirstName = "Alice", Email = "alice@company.com"], [CustomerID = 2, FirstName = "Bob"], [CustomerID = 3, First…
Record ที่ 2 ไม่มี Email field จะแสดงเป็น null แทน ซึ่ง MissingField.UseNull ช่วยให้ไม่เกิด Error
Power Query Formula:

let
    Source = Table.FromRecords({
        [CustomerID = 1, FirstName = "Alice", Email = "alice@company.com"],
        [CustomerID = 2, FirstName = "Bob"],
        [CustomerID = 3, FirstName = "Charlie", Email = "charlie@company.com"]
    },
    type table [CustomerID = number, FirstName = text, Email = text],
    MissingField.UseNull)
in
    Source

Result:

Table 3 rows - แถวที่ 2 มี null ในคอลัมน์ Email

สร้างจากข้อมูล JSON API
let JsonData = Json.Document(Web.Contents("https://api.example.com/users")), RecordsList = JsonData[data], Table = Table.FromRecords( RecordsList, type table [i…
รับข้อมูล JSON จาก API, แปลงเป็น Record List, แล้วสร้างตาราง โดยจัดการฟิลด์ที่อาจหายไป
Power Query Formula:

let
    JsonData = Json.Document(Web.Contents("https://api.example.com/users")),
    RecordsList = JsonData[data],
    Table = Table.FromRecords(
        RecordsList,
        type table [id = number, username = text, email = text],
        MissingField.UseNull
    )
in
    Table

Result:

Table ของผู้ใช้จาก API

FAQs

ต่างกันระหว่าง Table.FromRecords กับ Table.FromRows ยังไง?

Table.FromRecords ใช้กับ List of Record (named fields) เช่น {[Name=”Alice”, Age=25]} ส่วน Table.FromRows ใช้กับ List of List (positional values) เช่น {{“Alice”, 25}} เลือกตามรูปแบบข้อมูลที่มี

ถ้า Record ตัวหนึ่งไม่มีฟิลด์ที่ Record อื่นมี จะเกิดอะไร?

ตามค่า default (MissingField.Error) จะเกิด Error หากต้องให้มันเติม null แทน ให้ใช้ MissingField.UseNull

ลำดับคอลัมน์ไม่ตรงตามที่ต้องการ ต้องทำยังไง?

ใช้ columns parameter โดยระบุ type table เช่น type table [ColA = text, ColB = number] จะบังคับลำดับตามลำดับการประกาศ

เวอร์ชัน Power Query ไหนรองรับ Table.FromRecords?

สองพารามิเตอร์แรก (records, columns) มีจากแรก ส่วน missingField parameter เพิ่มมาในเวอร์ชันหลัง (Power Query 2016 ขึ้นไป)

Resources & Related

Related functions

Additional Notes

ฟังก์ชัน Table.FromRecords ใช้เปลี่ยน List ของ Record (รูปแบบคล้าย Dictionary) เป็นตาราง Structure ตามธรรมชาติ โดยชื่อฟิลด์ใน Record จะกลายเป็นชื่อคอลัมน์ และค่าแต่ละ Record เป็นหนึ่งแถว

ที่เจ๋งคือเมื่อคุณทำงานกับ API ที่คืน JSON หรือข้อมูลที่มาจากการ Transform ขั้นกลาง Table.FromRecords ช่วยให้เปลี่ยนข้อมูลลง Table ได้อย่างมีประสิทธิภาพ โดยคุณสามารถควบคุมลำดับคอลัมน์ และจัดการฟิลด์ที่หายไปได้

ส่วนตัวผม มักใช้ Table.FromRecords ตอนสร้างตารางจากข้อมูล JSON ที่ได้มาจาก API หรือสร้าง test data ในการ debug ข้อสำคัญคือ parameter columns เพราะถ้าไม่กำหนดลำดับคอลัมน์ อาจไม่ตรงตามที่คาดหวัง และ parameter missingField ช่วยให้ไม่ต้อง Error เมื่อ Record บางอันไม่มีฟิลด์บาง 😎

Leave a Reply

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