---
title: Error.Record – สร้างระเบียนข้อผิดพลาด
url: https://www.thepexcel.com/functions/power-query/error-handling/error-record/
type: function-explainer
program: Power Query
syntax: "= Error.Record(reason as text, optional message as nullable text, optional detail as any, optional parameters as nullable list, optional errorCode as nullable text) as record"
date: 2025-12-12
updated: 2025-12-25
scores:
  popularity: 5
  difficulty: 4
  usefulness: 6
---

# Error.Record – สร้างระเบียนข้อผิดพลาด

> Error.Record สร้างระเบียนข้อผิดพลาดที่มีข้อมูลโครงสร้าง ด้วย reason, message, detail และ errorCode เ

## คำอธิบาย

Error.Record สร้างระเบียนข้อผิดพลาดที่มีข้อมูลโครงสร้าง ด้วย reason, message, detail และ errorCode เพื่อให้การจัดการข้อผิดพลาดละเอียดขึ้น

## Syntax

```excel
= Error.Record(reason as text, optional message as nullable text, optional detail as any, optional parameters as nullable list, optional errorCode as nullable text) as record
```

## Arguments

| Name | Required | Type | Default | Description |
| --- | --- | --- | --- | --- |
| reason | Yes | text |  | ข้อความที่อธิบายเหตุผลหลักของข้อผิดพลาด (เช่น 'DivideByZero', 'ValidationFailed', 'CustomerNotFound') |
| message | No | nullable text | null | ข้อความรายละเอียดเพิ่มเติมที่อธิบายข้อผิดพลาด ช่วยให้ผู้ใช้เข้าใจสาเหตุ |
| detail | No | any | null | ข้อมูลเพิ่มเติมเกี่ยวกับข้อผิดพลาด สามารถเป็นข้อความ ตัวเลข หรือโครงสร้างอื่น |
| parameters | No | nullable list | null | รายการค่าที่ให้บริบทเพิ่มเติมสำหรับการวินิจฉัยหรือการจัดการแบบโปรแกรมเชิง |
| errorCode | No | nullable text | null | รหัสข้อผิดพลาดที่ระบุตัวตนสำหรับการอ้างอิง (เช่น 'ERR404', 'ERR500') |

## ตัวอย่าง

### 1. สร้างข้อผิดพลาดการหารด้วยศูนย์

```excel
let
    Input = 100,
    Divisor = 0,
    Result = try
        if Divisor = 0 then
            error Error.Record(
                "DivideByZero",
                "ไม่สามารถหารด้วยศูนย์ได้"
            )
        else
            Input / Divisor
    catch (e) => e
in
    Result
```

**ผลลัพธ์:** `[HasError = true, Error = [Reason = "DivideByZero", Message = "ไม่สามารถหารด้วยศูนย์ได้"]]`

สร้างข้อผิดพลาดเมื่อพยายามหารด้วยศูนย์ โดยใช้ Error.Record ร่วมกับ try-catch เพื่อจัดการข้อผิดพลาดแบบมีเงื่อนไข reason คือ 'DivideByZero' ส่วน message คือข้อความที่อธิบายปัญหา

### 2. ข้อผิดพลาดการตรวจสอบข้อมูลพร้อมรหัสข้อผิดพลาด

```excel
let
    CustomerId = 12345,
    Result = try
        if CustomerId > 9999 then
            error Error.Record(
                "CustomerNotFound",
                Text.Format("รหัสลูกค้า #{0} ไม่พบในระบบ", {CustomerId}),
                "ลูกค้าไม่มีอยู่ในฐานข้อมูล",
                {Text.Format("Invalid ID = #{0}", {CustomerId})},
                "ERR404"
            )
        else
            CustomerId
    catch (e) => e
in
    Result
```

**ผลลัพธ์:** `[HasError = true, Error = [Reason = "CustomerNotFound", Message = "รหัสลูกค้า #12345 ไม่พบในระบบ", Detail = "ลูกค้าไม่มีอยู่ในฐานข้อมูล", ErrorCode = "ERR404"]]`

สร้างข้อผิดพลาดที่มีรายละเอียดครบครัน พร้อมทั้ง reason, message ที่มีข้อมูลลูกค้า, detail, parameters, และ errorCode ทำให้สามารถติดตามและจัดหมวดหมู่ข้อผิดพลาดได้ง่าย

### 3. จัดการข้อผิดพลาดของการแปลงประเภทข้อมูล

```excel
let
    Value = "ABC",
    Result = try
        if Value is text and not Number.FromText(Value) then
            error Error.Record(
                "InvalidType",
                "ค่า '" & Value & "' ไม่สามารถแปลงเป็นตัวเลขได้",
                "โปรดตรวจสอบรูปแบบของข้อมูล",
                {Value, "Number"},
                "ERR_INVALID_TYPE"
            )
        else
            Number.FromText(Value)
    catch (e) => e
in
    Result
```

**ผลลัพธ์:** `[HasError = true, Error = [Reason = "InvalidType", Message = "ค่า 'ABC' ไม่สามารถแปลงเป็นตัวเลขได้", Detail = "โปรดตรวจสอบรูปแบบของข้อมูล", ErrorCode = "ERR_INVALID_TYPE"]]`

ใช้ Error.Record ร่วมกับ try-catch เพื่อสร้างข้อผิดพลาดที่กำหนดเองเมื่อการแปลงประเภทข้อมูลล้มเหลว ช่วยให้ผู้ใช้เข้าใจสาเหตุของปัญหาและวิธีแก้ไข

### 4. ข้อผิดพลาดการตรวจสอบความถูกต้องในตารางข้อมูล

```excel
let
    Source = Table.FromRows({{1, "John", 25}, {2, "Jane", -5}, {3, "Bob", 35}}, {"ID", "Name", "Age"}),
    ValidateAge = Table.AddColumn(
        Source,
        "AgeValidation",
        each
            try
                if [Age] < 0 or [Age] > 120 then
                    error Error.Record(
                        "AgeOutOfRange",
                        Text.Format("ชื่อ #{0} มีอายุ #{1} ซึ่งอยู่นอกช่วงที่ยอมรับได้", {[Name], [Age]}),
                        "อายุต้องอยู่ระหว่าง 0 ถึง 120 ปี",
                        {[Age], "0-120"},
                        "ERR_AGE_INVALID"
                    )
                else
                    "Valid"
            catch (e) => e
    )
in
    ValidateAge
```

**ผลลัพธ์:** `ตารางที่แสดงผลการตรวจสอบแต่ละแถว โดยแถวที่ 2 (Jane อายุ -5) จะมี error object ที่มี Reason="AgeOutOfRange" และแถวอื่นแสดง "Valid"`

สร้างการตรวจสอบข้อมูลในตารางโดยใช้ Table.AddColumn และ Error.Record เพื่อเก็บข้อผิดพลาดของแต่ละแถว แต่ละข้อมูลที่ไม่ตรงตามเงื่อนไข (อายุไม่อยู่ในช่วง 0-120) จะสร้าง error record ที่มีบริบทเฉพาะ ช่วยให้ค้นหาข้อมูลที่มีปัญหาได้ง่าย

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

- ผมแนะนำให้ตั้งชื่อ reason ให้สื่อความหมายชัดเจน เช่น 'DivideByZero', 'ValidationFailed', 'CustomerNotFound' เพราะจะช่วยให้ debugging ง่ายขึ้นและสามารถจัดเรียงข้อผิดพลาดตามประเภท

- ใช้ Text.Format() เพื่อแทรกข้อมูลผู้ใช้เข้าไปใน message แทนข้อความคงที่ ตัวอย่าง: "รหัสลูกค้า #{CustomerId} ไม่พบ" ช่วยให้ข้อความเกี่ยวข้องกับข้อมูลจริง

- ส่วนตัวผม มักเก็บ errorCode ไว้เสมอ (เช่น 'ERR404', 'ERR500') เพราะมันช่วยให้การจัดหมวดหมู่ข้อผิดพลาดเป็นไปได้ง่ายขึ้นและสามารถค้นหาได้ในภายหลัง

- ใช้ detail เพื่อให้ข้อมูลบริบทเพิ่มเติม เช่น ค่าของตัวแปรที่ทำให้เกิดข้อผิดพลาด หรือสถานะของระบบ นี่จะช่วยให้ผู้ใช้เข้าใจสาเหตุได้ชัดเจนมากขึ้น

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

**Q: ความแตกต่างระหว่าง Error.Record และ error คืออะไร?**

ผมจะอธิบาย error ใช้สำหรับการแจ้งข้อผิดพลาดแบบง่ายๆ เช่น error "Something went wrong" ส่วน Error.Record ใช้สำหรับสร้างระเบียนข้อผิดพลาดที่มีข้อมูลโครงสร้าง ประกอบด้วย reason, message, detail, parameters และ errorCode ทำให้สามารถจัดการข้อผิดพลาดได้ละเอียดและมีประสิทธิภาพกว่า

**Q: ควรใช้ Error.Record ร่วมกับ try-catch หรือ try-otherwise?**

ผมแนะนำให้ใช้ error Error.Record(...) ภายใน try block แล้ว catch ไว้ เพราะอย่างนี้คุณสามารถเข้าถึงข้อมูลข้อผิดพลาดทั้งหมดที่ Error.Record สร้างให้ได้ ส่วน try-otherwise ใช้สำหรับเงื่อนไขแล้ว จึงไม่เหมาะสำหรับ Error.Record

**Q: พารามิเตอร์ไหนบ้างที่เป็นสิ่งสำคัญจริงๆ?**

ผม reason เป็นพารามิเตอร์บังคับเท่านั้น ส่วนอื่นๆ ทั้งหมดเป็นตัวเลือก แต่ส่วนตัวผม ถ้าต้องสร้างข้อผิดพลาด ผมอยากให้เติม message กับ errorCode ด้วย เพราะมันช่วยให้ผู้ใช้เข้าใจปัญหาได้และสามารถค้นหาจัดหมวดหมู่ข้อผิดพลาดได้ในภายหลัง

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

- [Microsoft Learn - Error.Record](https://learn.microsoft.com/en-us/powerquery-m/error-record) _(official)_
- [Power Query M Language Reference](https://learn.microsoft.com/en-us/powerquery-m/) _(official)_

---

_Source: [https://www.thepexcel.com/functions/power-query/error-handling/error-record/](https://www.thepexcel.com/functions/power-query/error-handling/error-record/)_
