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
harshad_barge
Helper I
Helper I

DAX incorrectly calculating DATEADD condition

Hi all, 
I am still discovering DAX and I am incredibly grateful for the support of this forum so far. I have a problem I dont know why i am not getting an output for just a few of the values. I can really use your help!
I have the following DAX:

 

Position From (Year) = IF('Ordinary Hours'[Calculation Date].[Date] < DATEADD('Ordinary Hours'[Position From Date].[Date],1,YEAR), "Y1",
IF('Ordinary Hours'[Calculation Date].[Date] < DATEADD('Ordinary Hours'[Position From Date].[Date],2,YEAR), "Y2",
IF('Ordinary Hours'[Calculation Date].[Date] < DATEADD('Ordinary Hours'[Position From Date].[Date],3,YEAR), "Y3",
IF('Ordinary Hours'[Calculation Date].[Date] < DATEADD('Ordinary Hours'[Position From Date].[Date],4,YEAR), "Y4",
IF('Ordinary Hours'[Calculation Date].[Date] < DATEADD('Ordinary Hours'[Position From Date].[Date],5,YEAR), "Y5",
IF('Ordinary Hours'[Calculation Date].[Date] < DATEADD('Ordinary Hours'[Position From Date].[Date],6,YEAR), "Y6",
IF('Ordinary Hours'[Calculation Date].[Date] < DATEADD('Ordinary Hours'[Position From Date].[Date],7,YEAR), "Y7",
IF('Ordinary Hours'[Calculation Date].[Date] < DATEADD('Ordinary Hours'[Position From Date].[Date],8,YEAR), "Y8",
IF('Ordinary Hours'[Calculation Date].[Date] < DATEADD('Ordinary Hours'[Position From Date].[Date],9,YEAR), "Y9",
"NA")))))))))

The columns are date time columns as below:

Capture.PNGCapture1.PNG

 

I am getting NA as output for these values when it should be "Y1"
What am I doing wrong here? or is DAX calculating it incorrectly?
@Ashish_Mathur  @danextian  @amitchandak @Greg_Deckler  @PaulDBrown @ImkeF @MFelix 
Thanks,

Harshad

1 ACCEPTED SOLUTION
danextian
Super User
Super User

Hi @harshad_barge ,

 

If you are simply trying to get the difference in years between two dates, you can just use DATEDIFF().

Position from (Year) =
DATEDIFF ( 'Table'[Calculation Date], 'Table'[Position From Date], YEAR )

 










Did I answer your question? Mark my post as a solution!


Proud to be a Super User!









"Tell me and I’ll forget; show me and I may remember; involve me and I’ll understand."
Need Power BI consultation, get in touch with me on LinkedIn or hire me on UpWork.
Learn with me on YouTube @DAXJutsu or follow my page on Facebook @DAXJutsuPBI.

View solution in original post

4 REPLIES 4
amitchandak
Super User
Super User

@harshad_barge 

create a datediff column in year and month and the use switch true(). Both as column

 

diff = datediff('Ordinary Hours'[Calculation Date],'Ordinary Hours'[Position From Date],Year)

switch(true(),
diff < 1, "Y1",
diff < 2, "Y2",
diff < 3, "Y3",
diff < 4, "Y4",
diff < 5, "Y5",
diff < 6, "Y6",
diff < 7, "Y7",
diff < 8, "Y8",
diff < 9, "Y9",
"Y10")
danextian
Super User
Super User

Hi @harshad_barge ,

 

If you are simply trying to get the difference in years between two dates, you can just use DATEDIFF().

Position from (Year) =
DATEDIFF ( 'Table'[Calculation Date], 'Table'[Position From Date], YEAR )

 










Did I answer your question? Mark my post as a solution!


Proud to be a Super User!









"Tell me and I’ll forget; show me and I may remember; involve me and I’ll understand."
Need Power BI consultation, get in touch with me on LinkedIn or hire me on UpWork.
Learn with me on YouTube @DAXJutsu or follow my page on Facebook @DAXJutsuPBI.
amitchandak
Super User
Super User

If the date does not have a timestamp. Remove .date

Ashish_Mathur
Super User
Super User

Hi,

Write this calculated column formula

=IF('Ordinary Hours'[Calculation Date] < DATEADD('Ordinary Hours'[Position From Date],1,YEAR),"Y1",BLANK())


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

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.