Condividi tramite


Table.ReplaceValue

Sintassi

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

Informazioni

Sostituisce un valore con un nuovo valore nelle colonne specificate di una tabella.

  • table: La tabella da cercare.
  • oldValue: valore da sostituire.
  • newValue: valore di sostituzione.
  • replacer: funzione replacer da usare. La funzione può essere quella di Replacer.ReplaceText sostituire il testo originale con nuovo testo, Replacer.ReplaceValue per sostituire il valore originale con un nuovo valore o un sostituiscitore personalizzato.
  • columnsToSearch: elenco contenente la colonna o le colonne specifiche della tabella in cui cercare il valore da sostituire.

Esempio 1

Sostituire il testo "goodbye" con "world" nella colonna B, con corrispondenza solo per il valore intero.

Utilizzo

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

Output

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

Esempio 2

Sostituire il testo "ur" con "or" nella colonna B, con corrispondenza per qualsiasi parte del valore.

Utilizzo

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

Output

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

Esempio 3

Rendi anonimi i nomi dei dipendenti degli Stati Uniti.

Utilizzo

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

Output

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

Esempio 4

Rendi anonime tutte le colonne dei dipendenti degli Stati Uniti.

Utilizzo

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

Output

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

Funzioni di sostituzione