Search code examples
powerbidaxmpowerbi-datasourcepowerbi-desktop

power bi switch is unrecognized in query


Have used the following code to create a custom column but its stuck at the an expression error that it is unable to recognize the SWITCH function :

= 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()
))

I have tried removing the quotations, changing the column names but all to no avail.Please Advice. Thanks in Advance!


Solution

  • You've mixed up M and DAX. They are two different languages, and both are used in Power BI. SWITCH() is a DAX function, hence it cannot be used in the M query that you're writing.

    You can replace the SWITCH logic with an if-then-else expression in M:

    = 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

    Depends on exactly what you want to achieve, other functions can be used, but I'll keep it this way for now instead of making assumptions.