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

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

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

Result:

เรคคอร์ดที่มีประเภท Column1 = number, Column2 = text

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

= Type.RecordFields(
        Value.Type(
            Value.ReplaceType(
                [Amount = 500],
                type [Amount = number]
            )
        )
    )[Amount][Type]

Result:

type number

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

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

Result:

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

FAQs

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

Value.ReplaceType แทนที่ประเภทข้อมูล (type definition) ของค่า ส่วน Value.ReplaceMetadata แทนที่เมตาดาต้า (metadata) เช่น หมายเหตุ, คุณสมบัติในการแสดงผล โดยไม่เปลี่ยนประเภท

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

ไม่ เฉพาะการบอก Power Query ว่าจะประมวลผลข้อมูลเป็นประเภทไหน เพียงแต่ข้อมูลจริง (ค่า) ยังเหมือนเดิม

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

ได้ คุณสามารถแทนที่ประเภทตาราง (type table) เพื่อกำหนดชนิดของแต่ละคอลัมน์

มีประโยชน์เมื่อไหร่?

เมื่อคุณต้องการให้ Power Query รู้ว่าข้อมูลจากแหล่งต่างๆ ควรถูกปฏิบัติเป็นประเภทเดียวกัน หรือเมื่อต้องการแน่ใจว่าโครงสร้างข้อมูลตรงกับที่คาดหวัง

Resources & Related

Additional Notes

Value.ReplaceType ใช้สำหรับแทนที่ประเภทข้อมูลของค่าต่างๆ เช่น record, table หรือรูปแบบอื่นๆ ด้วยประเภทใหม่ที่ชัดเจน ฟังก์ชันนี้เป็นเครื่องมือสำคัญสำหรับกำหนดโครงสร้างชนิดข้อมูล (schema) และให้ความปลอดภัยเกี่ยวกับชนิดข้อมูล

ที่เจ๋งคือ Value.ReplaceType ช่วยให้คุณสามารถบอก Power Query ว่าข้อมูลนี้ควรถูกปฏิบัติเป็นประเภทไหน ซึ่งมีประโยชน์มากเมื่อต้องทำงานกับเรคคอร์ด (record) หรือตารางที่มีโครงสร้างที่ชัดเจน

ส่วนตัวผม มักใช้ Value.ReplaceType เมื่อต้องแน่ใจว่าข้อมูลจากแหล่งต่างๆ มีรูปแบบชนิดข้อมูลเหมือนกัน ก่อนที่จะรวมไว้ด้วยกัน ทำให้โปรแกรมไม่สับสนกับชนิดข้อมูลที่ไม่ตรงกัน

Leave a Reply

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