Value.ReplaceType แทนที่ประเภทข้อมูล (type) ของค่าด้วยประเภทใหม่ที่กำหนด โดยไม่เปลี่ยนข้อมูลจริง
= Value.ReplaceType(value as any, type as type) as any
= Value.ReplaceType(value as any, type as type) as any
| Argument | Type | Required | Default | Description |
|---|---|---|---|---|
| value | any | Yes | ค่าที่ต้องการแทนที่ประเภท เช่น เรคคอร์ด, ตาราง, หรือค่าอื่นๆ | |
| type | type | Yes | ประเภทใหม่ที่ต้องการกำหนดให้กับค่า |
let source = [Column1 = 123, Column2 = "text"], replacedType = Value.ReplaceType( source, type [Column1 = number, Column2 = text] ) in replacedType= let
source = [Column1 = 123, Column2 = "text"],
replacedType = Value.ReplaceType(
source,
type [Column1 = number, Column2 = text]
)
in
replacedType
เรคคอร์ดที่มีประเภท Column1 = number, Column2 = text
Type.RecordFields( Value.Type( Value.ReplaceType( [Amount = 500], type [Amount = number] ) ) )[Amount][Type]= Type.RecordFields(
Value.Type(
Value.ReplaceType(
[Amount = 500],
type [Amount = number]
)
)
)[Amount][Type]
type number
let sourceTable = Table.FromRows( {{1, "Alice"}, {2, "Bob"}}, {"ID", "Name"} ), replacedTable = Value.ReplaceType( sourceTable, type table [ID = number, Name =…= let
sourceTable = Table.FromRows(
{{1, "Alice"}, {2, "Bob"}},
{"ID", "Name"}
),
replacedTable = Value.ReplaceType(
sourceTable,
type table [ID = number, Name = text]
)
in
replacedTable
ตารางที่มีโครงสร้างประเภท ID = number, Name = text
Value.ReplaceType แทนที่ประเภทข้อมูล (type definition) ของค่า ส่วน Value.ReplaceMetadata แทนที่เมตาดาต้า (metadata) เช่น หมายเหตุ, คุณสมบัติในการแสดงผล โดยไม่เปลี่ยนประเภท
ไม่ เฉพาะการบอก Power Query ว่าจะประมวลผลข้อมูลเป็นประเภทไหน เพียงแต่ข้อมูลจริง (ค่า) ยังเหมือนเดิม
ได้ คุณสามารถแทนที่ประเภทตาราง (type table) เพื่อกำหนดชนิดของแต่ละคอลัมน์
เมื่อคุณต้องการให้ Power Query รู้ว่าข้อมูลจากแหล่งต่างๆ ควรถูกปฏิบัติเป็นประเภทเดียวกัน หรือเมื่อต้องการแน่ใจว่าโครงสร้างข้อมูลตรงกับที่คาดหวัง
Value.ReplaceType ใช้สำหรับแทนที่ประเภทข้อมูลของค่าต่างๆ เช่น record, table หรือรูปแบบอื่นๆ ด้วยประเภทใหม่ที่ชัดเจน ฟังก์ชันนี้เป็นเครื่องมือสำคัญสำหรับกำหนดโครงสร้างชนิดข้อมูล (schema) และให้ความปลอดภัยเกี่ยวกับชนิดข้อมูล
ที่เจ๋งคือ Value.ReplaceType ช่วยให้คุณสามารถบอก Power Query ว่าข้อมูลนี้ควรถูกปฏิบัติเป็นประเภทไหน ซึ่งมีประโยชน์มากเมื่อต้องทำงานกับเรคคอร์ด (record) หรือตารางที่มีโครงสร้างที่ชัดเจน
ส่วนตัวผม มักใช้ Value.ReplaceType เมื่อต้องแน่ใจว่าข้อมูลจากแหล่งต่างๆ มีรูปแบบชนิดข้อมูลเหมือนกัน ก่อนที่จะรวมไว้ด้วยกัน ทำให้โปรแกรมไม่สับสนกับชนิดข้อมูลที่ไม่ตรงกัน