在单个 Power BI Step / M 语句中更改字段类型和多个精确值替换

Change field type and multiple exact value replacements in a single Power BI Step / M Statement

这是我开始使用 Power BI 和 M 代码的第一天。我浏览了十几个替换线程,但找不到我要找的东西。

我目前正在使用以下代码(多个步骤)将字段类型更改为文本,然后将 3 个准确值替换为另一个值。我正在努力以更 efficent/elegant 的方式(一步)来做到这一点。

#"Changed Type" = Table.TransformColumnTypes(#"Renamed Columns",{{"EU Member State", type text}}),
#"Replaced Value" = Table.ReplaceValue(#"Changed Type","0","Unknown",Replacer.ReplaceValue,{"EU Member State"}),
#"Replaced Value1" = Table.ReplaceValue(#"Changed Type","1","Yes",Replacer.ReplaceValue,{"EU Member State"}),
#"Replaced Value2" = Table.ReplaceValue(#"Replaced Value","2","No",Replacer.ReplaceValue,{"EU Member State"})

如有任何帮助,我们将不胜感激!

注意:我不想使用查找 table。

谢谢!

周杰伦

Table.replace 一次只能执行一个替换,因此您必须使用替代方法在单个 m 查询中执行多个替换,这是解决方案,如果有帮助,请接受:)

#"Changed Type" = 'xxformula',
    #"Replaced Value" =(
    let
    Source1 = Table.FromColumns({{"0","1","2"}}),
    Substitutions = [
        #"0" = "unknown",
        #"1" = "yes",
        2 = "no"],
    Substituted = Table.TransformColumns(#"Changed Type", 
        {{"Name", each Record.FieldOrDefault(Substitutions, _, _)}})
    in
    Substituted
    )
    
    in
    #"Replaced Value"

之前:

之后: