แชร์ผ่าน


Table.TransformColumns

ไวยากรณ์

Table.TransformColumns(table as table, transformOperations as list, optional defaultTransformation as nullable function, optional missingField as nullable number) as table

เกี่ยวกับ

แปลงข้อมูล table โดยใช้การดําเนินการแต่ละคอลัมน์ที่แสดงอยู่ใน transformOperations (ซึ่งรูปแบบคือ { ชื่อคอลัมน์ การแปลง } หรือ { ชื่อคอลัมน์ การแปลง ชนิดคอลัมน์ใหม่ }) defaultTransformationถ้ามีการระบุ จะถูกนําไปใช้กับคอลัมน์ทั้งหมดที่ไม่ได้แสดงอยู่ในtransformOperations หากคอลัมน์ที่แสดงใน transformOperations ไม่มีอยู่ ข้อยกเว้นจะแสดงขึ้นมา เว้นแต่ว่าพารามิเตอร์ missingField ที่เลือกได้จะระบุทางเลือก (ตัวอย่างเช่น MissingField.UseNull หรือ MissingField.Ignore)

ตัวอย่างที่ 1

แปลงค่าข้อความในคอลัมน์ [A] เป็นค่าตัวเลขและค่าตัวเลขในคอลัมน์ [B] เป็นค่าข้อความ

การใช้งาน

Table.TransformColumns(
    Table.FromRecords({
        [A = "1", B = 2],
        [A = "5", B = 10]
    }),
    {
        {"A", Number.FromText},
        {"B", Text.From}
    }
)

เอาท์พุท

Table.FromRecords({
    [A = 1, B = "2"],
    [A = 5, B = "10"]
})

ตัวอย่าง 2

แปลงค่าตัวเลขในคอลัมน์ [X] ที่หายไปให้เป็นค่าข้อความ โดยไม่สนใจคอลัมน์ที่ไม่มีอยู่

การใช้งาน

Table.TransformColumns(
    Table.FromRecords({
        [A = "1", B = 2],
        [A = "5", B = 10]
    }),
    {"X", Number.FromText},
    null,
    MissingField.Ignore
)

เอาท์พุท

Table.FromRecords({
    [A = "1", B = 2],
    [A = "5", B = 10]
})

ตัวอย่างที่ 3

แปลงค่าตัวเลขในคอลัมน์ [X] ที่หายไปเป็นค่าข้อความ โดยกําหนดค่าเริ่มต้นเป็น null สําหรับคอลัมน์ที่ไม่มีอยู่

การใช้งาน

Table.TransformColumns(
    Table.FromRecords({
        [A = "1", B = 2],
        [A = "5", B = 10]
    }),
    {"X", Number.FromText},
    null,
    MissingField.UseNull
)

เอาท์พุท

Table.FromRecords({
    [A = "1", B = 2, X = null],
    [A = "5", B = 10, X = null]
})

ตัวอย่างที่ 4

เพิ่มค่าตัวเลขในคอลัมน์ [B] และแปลงเป็นค่าข้อความ และแปลงคอลัมน์อื่นทั้งหมดเป็นตัวเลข

การใช้งาน

Table.TransformColumns(
    Table.FromRecords({
        [A = "1", B = 2],
        [A = "5", B = 10]
    }),
    {"B", each Text.From(_ + 1), type text},
    Number.FromText
)

เอาท์พุท

Table.FromRecords({
    [A = 1, B = "3"],
    [A = 5, B = "11"]
})