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

Vote for your favorite vizzies from the Power BI Dataviz World Championship submissions. Vote now!

Reply
Anonymous
Not applicable

Using Parameter for SQL IF statement in WHERE clause

Example:

I have a table with 3 columns (ItemCode, CustCode, ItemGroupType) that I filter using a parameter (dynamic as this is populated by users on adhoc basis) which works a treat to return all Items assigned to that Customer....

WHERE CustCode = '"&UserCustCode&"'

 

Now however if a user enters a particular code (say 10666 (formatted as Text)) I want to return results based on ItemGroupCode rather than CustCode so M code would look like...

= Table.SelectRows(Source, each (if UserCustCode = "10666" then [ItemGroupCode] = "090" else [CustCode] = UserCustCode))

and this gets the desired results.

 

So my question is how do I move this into an "SQL format" so I don't have to bring the entire dataset back and filter in PQ? I've googled but can't find a use case of using a PQ parameter within an IF statement for MySQL.

I tried to guess the logic as my SQL knowledge is very limited...

where IF('"&UserCustCode&"' = '10666', ItemGroupCode = '090', CustCode = '"&UserCustCode&"')

but get an error 😞

Syntax error in SQL statement at or about "= '10666', ItemGroupCode = '090', CustCo" (10713)

 

For extra points a solution that has an IF ELSE type scenario would be ideal as I have at least 2 CustCodes I want this to apply to!

 

Thanks,

2 REPLIES 2
lbendlin
Super User
Super User

= Table.SelectRows(Source, each (if UserCustCode = "10666" then [ItemGroupCode] = "090" else [CustCode] = UserCustCode))

and this gets the desired results.

I doubt that. This is not the right syntax for Table.SelectRows.

 

In SQL you can use similar logical conditions

 

WHERE a=b or c=d

 

Learn SQL, it's not that difficult as it is close to natural language.

Anonymous
Not applicable

@lbendlin seems to be giving me the desired results unless I'm missing something??

 

CustCode 10666 in my parameter returning only 090 regardless of customer:

JamesRobson_2-1682079952991.png

 

 

 

CustCode 10480 returning all their items regardless of type (they only happened to have 001)

JamesRobson_0-1682079330065.png

Not sure how to utilise the where & or clause as I want to say IF this then filter based on A else filter based on B don't I?

As for learning SQL I appreicate that but I have the IF clause: 

IF(<condition>, <value if true>, <value if false>)

 but using that format with the PQ parameter seems to be my issue.

 

Thanks,

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

Vote for your favorite vizzies from the Power BI World Championship submissions!

Sticker Challenge 2026 Carousel

Join our Community Sticker Challenge 2026

If you love stickers, then you will definitely want to check out our Community Sticker Challenge!

January Power BI Update Carousel

Power BI Monthly Update - January 2026

Check out the January 2026 Power BI update to learn about new features.

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.