cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
mdrammeh Member
Member

IF Statement using Power Query

Hello,

 

I am trying to write an IF statement to capture the following dates. I have and excel spreadsheet withs millions of records. For one of the columns in my spreadsheet, I want to create a new column to capture all DT_SCHED (date field) prior to June 1, 2017 and return a "Delivery Date of June 1st" and if the DT_SCHED date is greater than June 1st but less than December 31st, I want my IF statement to return "December 1st" delivery date.  

 

Could anyone help me figure this out? I don't have the Power Query Add-In to do a conditional lookup but have attached a screenshot with my first attempt but apparently the formula is VERY WRONG 🙂DELIVERY DATE.PNG

1 ACCEPTED SOLUTION

Accepted Solutions
MarcelBeug Super Contributor
Super Contributor

Re: IF Statement using Power Query

Try this code:

 

= if [DT_SCHED] < #date(2017,6,1) then "DELIVERY DATE JUNE 1ST" else if [DT_SCHED] < #date(2017,12,31) then "DELIVERY DATE DECEMBER 1ST" else null

 

PQ is case sensitive and if...then...else is all lower case, just like null.

 

Also mind your boundaries: in your information, June 1st is undefined; in the code above it returns December 1.

Likewise, December 31, 2017 will return null, according to the specifications provided.

 

How can you have millions of rows in Excel? As far as I know, Excel has a maximum of 1,048,576 rows

Specializing in Power Query Formula Language (M)

View solution in original post

14 REPLIES 14
Sean Super Contributor
Super Contributor

Re: IF Statement using Power Query

In the Query Editor try the Add Conditional Column feature

 

Add Column tab => Conditional Column button (then the code will be generated for you)

dkay84_PowerBI New Contributor
New Contributor

Re: IF Statement using Power Query

The syntax for date values in Power Query is #date(year,month,day).

Thus, your if then statement would say if [Date] <= #date(2017,6,1) then ...
Phil_Seamark Super Contributor
Super Contributor

Re: IF Statement using Power Query

Agreed, the Syntax of your IF statement is good.

 

Here is a nice breakdown on the subtle differences betwen the Excel IF and the Power BI IF

 

http://www.excelguru.ca/blog/2014/08/27/the-if-function-in-power-query/


To learn more about DAX visit : aka.ms/practicalDAX

Proud to be a Datanaut!

MarcelBeug Super Contributor
Super Contributor

Re: IF Statement using Power Query

Try this code:

 

= if [DT_SCHED] < #date(2017,6,1) then "DELIVERY DATE JUNE 1ST" else if [DT_SCHED] < #date(2017,12,31) then "DELIVERY DATE DECEMBER 1ST" else null

 

PQ is case sensitive and if...then...else is all lower case, just like null.

 

Also mind your boundaries: in your information, June 1st is undefined; in the code above it returns December 1.

Likewise, December 31, 2017 will return null, according to the specifications provided.

 

How can you have millions of rows in Excel? As far as I know, Excel has a maximum of 1,048,576 rows

Specializing in Power Query Formula Language (M)

View solution in original post

Phil_Seamark Super Contributor
Super Contributor

Re: IF Statement using Power Query

Perhaps the millions of rows are in Power Pivot??

 

In anycase, if you have millions of rows, perhaps it's a good time to get a database. 🙂


To learn more about DAX visit : aka.ms/practicalDAX

Proud to be a Datanaut!

mdrammeh Member
Member

Re: IF Statement using Power Query

I miss spoke Marcel. It's the database that has millions of queries and that's where am going in to filter by date range. You filter worked perfectly. THANK YOU, YOU GUYS ARE AWESOME!!

marcoselias
Advisor

Re: IF Statement using Power Query

Sean,

 

Thanks for pointing this out. It seems like it's going to help me but how can I do an AND or an OR to combine conditions? It looks like it only accepts one rule at a time. I'm trying to do something like:

IF <condition1> AND <condition2> THEN <result> ELSE <alternate result>

 

I hope I don't need to resort to creating multiple conditional columns.

 

Thanks,

Marcos

 

dkay84_PowerBI New Contributor
New Contributor

Re: IF Statement using Power Query

You can use AND/OR you just have to fully qualify it:
if <something> = <something> and <something> = <something else> then...
Highlighted
marcoselias
Advisor

Re: IF Statement using Power Query

Thanks! My question was within the Add Column > Conditional Column section but your answer helped me within the Add Column > Custom Column  🙂

Helpful resources

Announcements
October 2019 Community Highlights

October 2019 Community Highlights

October was a busy month in the community. Read the recap article to learn about some of the events and content.

New Badges

Incoming: New and Improved Badges

Exciting news: We've given our badges an overhaul and added brand news ones.

Ask Amir Anything

Exclusive LIVE Community Event No. 2 – Ask Amir Anything

Next in our Triple A series: Ask Amir Netz questions about the latest updates, features and future.

Analytics in Azure virtual event

Analytics in Azure virtual event

Experience a limitless analytics service built to ingest, prep, manage, and serve data for immediate use in Power BI.

Users Online
Currently online: 369 members 3,946 guests
Please welcome our newest community members: