cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
TallPolishAdam
Frequent Visitor

Adding a +1 inside Date function results in an error but when adding +1 outside works fine, why?

So I've been using the following DAX expression for a while now and today was the first day it caused me problems.

 

Week of Report = DATE(YEAR([Date of Report]), MONTH([Date of Report]), DAY([Date of Report]) - WEEKDAY([Date of Report], 2) + 1)

 

The error I get thrown is:

 

MdxScript(Model) (62, 40) Calculation error in measure 'Measure Table'[Week of Report]: An > >argument of function 'DATE' has the wrong data type or the result is too large or too small.

 

Now after some thought I realized maybe this is a problem with going backwards across a month that I've never noticed. The [Date of Report] was 11/1/2022 and so I figured maybe going backwards to 10/31/22 was causing an issue.

Just for testing purposes I removed the + 1 at the end. The code worked!! It correctly output 10/30/22. So why was that little pesky + 1 causing an issue? I decided to try other variations of DAX that should produce the same output. (I actually ended up adopting one of the alternatives but this question still bugged me enough to post it here)

 

Code variations that correctly output 10/31/22:

 

Week of Report = DATE(YEAR([Date of Report]), MONTH([Date of Report]), DAY([Date of Report])) - WEEKDAY([Date of Report], 2) + 1
Week of Report = [Date of Report] - WEEKDAY([Date of Report], 2) + 1

 

So why in the world does the first code variation not work?!? Is this a Power BI bug or is there something else at play?

1 ACCEPTED SOLUTION

Hi @TallPolishAdam ,

 

I think the final two measures should be correct. 

In your first code, you add "- WEEKDAY([Date of Report], 2) + 1" in Date() function. Power BI will check whether year/month/day parts all correct, then return date value. So if day part return Negative numbers, Power BI will return error.

Week of Report =
DATE ( YEAR ( [Date of Report] ), MONTH ( [Date of Report] ), DAY ( [Date of Report] ) - WEEKDAY ( [Date of Report], 2 ) + 1 )

RicoZhou_0-1669973662819.png

The final two measures, both add "- WEEKDAY([Date of Report], 2) + 1" outof date, so Power BI will do calculation on [Date of Report] and think "- WEEKDAY([Date of Report], 2) + 1" as days.

 

Best Regards,
Rico Zhou

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

 

 

View solution in original post

3 REPLIES 3
TallPolishAdam
Frequent Visitor

Yes, that is correct. DATE() would likely end up being 2022/11/0 with the +1 in there which fails. But when I remove the +1 the function correctly outputs 2022/10/30 instead of the expected error that would be associated with 2022/11/-1. I feel like either both should work or neither.

Hi @TallPolishAdam ,

 

I think the final two measures should be correct. 

In your first code, you add "- WEEKDAY([Date of Report], 2) + 1" in Date() function. Power BI will check whether year/month/day parts all correct, then return date value. So if day part return Negative numbers, Power BI will return error.

Week of Report =
DATE ( YEAR ( [Date of Report] ), MONTH ( [Date of Report] ), DAY ( [Date of Report] ) - WEEKDAY ( [Date of Report], 2 ) + 1 )

RicoZhou_0-1669973662819.png

The final two measures, both add "- WEEKDAY([Date of Report], 2) + 1" outof date, so Power BI will do calculation on [Date of Report] and think "- WEEKDAY([Date of Report], 2) + 1" as days.

 

Best Regards,
Rico Zhou

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

 

 

jdbuchanan71
Super User
Super User

@TallPolishAdam 

Just looking at the code, my guess is, because you are adding 1 to a number then subtracting that you are getting some results like 2022/10/-1

Helpful resources

Announcements
Vote for T-Shirt Design

Power BI T-Shirt Design Challenge 2023

Vote for your favorite t-shirt design now through March 28.

March 2023 Update3

Power BI March 2023 Update

Find out more about the March 2023 update.

March Events 2023A

March 2023 Events

Find out more about the online and in person events happening in March!

Top Solution Authors