Power BI查询中SWITCH函数未被识别,自定义列表达式报错求助
Fixing the SWITCH Function Error in Power BI Power Query Custom Column
Looks like you ran into a classic mix-up between DAX and Power Query M language syntax—plus a tiny spelling error that was adding to the confusion. Let's break down what went wrong and fix it:
Key Issues in Your Original Code
- Wrong SWITCH Syntax for M Language: Unlike DAX, Power Query's
SWITCHdoesn't use assignment expressions (likeEmpolyees = "Chris1") in the result branches. You just need to return the value you want the new column to have directly. - Typos: You misspelled "Employees" as "Empolyees" (missing the letter 'y') in both the new column name and the branch expressions.
- Incorrect Result Structure: Each pairing in M's
SWITCHshould be[Matching Value], [Result Value], not a conditional assignment.
Corrected Code
= Table.AddColumn(#"Removed Columns", "Employees", each SWITCH( [Index], 1, "Chris1", 2, "Chris2", 3, "Chris3", 4, "Chris4", 5, "Chris5", 6, "Chris6", 7, "Chris7", 8, "Chris8", BLANK() ) )
What Changed?
- Fixed the Spelling: Renamed the new column and corrected all references to "Employees" (no more missing 'y').
- Adjusted SWITCH Branches: Instead of writing
Empolyees = "ChrisX", we just return the string value"ChrisX"directly—this is how M language assigns values to the new column. - Preserved Valid Structure: Kept the core logic of matching the
[Index]column values to your desired names, withBLANK()as the fallback for unlisted index numbers.
A quick reminder: If you were trying to create a DAX calculated column instead of a Power Query custom column, your original syntax would be closer to correct (with the spelling fix), but since you're using Table.AddColumn, we're working in M language here—hence the syntax differences.
内容的提问来源于stack exchange,提问作者Cosq
相关产品推荐
相关产品推荐

