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
akhil148265
Frequent Visitor

Data difference in the same column including hours

Hi,

 

My date and time present in the same column, i wanted the difference of date and hours in the power bi.

I have explained in the excel file, need to achieve the 'Time Taken' field in the poer bi ( The logic field is the requirement).

 

Thanks in advance.😊 Raw requirement.png

 

Regards,

Akhil.

 

1 ACCEPTED SOLUTION
Mariusz
Community Champion
Community Champion

Hi @akhil148265 

 

You can add calculated column like below.

Column = 
VAR __current = 'Table'[Status Date Time]
VAR __previous = CALCULATE(
    MAX( 'Table'[Status Date Time] ),
    ALLEXCEPT( 'Table', 'Table'[WorkItemId] ),
    'Table'[Status Date Time] < __current 
)
VAR __weekends = 
    COUNTROWS(
        FILTER(
            CALENDAR( 
                IF( NOT ISBLANK( __previous ), __previous,  __current ), 
                __current ),
            WEEKDAY( [Date], 2 ) IN { 6, 7 } ) 
    ) - 2
    
RETURN IF( 
    NOT ISBLANK( __previous ), 
    __current - __previous - __weekends 
)

Also, see the attached file for reference.

 

Best Regards,
Mariusz

If this post helps, then please consider Accepting it as the solution.


 

View solution in original post

4 REPLIES 4
v-gizhi-msft
Community Support
Community Support

Hi,

 

According to your description, I create a table to test:

81.png

I have two ways to calculate Time Taken by column and measure:

Please take following steps:

 

By column:

1)Create a column to get original time’s last time:

Last Date Time - Column =

CALCULATE(MAX('Test'[Status Date Time]),FILTER('Test','Test'[Status Date Time] < EARLIER('Test'[Status Date Time]) && 'Test'[WorkitemID] = EARLIER('Test'[WorkitemID])))

 

2)Create a column to calculate Time Taken:

Time Taken - Column = DATEDIFF(Test[Last Date Time - Column],Test[Status Date Time],SECOND)/(3600*24)

 

By measure:

1)Create a measure to get original time’s last time:

Last Date Time - Measure =

CALCULATE(MAX('Test'[Status Date Time]),FILTER(ALLSELECTED('Test'),'Test'[Status Date Time] < CALCULATE(MIN('Test'[Status Date Time]),'Test') && 'Test'[WorkitemID] in FILTERS('Test'[WorkitemID])))

 

2)Create a measure to calculate Time Taken:

Time Taken - Measure = DATEDIFF([Last Date Time - Measure],MIN('Test'[Status Date Time]),SECOND)/(3600*24)

 

And the above two ways’ results show:

82.png

Here is my test pbix file:

pbix 

 

Best Regards,

Giotto Zhi

Mariusz
Community Champion
Community Champion

Hi @akhil148265 

 

You can add calculated column like below.

Column = 
VAR __current = 'Table'[Status Date Time]
VAR __previous = CALCULATE(
    MAX( 'Table'[Status Date Time] ),
    ALLEXCEPT( 'Table', 'Table'[WorkItemId] ),
    'Table'[Status Date Time] < __current 
)
VAR __weekends = 
    COUNTROWS(
        FILTER(
            CALENDAR( 
                IF( NOT ISBLANK( __previous ), __previous,  __current ), 
                __current ),
            WEEKDAY( [Date], 2 ) IN { 6, 7 } ) 
    ) - 2
    
RETURN IF( 
    NOT ISBLANK( __previous ), 
    __current - __previous - __weekends 
)

Also, see the attached file for reference.

 

Best Regards,
Mariusz

If this post helps, then please consider Accepting it as the solution.


 

Hi @Mariusz,

 

The logic which you have provided works for the different dates, but if the status changes at the same 'date and time' the result gives incorrectly. Could you please help me with this😊.

 

Attached is the screnario ( Raw Data).

screenshot2.png

 

Regards,

Akhil.

 

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.