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

Score big with last-minute savings on the final tickets to FabCon Vienna. Secure your discount

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
August Power BI Update Carousel

Power BI Monthly Update - August 2025

Check out the August 2025 Power BI update to learn about new features.

August 2025 community update carousel

Fabric Community Update - August 2025

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

Top Solution Authors