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”替换为 B 列中的“world”,仅匹配整个值。

使用情况

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

将文本“ur”替换为 B 列中的“or”,匹配值的任何部分。

使用情况

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"]
})

替换器函数