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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
jasonyeung87
Helper V
Helper V

Detecting null dates in Power Query

Hi,

 

I have the following function in Power Query that returns the fiscal quarter, based on the month of the date passed.

= (AnyDate as date) =>
let
    // CurrentDate = AnyDate,
    CurrentMonth = if AnyDate = null then 13
		else Date.Month(AnyDate),
    CurrentQuarter = if CurrentMonth = 1 then "Q4" 
		else if CurrentMonth = 2 then "Q1" 
		else if CurrentMonth = 3 then "Q1" 
		else if CurrentMonth = 4 then "Q1" 
		else if CurrentMonth = 5 then "Q2" 
		else if CurrentMonth = 6 then "Q2" 
		else if CurrentMonth = 7 then "Q2" 
		else if CurrentMonth = 8 then "Q3" 
		else if CurrentMonth = 9 then "Q3" 
		else if CurrentMonth = 10 then "Q3" 
		else if CurrentMonth = 11 then "Q4" 
		else if CurrentMonth = 12 then "Q4"
		else "Unknown" 
	,
    CurrentFiscalQuarter = CurrentQuarter
        in
    CurrentFiscalQuarter

It works fine if there is a valid date is passed to the function. (Our fiscal quarters start in Feb instead of Jan).

 

I want the function to return "Unknown" if the date passed is null (e.g. If "AnyDate" is null, then set the CurrentMonth value to the dummy value of 13 so the CurrentQuarter goes to the "else" part.). I'm using this function in a custom column and some of the dates in the recordset is null.

 

However I get the following error message:

We cannot convert the value null to type Date.

 

I was wondering how do I check whether the date is null in the above function. 

 

Jason

 

1 ACCEPTED SOLUTION
OwenAuger
Super User
Super User

Hi @jasonyeung87 

To fix the immediate issue, change

(AnyDate as date)

to

(AnyDate as nullable date)

I won't pretend to be an expert on this, but this ensures that null values of AnyDate can be handled.

 

See this article from Ben Gribaudo.

 

Regards,

Owen

 


Owen Auger
Did I answer your question? Mark my post as a solution!
Blog
Twitter
LinkedIn

View solution in original post

2 REPLIES 2
jasonyeung87
Helper V
Helper V

Hi Owen,

 

thanks for your help!

 

Sincerely,

 

Jason

 

OwenAuger
Super User
Super User

Hi @jasonyeung87 

To fix the immediate issue, change

(AnyDate as date)

to

(AnyDate as nullable date)

I won't pretend to be an expert on this, but this ensures that null values of AnyDate can be handled.

 

See this article from Ben Gribaudo.

 

Regards,

Owen

 


Owen Auger
Did I answer your question? Mark my post as a solution!
Blog
Twitter
LinkedIn

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

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

April Fabric Community Update

Fabric Community Update - April 2024

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