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

July 7 - July 17 | Round 2 of the Power BI Dataviz World Championships. Don't miss your chance! Learn more

Reply
Anonymous
Not applicable

DAX for date difference

Hi,

I have 2 dates "Start Date" and "End Date", I have to create a new column with the difference of these 2 dates as highlighled below.

neelofarshama_0-1620326591394.png

I want the new column to show up as 4/1/2021-4/30/2021=1 and 7/1/2021-9/30/2021=3  

I am using DAX DATEDIFF(Opportunity[Start Date].[Date],Opportunity[End Date].[Date],MONTH) for this but its not including the current start month for 4/1/2021-4/30/2021=0 but I want it to show up as 1.
 
Please help.
 
 
1 ACCEPTED SOLUTION

Hi @Anonymous ,

so here is a third variation:

 

07-05-_2021_17-00-01.png

 

Difference in Month = 
VAR _StartLessThanEnd =
IF (
    OR ( 'Table'[Start], 'Table'[End] ) = BLANK (),
    BLANK (),
    DATEDIFF ( 'Table'[Start], 'Table'[End], MONTH ) + 1
)
VAR _EndLessThanStart =
IF ( 
    OR ( 'Table'[Start], 'Table'[End] ) = BLANK (),
    BLANK (),
    DATEDIFF ( 'Table'[End], 'Table'[Start], MONTH ) + 1
)
RETURN
    if ( 'Table'[Start] < 'Table'[End], _StartlessThanEnd, _EndLessThanStart)

 

With kind regards from the town where the legend of the 'Pied Piper of Hamelin' is at home
FrankAT (Proud to be a Datanaut)

 

View solution in original post

7 REPLIES 7
FrankAT
Community Champion
Community Champion

Hi @Anonymous 

if the absolute number in 'Test Term' is correct and you want only to get reed of the negativ sign then use the following measure:

Difference in Month =
IF (
    OR ( 'Table'[Start], 'Table'[End] ) = BLANK (),
    BLANK (),
    ABS( DATEDIFF ( 'Table'[Start], 'Table'[End], MONTH ) + 1 )
)

With kind regards from the town where the legend of the 'Pied Piper of Hamelin' is at home
FrankAT (Proud to be a Datanaut)

 

Anonymous
Not applicable

Hi @FrankAT ,

Thak you for the prompt reply but the value is also wrong for these dates

start date =01-19-2022 -end date =03-31-2021  =  11 months but its giving -9

neelofarshama_0-1620385284220.png

 

Hi @Anonymous ,

so here is a third variation:

 

07-05-_2021_17-00-01.png

 

Difference in Month = 
VAR _StartLessThanEnd =
IF (
    OR ( 'Table'[Start], 'Table'[End] ) = BLANK (),
    BLANK (),
    DATEDIFF ( 'Table'[Start], 'Table'[End], MONTH ) + 1
)
VAR _EndLessThanStart =
IF ( 
    OR ( 'Table'[Start], 'Table'[End] ) = BLANK (),
    BLANK (),
    DATEDIFF ( 'Table'[End], 'Table'[Start], MONTH ) + 1
)
RETURN
    if ( 'Table'[Start] < 'Table'[End], _StartlessThanEnd, _EndLessThanStart)

 

With kind regards from the town where the legend of the 'Pied Piper of Hamelin' is at home
FrankAT (Proud to be a Datanaut)

 

FrankAT
Community Champion
Community Champion

Hi @Anonymous 

adjust your formula like this:

 

06-05-_2021_21-17-22.png

 

= DATEDIFF('Table'[Start],'Table'[End],MONTH) + 1

With kind regards from the town where the legend of the 'Pied Piper of Hamelin' is at home
FrankAT (Proud to be a Datanaut)

Anonymous
Not applicable

Hi @FrankAT,

I cannot use +1 at the end blanks in start and end dtae columns and they would appear as 1 in my new column.

Hi @Anonymous ,

use the following solution:

 

06-05-_2021_22-05-59.png

 

Difference in Month =
IF (
    OR ( 'Table'[Start], 'Table'[End] ) = BLANK (),
    BLANK (),
    DATEDIFF ( 'Table'[Start], 'Table'[End], MONTH ) + 1
)

With kind regards from the town where the legend of the 'Pied Piper of Hamelin' is at home
FrankAT (Proud to be a Datanaut)

Anonymous
Not applicable

Hi @FrankAT ,

This is working but there is an issue with dates whose end date(day) is greater than start date(day) its givning negative values as highlighted below

neelofarshama_0-1620384092615.png

 

Helpful resources

Announcements
FabCon and SQLCon Barcelona 2026

FabCon & SQLCon – Barcelona 2026

Join us in Barcelona for FabCon and SQLCon, the Fabric, Power BI, SQL, and AI community event. Save €200 with code FABCMTY200.

60 days of Data Days Carousel

Data Days 2026

Join Data Days 2026: 60 days of free live/on-demand sessions, challenges, study groups, and certification opportunities.

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.

Top Solution Authors
Top Kudoed Authors