cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
babyjb123
Helper II
Helper II

Datadiff returns wrong value

Hi all,

 

I am calculating a simply date difference using Datediff function. But the value I got is wrong. I check the data types of the two date column are date. I still can not find the reason. Please help!

 

Thank you in advance!

Branko 

 

babyjb123_0-1666887510422.png

babyjb123_1-1666887925155.png

 

 

1 ACCEPTED SOLUTION
tamerj1
Super User
Super User

Hi @babyjb123 

if you change the aggregation from SUM to MAX or MIN you will get correct results at the table visual row level but wrong at the total level. 

You have two options. 
first option is to create a measure based on the Date Var column that you have created 

Difference (days) =
SUMX (
    SUMMARIZE ( 'Table', 'Table'[Invoice Date], 'Table'[Service/ Tax Date] ),
    CALCULATE ( MAX ( 'Table'[Date Var] ) )
)

or directly using the following measure 

Difference (days) =
SUMX (
    SUMMARIZE ( 'Table', 'Table'[Invoice Date], 'Table'[Service/ Tax Date] ),
    DATEDIFF ( 'Table'[Service/ Tax Date], 'Table'[Invoice Date], DAY )
)

 

View solution in original post

4 REPLIES 4
tamerj1
Super User
Super User

Hi @babyjb123 

if you change the aggregation from SUM to MAX or MIN you will get correct results at the table visual row level but wrong at the total level. 

You have two options. 
first option is to create a measure based on the Date Var column that you have created 

Difference (days) =
SUMX (
    SUMMARIZE ( 'Table', 'Table'[Invoice Date], 'Table'[Service/ Tax Date] ),
    CALCULATE ( MAX ( 'Table'[Date Var] ) )
)

or directly using the following measure 

Difference (days) =
SUMX (
    SUMMARIZE ( 'Table', 'Table'[Invoice Date], 'Table'[Service/ Tax Date] ),
    DATEDIFF ( 'Table'[Service/ Tax Date], 'Table'[Invoice Date], DAY )
)

 

Hi,

 

Thank you very much for your solution!!!!  But could you tell me the reason, why I can't get right outcoming by simply using DateDiff?

 

Thank you!

@babyjb123 

The problem is not with DATEDIFF. You are creating a calculated column and you have duplicate dates. It depends on your business logic. Actually, the first result you got is arguably correct. If you divide the number of days over how many times the 'Table'[Invoice Date] ; 'Table'[Service/ Tax Date] combination is dupicated in your data, you'll get the same result on my solution. However, it can be argued the first result is the correct one as it aggregates the days for each record. It is up to your business requirement and logic to determine which result is required. 

daXtreme
Super User
Super User

If in doubt... please consult DATEDIFF – DAX Guide

Helpful resources

Announcements
PBI Sept Update Carousel

Power BI September 2023 Update

Take a look at the September 2023 Power BI update to learn more.

Learn Live

Learn Live: Event Series

Join Microsoft Reactor and learn from developers.

Dashboard in a day with date

Exclusive opportunity for Women!

Join us for a free, hands-on Microsoft workshop led by women trainers for women where you will learn how to build a Dashboard in a Day!

MPPC 2023 PBI Carousel

Power Platform Conference-Power BI and Fabric Sessions

Join us Oct 1 - 6 in Las Vegas for the Microsoft Power Platform Conference.

Top Solution Authors
Top Kudoed Authors