Thep Excel

Table.WithErrorContext – เพิ่มบริบท Error ให้ตาราง

Table.WithErrorContext เป็นฟังก์ชันภายใน Power Query ที่ใช้เพิ่มข้อมูล context ให้กับค่า เพื่อให้ error messages มีความชัดเจนมากขึ้นเวลา debug

= Table.WithErrorContext(value as any, context as text) as any

By ThepExcel AI Agent
6 December 2025

Function Metrics


Popularity
5/10

Difficulty
4/10

Usefulness
6/10

Syntax & Arguments

= Table.WithErrorContext(value as any, context as text) as any

Argument Type Required Default Description
value any Yes ค่าที่ต้องการเพิ่มข้อมูล context ให้ (สามารถเป็น table, record, list, หรือค่าใด ๆ ก็ได้)
context text Yes ข้อความ context ที่อธิบายค่า หรือบอก error context information (เช่น ‘Rows 1-100’, ‘Processing Customer Table’, ‘Validation Step 2’)

How it works

Debugging

Debugging

Error Tracing

Error Tracing

Examples

ตัวอย่างพื้นฐาน – เพิ่ม context ให้ table
let Source = Table.FromRows({{1, "A"}, {2, "B"}}, {"ID", "Name"}), WithContext = Table.WithErrorContext(Source, "Initial data load") in WithContext
ใช้ Table.WithErrorContext เพื่อเพิ่มข้อมูล context ให้กับ Source table ว่า data นี้มาจากการ load ครั้งแรก เพื่อให้ debug ได้ชัดเจน
Power Query Formula:

let
    Source = Table.FromRows({{1, "A"}, {2, "B"}}, {"ID", "Name"}),
    WithContext = Table.WithErrorContext(Source, "Initial data load")
in
    WithContext

Result:

ตารางที่มี context "Initial data load" เชื่อมโยงไว้ เตรียมสำหรับขั้นตอนถัดไป

ตัวอย่างกับ error handling – ดัก error ด้วย try-catch
let Data = Table.FromRows({{1, 100}, {2, 200}}, {"ID", "Value"}), Transform = try Table.WithErrorContext( Table.SelectRows(Data, each [Value] > 150), "Filtering…
รวม Table.WithErrorContext กับ try-catch เพื่อให้ได้ error message ที่มีข้อมูล context ชี้แจงมากขึ้น ทำให้รู้ว่า error เกิดตรงไหนในกระบวนการ
Power Query Formula:

let
    Data = Table.FromRows({{1, 100}, {2, 200}}, {"ID", "Value"}),
    Transform = try
        Table.WithErrorContext(
            Table.SelectRows(Data, each [Value] > 150),
            "Filtering rows where Value > 150"
        )
    catch (error) => error[Message]
in
    Transform

Result:

ตารางที่ผ่านการ filter กับ context ข้อมูล หรือ error message ถ้า transform ล้มเหลว

ตัวอย่างกับการประมวลผลข้อมูลซับซ้อน – Multi-step transformation
let Source = Table.FromRows( {{"2025-01-01", "John", 5000}, {"2025-01-02", "Jane", 6000}}, {"Date", "Name", "Amount"} ), Step1_Load = Table.WithErrorContext(Sou…
เพิ่ม context ที่ละเอียดสำหรับแต่ละขั้นตอน เพื่อให้ถ้า error เกิดขึ้น จะรู้ได้ว่าล้มเหลวตรง Step ไหน ทำให้ debug ง่ายมาก
Power Query Formula:

let
    Source = Table.FromRows(
        {{"2025-01-01", "John", 5000}, {"2025-01-02", "Jane", 6000}},
        {"Date", "Name", "Amount"}
    ),
    Step1_Load = Table.WithErrorContext(Source, "Step 1: Load Sales Data"),
    Step2_Filter = Table.WithErrorContext(
        Table.SelectRows(Step1_Load, each [Amount] > 4000),
        "Step 2: Filter Amount > 4000"
    ),
    Step3_Transform = Table.WithErrorContext(
        Table.AddColumn(Step2_Filter, "Verified", each "Yes"),
        "Step 3: Add Verified Column"
    )
in
    Step3_Transform

Result:

ตาราง sales ที่ผ่านการ filter และเพิ่ม column ว่า Verified พร้อมกับข้อมูล context ของแต่ละขั้นตอน

ตัวอย่างการใช้กับ record
let MyRecord = [Name = "Alice", Age = 25, Salary = 50000], WithContext = Table.WithErrorContext(MyRecord, "User Profile Record") in WithContext
Table.WithErrorContext สามารถใช้กับ record ได้ด้วย ไม่จำเป็นต้องใช้แค่ table เท่านั้น ช่วยให้ tracking record ง่ายขึ้น
Power Query Formula:

let
    MyRecord = [Name = "Alice", Age = 25, Salary = 50000],
    WithContext = Table.WithErrorContext(MyRecord, "User Profile Record")
in
    WithContext

Result:

Record ที่มี context "User Profile Record" ติดอยู่

FAQs

Table.WithErrorContext เป็นฟังก์ชันภายใน (internal function) หรือเปล่า?

ใช่ค่ะ ผม Table.WithErrorContext เป็นฟังก์ชันภายในของ Power Query ที่ Microsoft ออกแบบมาสำหรับใช้ภายในระบบ ไม่ได้ recommend ใช้ใน production query อย่างเป็นทางการ แต่ยังสามารถใช้ได้ตามปกติ ปัญหาคือ ถ้า Microsoft เปลี่ยนการทำงาน version ใหม่ อาจจะ break code ได้

ควรใช้ Table.WithErrorContext กับอะไร?

ผมแนะนำให้ใช้กับค่าใด ๆ ก็ได้ (table, record, list, text, number) แต่โดยทั่วไปเหมาะสุดเวลาใช้กับ table หรือ record เพื่อเพิ่มข้อมูล debug context โดยเฉพาะ multi-step transformation ที่มีหลาย steps

ต่างกับ try-catch อย่างไร?

ผม try-catch ใช้สำหรับ**จับ** error ที่เกิดขึ้นแล้ว ส่วน Table.WithErrorContext ใช้เพื่อ**เตรียม context** ให้ error message มีข้อมูลชี้แจงมากขึ้น ทั้งสองตัวสามารถใช้ร่วมกันได้ดี ผม try-catch ข้างนอก Table.WithErrorContext ข้างในเลย

ถ้าใช้ Table.WithErrorContext แล้ว context ดังกล่าวจะไปไหน?

ผม context นั้น จะบันทึกอยู่ใน value และจะปรากฏขึ้นมา เวลาที่ error เกิดขึ้น ผมเห็นมันอยู่ใน error message หรือ diagnostic data ของ Power Query

Resources & Related

Additional Notes

Table.WithErrorContext คือฟังก์ชันภายใน Power Query ที่ออกแบบมาสำหรับการจัดการข้อมูล context ในช่วงประมวลผล โดยเชื่อมข้อมูล context text เข้ากับค่า (value) เพื่อให้เมื่อเกิด error ขึ้นมา error message นั้นจะมีข้อมูลที่ชี้แจงมากขึ้น

ที่เจ๋งคือ ฟังก์ชันนี้ช่วยให้เรา track ได้ว่าข้อมูลไปถึงขั้นไหนในกระบวนการประมวลผล และเกิด error จากขั้นตอนไหน ทำให้ debugging ซับซ้อนได้ง่ายขึ้นมากเลย โดยไม่ต้องลองผิดลองถูกนาน

ส่วนตัวผมใช้ Table.WithErrorContext บ่อยเวลาต้องประมวลผลข้อมูลแบบ multi-step ที่มีหลายตาราง transform เพราะถ้า error เกิดขึ้น ก็รู้ได้ทันทีว่า “ปัญหาอยู่ตรงไหน” แทนที่จะ scroll หา error ไปสักพักครึ่ง

Leave a Reply

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