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

July 7 - July 17 | Round 2 of the Power BI Dataviz World Championships. Don't miss your chance! Learn more

Reply

Help with complex nested IF function created as Query, with AND and OR operators

Dear BI community. Once again I ask your kind help with the following issue: I have a table which contains a team description, a date, a test result and a region for every purchaser. I'm trying to create a custom function so that I can invoke  it every time I want on any query. It is a really complex function but in order to sum up, the purchaser that meets the following 3 conditions gets the price:

 

1. The team must be either "A" or the test result must be different to "Negative".

 

2. The date must be between january and june.

 

3. The region must be either "Center" or "East Coast".

 

The table looks as follows: The purchasers that would receive the prize are 1, 3, 8 and 11. Below you can see the table and the code I'm trying to use. I've tried with && and  ‖, but it doesn't work either. I'd really appreciate your help guys!!. Thanks.

 

let
   SuperFunction= (Team,Date,Region,Test) =>

if Team = "A" Or (Test <> "Negative") and (Date>= 1/1/2016 and Date<=6/1/2016) and (Region = "Center" or Region = "East Coast") then "Gets Prize" else

"Doesn't get Prize"

in
    SuperFunction

 

 

 

Table sample.jpgAs I told you before, I'd like to be able to invoke the function, from a query and choose the parameters.

 

 

 

1 ACCEPTED SOLUTION
MarcelBeug
Community Champion
Community Champion

In general, and has a higher priority than or.

The keywords if, then, else, and, or are all lowercase.

For date values, the function #date must be used.

 

Based on these rules, my suggestion would be:

 

let
   SuperFunction= (Team as text, Date as date, Region as text, Test as text) =>

if (Team = "A" or Test <> "Negative") and Date>= #date(2016,1,1) and Date<=#date(2016,6,1) and (Region = "Center" or Region = "East Coast") then "Gets Prize" else

"Doesn't get Prize"

in
    SuperFunction

 

I would expect < #date(2016,6,1) instead of <= #date(2016,6,1), so June 1, 2016 would be excluded.

Specializing in Power Query Formula Language (M)

View solution in original post

2 REPLIES 2
MarcelBeug
Community Champion
Community Champion

In general, and has a higher priority than or.

The keywords if, then, else, and, or are all lowercase.

For date values, the function #date must be used.

 

Based on these rules, my suggestion would be:

 

let
   SuperFunction= (Team as text, Date as date, Region as text, Test as text) =>

if (Team = "A" or Test <> "Negative") and Date>= #date(2016,1,1) and Date<=#date(2016,6,1) and (Region = "Center" or Region = "East Coast") then "Gets Prize" else

"Doesn't get Prize"

in
    SuperFunction

 

I would expect < #date(2016,6,1) instead of <= #date(2016,6,1), so June 1, 2016 would be excluded.

Specializing in Power Query Formula Language (M)

Sir, once again, you saved my life!!! Thanks, I guess I'm used to thinking like Excel and there's a long way to master "M" language. I didn't declare variables and I was forgetting about the lowercase. Anyway, I really find this kind of functions really useful for users like I, who don't have any access to complex data bases and need to create this kind of functions in PQ.

 

Thanks sir!

Helpful resources

Announcements
FabCon and SQLCon Barcelona 2026

FabCon & SQLCon – Barcelona 2026

Join us in Barcelona for FabCon and SQLCon, the Fabric, Power BI, SQL, and AI community event. Save €200 with code FABCMTY200.

60 days of Data Days Carousel

Data Days 2026

Join Fabric Data Days 2026: 60 days of free live/on-demand sessions, challenges, study groups, and certification opportunities.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.

Top Solution Authors