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

The FabCon + SQLCon recap series starts April 14th at 8am Pacific. If you’re tracking where AI is going inside Fabric, this first session is a can't miss. Register now

Reply
cholton
Frequent Visitor

Date.AddDays with a Date.DayOfWeek based calculation gives cannot convert the value null to type Num

This step of my query

#"Added Custom" = Table.AddColumn(#"Last step", "Ref date", each Date.AddDays([StoredDate],-(Date.DayOfWeek([StoredDate])-5)-7)),

gives

[Expression.Error] We cannot convert the value null to type Number.

I can see this by using the column filter arrow and choosing load more. The Expression.Error then appears below the list of values. The error doesn't stop query processing until several steps later when I try to expand a merge that relies on a derivative field based off the Ref date. (I checked data types and for nulls on both sides of the merge and the field I want to expand before finding this hidden error several steps prior.)

 

The complete query was running when it was based off of a different date, but I can't find any problems with the new StoredDate field. It doesn't show any errors. I have replaced nulls with a dummy date. It is formatted as date field.

I've been unsuccessful finding this particular problem solved. Grateful for help.

1 ACCEPTED SOLUTION
edhans
Community Champion
Community Champion

Several ways to fix. You can wrap the whole thing in an if/then/else statement.

 

#"Added Custom" = Table.AddColumn(#"Last step", "Ref date", each if [StoredDate] = null then null else Date.AddDays([StoredDate],-(Date.DayOfWeek([StoredDate])-5)-7)),

 

Or if it evaluates to error, return null

 

#"Added Custom" = Table.AddColumn(#"Last step", "Ref date", each try Date.AddDays([StoredDate],-(Date.DayOfWeek([StoredDate])-5)-7) otherwise null),

 

You could also replace the null in [StoredDate] with 0, but while that will not produce an error, it will give you bogus dates as 0 is Dec 31, 1899.



Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

DAX is for Analysis. Power Query is for Data Modeling


Proud to be a Super User!

MCSA: BI Reporting

View solution in original post

3 REPLIES 3
edhans
Community Champion
Community Champion

Several ways to fix. You can wrap the whole thing in an if/then/else statement.

 

#"Added Custom" = Table.AddColumn(#"Last step", "Ref date", each if [StoredDate] = null then null else Date.AddDays([StoredDate],-(Date.DayOfWeek([StoredDate])-5)-7)),

 

Or if it evaluates to error, return null

 

#"Added Custom" = Table.AddColumn(#"Last step", "Ref date", each try Date.AddDays([StoredDate],-(Date.DayOfWeek([StoredDate])-5)-7) otherwise null),

 

You could also replace the null in [StoredDate] with 0, but while that will not produce an error, it will give you bogus dates as 0 is Dec 31, 1899.



Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

DAX is for Analysis. Power Query is for Data Modeling


Proud to be a Super User!

MCSA: BI Reporting

Thank you. No nulls and no visible row errors in [StoredDate], but try/otherwise worked so there was something.

edhans
Community Champion
Community Champion

Glad to help @cholton 

 

try/otherwise has saved me several times as well.



Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

DAX is for Analysis. Power Query is for Data Modeling


Proud to be a Super User!

MCSA: BI Reporting

Helpful resources

Announcements
New to Fabric survey Carousel

New to Fabric Survey

If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.

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.

Join our Fabric User Panel

Join our Fabric User Panel

Share feedback directly with Fabric product managers, participate in targeted research studies and influence the Fabric roadmap.

March Power BI Update Carousel

Power BI Community Update - March 2026

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