แชร์ผ่าน


Table.ReplaceValue

ไวยากรณ์

Table.ReplaceValue(
    table as table,
    oldValue as any,
    newValue as any,
    replacer as function,
    columnsToSearch as list
) as table

เกี่ยวกับ

แทนที่ค่าด้วยค่าใหม่ในคอลัมน์ที่ระบุของตาราง

  • table: ตารางที่จะค้นหา
  • oldValue: ค่าที่จะแทนที่
  • newValue: ค่าการแทนที่
  • replacer: ฟังก์ชันแทนที่ที่จะใช้ ฟังก์ชันนี้สามารถ Replacer.ReplaceText แทนที่ข้อความต้นฉบับด้วยข้อความ Replacer.ReplaceValue ใหม่เพื่อแทนที่ค่าเดิมด้วยค่าใหม่หรือตัวแทนที่แบบกําหนดเอง
  • columnsToSearch: รายการที่มีคอลัมน์หรือคอลัมน์เฉพาะในตารางเพื่อค้นหาค่าที่จะแทนที่

ตัวอย่าง 1

แทนที่ข้อความ "goodbye" ด้วย "world" ในคอลัมน์ B ซึ่งจับคู่เฉพาะค่าทั้งหมด

การใช้งาน

Table.ReplaceValue(
    Table.FromRecords({
        [A = 1, B = "hello"],
        [A = 2, B = "goodbye"],
        [A = 3, B = "goodbyes"]
    }),
    "goodbye",
    "world",
    Replacer.ReplaceValue,
    {"B"}
)

เอาท์พุท

Table.FromRecords({
    [A = 1, B = "hello"],
    [A = 2, B = "world"],
    [A = 3, B = "goodbyes"]
})

ตัวอย่าง 2

แทนที่ข้อความ "your" ด้วย "or" ในคอลัมน์ B ซึ่งจับคู่ส่วนใดส่วนหนึ่งของค่า

การใช้งาน

Table.ReplaceValue(
    Table.FromRecords({
        [A = 1, B = "hello"],
        [A = 2, B = "wurld"]
    }),
    "ur",
    "or",
    Replacer.ReplaceText,
    {"B"}
)

เอาท์พุท

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

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

ปิดเสียงเรียกชื่อของพนักงานในสหรัฐอเมริกา

การใช้งาน

Table.ReplaceValue(
    Table.FromRecords({
        [Name = "Cindy", Country = "US"],
        [Name = "Bob", Country = "CA"]
    }),
    each if [Country] = "US" then [Name] else false,
    each Text.Repeat("*", Text.Length([Name])),
    Replacer.ReplaceValue,
    {"Name"}
)

เอาท์พุท

Table.FromRecords({
    [Name = "*****", Country = "US"],
    [Name = "Bob", Country = "CA"]
})

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

ซ่อนตัวคอลัมน์ทั้งหมดของพนักงานในสหรัฐอเมริกา

การใช้งาน

Table.ReplaceValue(
    Table.FromRecords({
        [Name = "Cindy", Country = "US"],
        [Name = "Bob", Country = "CA"]
    }),
    each [Country] = "US",
    "?",
    (currentValue, isUS, replacementValue) =>
        if isUS then
            Text.Repeat(replacementValue, Text.Length(currentValue))
        else
            currentValue,
    {"Name", "Country"}
)

เอาท์พุท

Table.FromRecords({
    [Name = "?????", Country = "??"],
    [Name = "Bob", Country = "CA"]
})

ฟังก์ชันตัวแทนที่