power bi switch在查询中无法识别

时间:2018-04-08 17:16:47

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

使用以下代码创建自定义列,但它卡在一个无法识别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()
))

我试过删除引号,更改列名但是都没有用。请咨询。在此先感谢!

2 个答案:

答案 0 :(得分:3)

你混淆了M and DAX。它们是两种不同的语言,都在Power BI中使用。 SWITCH()是DAX函数,因此无法在您正在撰写的M query中使用。

您可以使用M:

中的if-then-else expression替换SWITCH逻辑
= 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 "")

result

取决于你想要实现的目标,可以使用其他功能,但我现在将保持这种方式,而不是做出假设。

答案 1 :(得分:0)

将Employees列表存储在表中并将其与您的查询合并会好得多。您可以在查询中生成表格 - 例如:

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 /名称存储在单独的表中,并以相同的方式加入。