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
ben-t
Frequent Visitor

How to reference date column in measure

 

I'm trying to calculate the difference between 2 date/times. 

But if the field is blank, i want it to reference the earliest & last dates in the slicer

 

 I have a seperate measure that already selects the first and last dates, but it doesnt work properly in measure or calculated columns

 

DATEDIFF(

if(ISBLANK(MachAvail[ACTSTART]),[Min Date],MachAvail[ACTSTART]),

if(ISBLANK(MachAvail[ACTFINISH]),[Max Date],MachAvail[ACTFINISH]), 
SECOND)
 

bent_1-1627016665411.png

 

bent_3-1627016777226.png

bent_0-1627017010642.png

bent_0-1627017098483.png

 

 

 

any advise would be appreciated

 

 

 

1 ACCEPTED SOLUTION
v-stephen-msft
Community Support
Community Support

Hi @ben-t ,

 

This error is because in the measure, you cannot write the field directly. You need to use MAX or MIN to get the current row of the field.

Screenshot 2021-07-27 141345.png

 

Try to add MAX/MIN in the error field of the red curve.

Hrs measure =
DATEDIFF (
    IF (
        ISBLANK ( MAX ( 'MachAvail'[ACTSTART] ) ),
        [Min Date],
        MAX ( ' MachAvail'[ACTSTART] )
    ),
    IF (
        ISBLANK ( MAX ( 'MachAvail'[ACTFINISH] ) ),
        [Max Date],
        MAX ( 'MachAvail'[ACTFINISH] )
    ),
    SECOND
)

 

 

 

Best Regards,

Stephen Tao

 

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

4 REPLIES 4
v-stephen-msft
Community Support
Community Support

Hi @ben-t ,

 

This error is because in the measure, you cannot write the field directly. You need to use MAX or MIN to get the current row of the field.

Screenshot 2021-07-27 141345.png

 

Try to add MAX/MIN in the error field of the red curve.

Hrs measure =
DATEDIFF (
    IF (
        ISBLANK ( MAX ( 'MachAvail'[ACTSTART] ) ),
        [Min Date],
        MAX ( ' MachAvail'[ACTSTART] )
    ),
    IF (
        ISBLANK ( MAX ( 'MachAvail'[ACTFINISH] ) ),
        [Max Date],
        MAX ( 'MachAvail'[ACTFINISH] )
    ),
    SECOND
)

 

 

 

Best Regards,

Stephen Tao

 

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

 

Hi @v-stephen-msft ,

I am facing the similar kind of error in which i am creating a Measure and comapring date column (Deliver Date) with today date and getting the he same error as mentioned below :

Request you to please help how can i compare my date value column with Today date in Measure where i do not want to take Min or Max .
Please see below :

Column.JPG

Thanks ,

Ashish

Thanks, figured out the error and got another workaround to get the datediff

Hrs =SUMX(MachAvail,DATEDIFF(MachAvail[ACTSTART],MachAvail[ACTFINISH],SECOND))

amitchandak
Super User
Super User

@ben-t , This need to a column like

datediff((MachAvail[ACTSTART]), (MachAvail[ACTFINISH]), second)

 

Or in case measure , but to sum or Avg you need to have key column
datediff(Min(MachAvail[ACTSTART]), Max(MachAvail[ACTFINISH]), second)

 

Averagex(Values(MachAvail[ID]) ,datediff(Min(MachAvail[ACTSTART]), Max(MachAvail[ACTFINISH]), second))

 

https://community.powerbi.com/t5/Community-Blog/Decoding-Direct-Query-in-Power-BI-Part-2-Date-Differ...

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.