---
title: Table.WithErrorContext – เพิ่มบริบท Error ให้ตาราง
url: https://www.thepexcel.com/functions/power-query/table-functions/table-witherrorcontext/
type: function-explainer
program: Power Query
syntax: "= Table.WithErrorContext(value as any, context as text) as any"
date: 2025-12-06
updated: 2025-12-25
scores:
  popularity: 5
  difficulty: 4
  usefulness: 6
---

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

> Table.WithErrorContext เป็นฟังก์ชันภายใน Power Query ที่ใช้เพิ่มข้อมูล context ให้กับค่า เพื่อให้ er

## คำอธิบาย

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

## Syntax

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

## Arguments

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

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

### Debugging

Debugging

### Error Tracing

Error Tracing

## ตัวอย่าง

### 1. ตัวอย่างพื้นฐาน - เพิ่ม context ให้ table

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

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

ใช้ Table.WithErrorContext เพื่อเพิ่มข้อมูล context ให้กับ Source table ว่า data นี้มาจากการ load ครั้งแรก เพื่อให้ debug ได้ชัดเจน

### 2. ตัวอย่างกับ error handling - ดัก error ด้วย try-catch

```excel
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
```

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

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

### 3. ตัวอย่างกับการประมวลผลข้อมูลซับซ้อน - Multi-step transformation

```excel
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
```

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

เพิ่ม context ที่ละเอียดสำหรับแต่ละขั้นตอน เพื่อให้ถ้า error เกิดขึ้น จะรู้ได้ว่าล้มเหลวตรง Step ไหน ทำให้ debug ง่ายมาก

### 4. ตัวอย่างการใช้กับ record

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

**ผลลัพธ์:** `Record ที่มี context "User Profile Record" ติดอยู่`

Table.WithErrorContext สามารถใช้กับ record ได้ด้วย ไม่จำเป็นต้องใช้แค่ table เท่านั้น ช่วยให้ tracking record ง่ายขึ้น

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

- ผมแนะนำให้ใช้ Table.WithErrorContext เวลาต้องการติดตามขั้นตอนการประมวลผลข้อมูลที่ซับซ้อน เพราะมันช่วยให้ debug ได้ว่า error อยู่ step ไหน

- อธิบาย context ให้ชัดเจนและเป็นตรรมชาติ เช่น 'Processing Sales Data - Validation Step' แทนแค่ 'Data' เพราะทำให้หา error ได้ว่า

- ส่วนตัวผม ใช้ Table.WithErrorContext ทั้งขั้นตอน load, transform, และ validate เพื่อให้ tracking data flow ง่ายขึ้น ถ้า error เกิด ก็รู้ได้เลยว่า data มาจากไหน

- ผมแนะนำให้รวม Table.WithErrorContext กับ try-catch เพื่อได้ error message ที่มีข้อมูล context + error details ครบถ้วน

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

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

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

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

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

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

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

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

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

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

- [Microsoft Learn: Table.WithErrorContext](https://learn.microsoft.com/en-us/powerquery-m/table-witherrorcontext) _(official)_
- [Power Query M Language Reference](https://learn.microsoft.com/en-us/powerquery-m/) _(official)_

---

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