Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Get certified in Microsoft Fabric—for free! For a limited time, get a free DP-600 exam voucher to use by the end of 2024. Register now

Reply
Anonymous
Not applicable

ISSUE with Switch

Hi 

 

I am trying to select multiple text criteria from a column in power query. I thought switch would work but this does not work, but works outside of it. 

 

The data is as follows 

 

Farm

Farmer

Grower

Producer 

Manager 

Director 

owner 

 

Im trying to choose Farm, Grower, manager, director and flag these with a one in a new column

 

I have tried the below:

 

SWITCH(
([Job role.Job Role]),

"Director",1,
"Grower",1,
"Manager",1,
0)

 

But this dont work and I get the below error:

 

Expression.Error: The name 'SWITCH' wasn't recognized. Make sure it's spelled correctly.

 

Please help with either the formula or suggest an alternative as i have 20 selections to make in my report, multiple times.

1 ACCEPTED SOLUTION
edhans
Super User
Super User

There is no SWITCH() function in Power Query. All Power Query functions have the type and function name with a period, so Text.Start(), Number.From(), DateTime.Date(), etc. 

 

SWITCH is DAX only. In Power Query, you'd need to use nested if/then/else statements, or use a custom function that does SWITCH functionality, but I would advise against that unless you are pretty good in Power Query.

 

If you can share what you are tryting to do, we can assist getting you started with if/then/else.

EDIT: Saw your sample logic. Try something like this:

= if [Job Role] = "Director" then 1
else if [Job Role] = "Grower" then 1
else if [Job Role] = "Manager" then 1
else null

or this if you have a default you want to use beside a null value.

if [Job Role] = "Director" then 1
else if [Job Role] = "Grower" then 1
else if [Job Role] = "Manager" then 1
else 2

  



Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

DAX is for Analysis. Power Query is for Data Modeling


Proud to be a Super User!

MCSA: BI Reporting

View solution in original post

2 REPLIES 2
v-eachen-msft
Community Support
Community Support

Hi @Anonymous ,

 

If you want some codes like SWITCH(), you could refer to this blog.

https://blog.crossjoin.co.uk/2014/03/10/conditional-logic-in-power-query/

 

Community Support Team _ Eads
If this post helps, then please consider Accept it as the solution to help the other members find it.
edhans
Super User
Super User

There is no SWITCH() function in Power Query. All Power Query functions have the type and function name with a period, so Text.Start(), Number.From(), DateTime.Date(), etc. 

 

SWITCH is DAX only. In Power Query, you'd need to use nested if/then/else statements, or use a custom function that does SWITCH functionality, but I would advise against that unless you are pretty good in Power Query.

 

If you can share what you are tryting to do, we can assist getting you started with if/then/else.

EDIT: Saw your sample logic. Try something like this:

= if [Job Role] = "Director" then 1
else if [Job Role] = "Grower" then 1
else if [Job Role] = "Manager" then 1
else null

or this if you have a default you want to use beside a null value.

if [Job Role] = "Director" then 1
else if [Job Role] = "Grower" then 1
else if [Job Role] = "Manager" then 1
else 2

  



Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

DAX is for Analysis. Power Query is for Data Modeling


Proud to be a Super User!

MCSA: BI Reporting

Helpful resources

Announcements
November Carousel

Fabric Community Update - November 2024

Find out what's new and trending in the Fabric Community.

Live Sessions with Fabric DB

Be one of the first to start using Fabric Databases

Starting December 3, join live sessions with database experts and the Fabric product team to learn just how easy it is to get started.

Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Early Bird pricing ends December 9th.

Nov PBI Update Carousel

Power BI Monthly Update - November 2024

Check out the November 2024 Power BI update to learn about new features.