---
title: Value.ReplaceType – แทนที่ประเภทข้อมูลของค่า
url: https://www.thepexcel.com/functions/power-query/value-functions/value-replacetype/
type: function-explainer
program: Power Query
syntax: "= Value.ReplaceType(value as any, type as type) as any"
date: 2025-12-12
updated: 2025-12-25
scores:
  popularity: 4
  difficulty: 7
  usefulness: 5
---

# Value.ReplaceType – แทนที่ประเภทข้อมูลของค่า

> Value.ReplaceType แทนที่ประเภทข้อมูล (type) ของค่าด้วยประเภทใหม่ที่กำหนด โดยไม่เปลี่ยนข้อมูลจริง

## คำอธิบาย

Value.ReplaceType แทนที่ประเภทข้อมูล (type) ของค่าด้วยประเภทใหม่ที่กำหนด โดยไม่เปลี่ยนข้อมูลจริง

## Syntax

```excel
= Value.ReplaceType(value as any, type as type) as any
```

## Arguments

| Name | Required | Type | Default | Description |
| --- | --- | --- | --- | --- |
| value | Yes | any |  | ค่าที่ต้องการแทนที่ประเภท เช่น เรคคอร์ด, ตาราง, หรือค่าอื่นๆ |
| type | Yes | type |  | ประเภทใหม่ที่ต้องการกำหนดให้กับค่า |

## ตัวอย่าง

### 1. ตัวอย่างที่ 1: แทนที่ประเภทเรคคอร์ดด้วยประเภทที่ชัดเจน

```excel
let
    Source = [Column1 = 123, Column2 = "text"],
    TypedRecord = Value.ReplaceType(
        Source,
        type [Column1 = number, Column2 = text]
    )
in
    TypedRecord
```

**ผลลัพธ์:** `[Column1 = 123, Column2 = "text"]`

สร้างเรคคอร์ดแล้วแทนที่ประเภทของมันด้วยประเภทที่กำหนดชัดเจน ทำให้ Power Query รู้ว่าแต่ละช่องควรเป็นประเภทไหน ค่าจริงไม่เปลี่ยน เพียงแต่ Type Metadata

### 2. ตัวอย่างที่ 2: ตรวจสอบประเภทหลังจากแทนที่

```excel
let
    Source = [Amount = 500],
    TypedRecord = Value.ReplaceType(Source, type [Amount = number]),
    RecordFields = Type.RecordFields(Value.Type(TypedRecord))
in
    RecordFields[Amount][Type]
```

**ผลลัพธ์:** `type number`

แทนที่ประเภทของเรคคอร์ดแล้วใช้ Value.Type และ Type.RecordFields เพื่อตรวจสอบและยืนยันว่าช่อง Amount มีประเภท number

### 3. ตัวอย่างที่ 3: แทนที่ประเภทตารางด้วยรูปแบบที่ชัดเจน

```excel
let
    SourceTable = Table.FromRows(
        {{1, "Alice"}, {2, "Bob"}},
        {"ID", "Name"}
    ),
    TypedTable = Value.ReplaceType(
        SourceTable,
        type table [ID = number, Name = text]
    )
in
    TypedTable
```

**ผลลัพธ์:** `ตารางที่มีโครงสร้างประเภท ID = number, Name = text`

แทนที่ประเภทตารางเพื่อให้ Power Query รู้ว่า ID ควรเป็น number และ Name ควรเป็น text ช่วยให้ IntelliSense และ Type Checking ทำงานได้ดี

### 4. ตัวอย่างที่ 4: สร้าง Strongly-Typed Record List

```excel
let
    Source = {{1, "Alice"}, {2, "Bob"}, {3, "Carol"}},
    ToRecords = List.Transform(Source, each [ID = _{0}, Name = _{1}]),
    RecordType = type [ID = number, Name = text],
    TypedRecords = List.Transform(ToRecords, each Value.ReplaceType(_, RecordType))
in
    TypedRecords
```

**ผลลัพธ์:** `{[ID = 1, Name = "Alice"], [ID = 2, Name = "Bob"], [ID = 3, Name = "Carol"]}`

แปลง List ของ Tuples เป็น List ของ Records จากนั้นใช้ Value.ReplaceType เพื่อบังคับให้แต่ละ Record มี Type ที่ชัดเจน ทำให้List มี Consistent Type

### 5. ตัวอย่างที่ 5: ตรวจสอบ Optional Field ใน Type

```excel
let
    Source = [Name = "John", Age = 30],
    RecordType = type [Name = text, optional Age = number],
    TypedRecord = Value.ReplaceType(Source, RecordType),
    Fields = Type.RecordFields(Value.Type(TypedRecord))
in
    Fields[Age][Optional]
```

**ผลลัพธ์:** `true`

สร้าง Record และใช้ Value.ReplaceType กับ Type ที่มี optional Field (Age) จากนั้นตรวจสอบด้วย Type.RecordFields เพื่อยืนยันว่า Age เป็น Optional Field

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

- ส่วนตัวผมแนะนำ ใช้ Value.ReplaceType ในทุกที่ที่ต้องรวม (merge) ข้อมูลจากแหล่งต่างๆ มันช่วยจับ Type Mismatch ได้เร็ว แทนที่จะค้นหาบัก 2-3 ชั่วโมงต่อมา

- ตรวจสอบ Type หลังแทนที่ด้วย Type.RecordFields กับ Value.Type ทำให้แน่ใจว่าทุกช่องถูกตั้งค่า Type ถูกต้อง เช็คก่อนใช้ไป

- ใช้ let...in structure เมื่อทำงานกับ Type ซ้ำๆ ยาวมากครับ ถ้าเขียนแบบ nested ที่เดียว อ่านไม่รู้เรื่องจะ 😅

- ระวังนะ Type ที่คุณใช้ต้องตรงกับข้อมูลจริง ถ้า Schema ไม่ตรง ลำต้องจะคนหาบัก แล้วจะหาว่ามันเกิดจากไหน เหน็บหัวมากจริง

- ใช้ type [field = type] สำหรับ Record, type table [field = type] สำหรับ Table เลือกให้ถูกนะ ไม่งั้นจะ error

- สร้าง Custom Function ด้วย Value.ReplaceType ทำให้ IntelliSense แสดง Type ได้ชัดเจน ผู้ใช้จะรู้ว่า Function คาดหวังพารามิเตอร์ประเภทไหน ดีมากครับ 😎

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

**Q: ต่างจาก Value.ReplaceMetadata ยังไง?**

เจอคำถามนี้บ่อยครับ 😅 Value.ReplaceType แทนที่ 'ประเภท' ของข้อมูล (type definition) ส่วน Value.ReplaceMetadata แทนที่ 'ตัวอักษร' (metadata) เช่น ชื่อแสดงผล, รูปแบบวันที่ ข้อมูลจริงไม่เปลี่ยน Type ก็ไม่เปลี่ยนเหมือนกัน

**Q: ข้อมูลจริงเปลี่ยนไหมเมื่อใช้ Value.ReplaceType?**

ไม่เลยครับ เฉพาะ Type Tag ที่บอก Power Query ว่า "ค่า 123 นี้คือ number" เท่านั้น ค่า 123 ยังคงเป็น 123 เหมือนเดิม ไม่มีการแปลงค่า

**Q: ใช้กับตารางได้ไหม?**

ได้ห้อยครับ ใช้ได้กับ table, record, list หรือค่าใดๆ ก็ได้ แค่ต้องมี Type ที่ตรงกับโครงสร้างจริง

**Q: มีประโยชน์เมื่อไหร่ถึงจริง?**

เมื่อต้องรวม (merge) ข้อมูลจากหลายแหล่ง หรือสร้าง Custom Function ที่ต้องการให้ Type ชัดเจน ลองไม่ใช้แล้ว debug Type Error บ่อยเหว่ 😭 ใช้ Value.ReplaceType แล้วมันจะจับปัญหาเร็ว

**Q: ต่างจาก Type Conversion ยังไง?**

คนส่วนใหญ่งง เรื่องนี้ครับ Value.ReplaceType แค่บอก Type ไม่เปลี่ยนค่า ส่วน Type Conversion (เช่น Number.FromText("123")) เปลี่ยนค่าจริง "123" ตัวอักษร เป็น 123 ตัวเลข ReplaceType = ติดป้ายให้ Conversion = เปลี่ยนจริง

**Q: Type Parameter เป็น Text String ได้ไหม?**

ไม่ได้นะครับ ต้องเป็น Type Value เช่น type [Amount = number] หรือ type table [ID = number] โปรแกรมต้องเข้าใจ Type ของคุณ จึงไม่สามารถ parse จาก Text String ได้

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

- [Microsoft Learn: Value.ReplaceType](https://learn.microsoft.com/en-us/powerquery-m/value-replacetype) _(official)_
- [Microsoft Learn: Value.Type](https://learn.microsoft.com/en-us/powerquery-m/value-type) _(official)_
- [Microsoft Learn: Type.RecordFields](https://learn.microsoft.com/en-us/powerquery-m/type-recordfields) _(official)_
- [Microsoft Learn: Value.ReplaceMetadata](https://learn.microsoft.com/en-us/powerquery-m/value-replacemetadata) _(official)_
- [Microsoft Learn: Types and type conversion](https://learn.microsoft.com/en-us/powerquery-m/powerquery-m-type-system) _(official)_
- [Power Query M Language - Type Functions](https://learn.microsoft.com/en-us/powerquery-m/power-query-m-function-reference#type-functions) _(official)_
- [Power Query M Language - Value Functions](https://learn.microsoft.com/en-us/powerquery-m/power-query-m-function-reference#value-functions) _(official)_

---

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