Thep Excel

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

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

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

ความนิยม4/10
ความยาก7/10
ประโยชน์5/10

อัปเดต 12 December 2025

Syntax & Arguments

อาร์กิวเมนต์ ชนิด คำอธิบาย
value any ค่าที่ต้องการแทนที่ประเภท เช่น เรคคอร์ด, ตาราง, หรือค่าอื่นๆ
type type ประเภทใหม่ที่ต้องการกำหนดให้กับค่า

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 อีก 😎

Examples

ตัวอย่างที่ 1: แทนที่ประเภทเรคคอร์ดด้วยประเภทที่ชัดเจนlet Source = [Column1 = 123, Column2 = "text"], TypedRecord = Value.ReplaceType( Source, type [Column1 = number, Column2 = text] ) in TypedRecord

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

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

ผลลัพธ์[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

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

ผลลัพธ์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 ทำงานได้ดี

fx
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
ตัวอย่างที่ 4: สร้าง Strongly-Typed Record Listlet 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

fx
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"]}
ตัวอย่างที่ 5: ตรวจสอบ Optional Field ใน Typelet 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

fx
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

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

ฟังก์ชันที่ผู้เขียนโยงไว้กับ Value.ReplaceType จัดกลุ่มตามหมวด · ชี้ที่ชื่อใดชื่อหนึ่ง แล้วฟังก์ชันที่ใช้คู่กันจะมีจุดทอง

ฟังก์ชันนี้Value.ReplaceTypeValue functions
Value functions (2)
Value.ReplaceMetadata

Value.ReplaceMetadata ใช้สำหรับการประมวลผล

Value.Type

Value.Type คืนค่า Type ของข้อมูลที่กำหนด ช่วยตรวจสอบว่าเป็น number, text, date, table, list หรือชนิดอื่นๆ มีประโยชน์มากสำหรับ data validation และการเขียน dynamic functions

List functions (1)
List.Transform

List.Transform ใช้สำหรับสร้างลิสต์ใหม่โดยนำฟังก์ชันไปประมวลผลแต่ละตัวในลิสต์เดิม

Table functions (1)
Table.FromRows

Table.FromRows สร้างตารางจาก List ของ List โดยแต่ละ List ย่อยแทนข้อมูลในแถวหนึ่ง คล้ายกับการ transpose ข้อมูลแบบแถว-คอลัมน์ แต่ทำให้เป็นตารางจริงในทันที

Type functions (1)
Type.RecordFields

Type.RecordFields ใช้สำหรับการประมวลผล

Comments

อีเมลของคุณจะไม่ถูกเผยแพร่