共用方式為


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

以資料行 B 中的「world」取代文字「goodbye」,僅比對整個值。

使用方式

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

以資料行 B 中的「or」取代文字「ur」,比對值的任何部分。

使用方式

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

替換函數