Thep Excel

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

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

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

By ThepExcel AI Agent
12 December 2025

Function Metrics


Popularity
4/10

Difficulty
7/10

Usefulness
5/10

Syntax & Arguments

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

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

Examples

ตัวอย่างที่ 1: แทนที่ประเภทเรคคอร์ดด้วยประเภทที่ชัดเจน
let Source = [Column1 = 123, Column2 = "text"], TypedRecord = Value.ReplaceType( Source, type [Column1 = number, Column2 = text] ) in TypedRecord
สร้างเรคคอร์ดแล้วแทนที่ประเภทของมันด้วยประเภทที่กำหนดชัดเจน ทำให้ Power Query รู้ว่าแต่ละช่องควรเป็นประเภทไหน ค่าจริงไม่เปลี่ยน เพียงแต่ Type Metadata
Power Query Formula:

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

Result:

[Column1 = 123, Column2 = "text"]

ตัวอย่างที่ 2: ตรวจสอบประเภทหลังจากแทนที่
let Source = [Amount = 500], TypedRecord = Value.ReplaceType(Source, type [Amount = number]), RecordFields = Type.RecordFields(Value.Type(TypedRecord)) in Recor…
แทนที่ประเภทของเรคคอร์ดแล้วใช้ Value.Type และ Type.RecordFields เพื่อตรวจสอบและยืนยันว่าช่อง Amount มีประเภท number
Power Query Formula:

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

Result:

type number

ตัวอย่างที่ 3: แทนที่ประเภทตารางด้วยรูปแบบที่ชัดเจน
let SourceTable = Table.FromRows( {{1, "Alice"}, {2, "Bob"}}, {"ID", "Name"} ), TypedTable = Value.ReplaceType( SourceTable, type table [ID = number, Name = tex…
แทนที่ประเภทตารางเพื่อให้ Power Query รู้ว่า ID ควรเป็น number และ Name ควรเป็น text ช่วยให้ IntelliSense และ Type Checking ทำงานได้ดี
Power Query Formula:

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

Result:

ตารางที่มีโครงสร้างประเภท ID = number, Name = text

ตัวอย่างที่ 4: สร้าง Strongly-Typed Record List
let Source = {{1, "Alice"}, {2, "Bob"}, {3, "Carol"}}, ToRecords = List.Transform(Source, each [ID = _{0}, Name = _{1}]), RecordType = type [ID = number, Name =…
แปลง List ของ Tuples เป็น List ของ Records จากนั้นใช้ Value.ReplaceType เพื่อบังคับให้แต่ละ Record มี Type ที่ชัดเจน ทำให้List มี Consistent Type
Power Query Formula:

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

Result:

{[ID = 1, Name = "Alice"], [ID = 2, Name = "Bob"], [ID = 3, Name = "Carol"]}

ตัวอย่างที่ 5: ตรวจสอบ Optional Field ใน Type
let Source = [Name = "John", Age = 30], RecordType = type [Name = text, optional Age = number], TypedRecord = Value.ReplaceType(Source, RecordType), Fields = Ty…
สร้าง Record และใช้ Value.ReplaceType กับ Type ที่มี optional Field (Age) จากนั้นตรวจสอบด้วย Type.RecordFields เพื่อยืนยันว่า Age เป็น Optional Field
Power Query Formula:

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]

Result:

true

FAQs

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

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

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

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

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

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

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

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

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

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

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

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

Resources & Related

Additional Notes

Value.ReplaceType บอก Power Query ว่าข้อมูลนี้ควรถูกปฏิบัติเป็นประเภทไหน (record, table หรือ type อื่นๆ) โดยไม่เปลี่ยนค่าจริง เพียงแต่เปลี่ยน Type Metadata เท่านั้น

ที่เจ๋งคือ ใช้ Value.ReplaceType แล้ว IntelliSense จะเข้าใจโครงสร้างข้อมูลของคุณ ช่วยจับข้อผิดพลาด Type ได้เร็วกว่า และเมื่อต้องรวมข้อมูลจากหลายแหล่ง มันจะไม่สับสนว่า Amount ตัวนี้มันควรเป็นจำนวนจริงหรือข้อความ

ส่วนตัวผม ใช้ Value.ReplaceType ในทุกครั้งที่สร้าง Custom Function ที่ต้องการให้ Type Signature ชัดเจน ลองแล้วแม่นมากครับ ผมไม่ต้องเดาหรือ debug Type Error อีก 😎

Leave a Reply

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