【问题标题】:power bi switch is unrecognized in query查询中无法识别 power bi 开关
【发布时间】:2018-09-18 02:54:49
【问题描述】:

已使用以下代码创建自定义列,但它卡在无法识别 SWITCH 函数的表达式错误:

= Table.AddColumn(#"Removed Columns", "Empolyees", each SWITCH([Index],  
1, Empolyees = "Chris1",   
2, Empolyees = "Chris2",
3, Empolyees = "Chris3",
4, Empolyees = "Chris4", 
5, Empolyees = "Chris5",
6, Empolyees = "Chris6",
7, Empolyees = "Chris7",
8, Empolyees = "Chris8",
BLANK()
))

我试过删除引号,更改列名,但都无济于事。请指教。提前致谢!

【问题讨论】:

    标签: powerbi dax m powerbi-datasource powerbi-desktop


    【解决方案1】:

    你搞错了M and DAX。它们是两种不同的语言,都在 Power BI 中使用。 SWITCH() 是一个 DAX 函数,因此它不能在您正在编写的 M query 中使用。

    您可以将 SWITCH 逻辑替换为 M 中的 if-then-else expression

    = Table.AddColumn(#"Removed Columns", "Employees", each if [Index] = 1 then "Chris1" else if [Index] = 2 then "Chris2" else if [Index] = 3 then "Chris3" else if [Index] = 4 then "Chris4" else if [Index] = 5 then "Chris5" else if [Index] = 6 then "Chris6" else if [Index] = 7 then "Chris7" else if [Index] = 8 then "Chris8" else "")
    

    具体取决于你想要实现的目标,可以使用其他功能,但我暂时保持这种方式,而不是做任何假设。

    【讨论】:

    • 嗨@clement-ong 这到底有助于解决问题吗?如果是这样,您可以accept an answer 积极结束问题。
    【解决方案2】:

    最好将员工列表存储在一个表中,然后将其与您的查询合并。您可以在查询中生成一个表 - 例如:

    let
        Source = Excel.CurrentWorkbook(){[Name="MyTable"]}[Content],
        TempTable = #table(
            {"ID","Name"},{
                {1,"Employee 1"},
                {2,"Employee 2"},
                {3,"Employee 3"},
                {4,"Employee 4"},
                {5,"Employee 5"}
                }
            ),
        #"Merged Queries" = Table.NestedJoin(Source,{"ID"},TempTable,{"ID"},"Join",JoinKind.LeftOuter),
        #"Expanded Join" = Table.ExpandTableColumn(#"Merged Queries", "Join", {"Name"}, {"Name"})
    in
        #"Expanded Join"
    

    更好的做法是将员工 ID/姓名存储在单独的表中,并以相同的方式加入。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-02-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-29
      相关资源
      最近更新 更多