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

Don't miss out! 2025 Microsoft Fabric Community Conference, March 31 - April 2, Las Vegas, Nevada. Use code MSCUST for a $150 discount. Prices go up February 11th. Register now.

Reply
ToddMate
Helper II
Helper II

Calculation Duration with IF statement

Hi All,

Can you please help me out. I need to calculate a duration with the following IF considered.

if "index" = 1 then date_replied - date created else
if "index > 1 then date_replied - date replied from the row prior (current index - 1)

ToddMate_0-1655174913310.png

 

I would love to do this in both a DAX and also within Power Query using custom column as i will be looking to average this duration out. 

Thanks in advance for any assistance provided.

Todd

 

1 ACCEPTED SOLUTION
Samarth_18
Community Champion
Community Champion

Hi @ToddMate ,

You could create a column as below:-

 

Column =
VAR current_index = 'Table (2)'[index]
VAR prev_index = current_index - 1
VAR current_ticket = [ticket_number]
VAR prev_date_rep =
    CALCULATE (
        MAX ( 'Table (2)'[date replied] ),
        FILTER (
            'Table (2)',
            'Table (2)'[index] = prev_index
                && 'Table (2)'[ticket_number] = current_ticket
        )
    )
VAR result =
    IF (
        current_index = 1,
        DATEDIFF ( 'Table (2)'[date created], 'Table (2)'[date replied], SECOND ),
        DATEDIFF ( prev_date_rep, 'Table (2)'[date replied], SECOND )
    )
VAR _Seconds = result
VAR _Minutes =
    INT ( _Seconds / 60 )
VAR _RemainingSeconds =
    MOD ( _Minutes, 60 )
VAR _Hours =
    INT ( _Minutes / 60 )
VAR _RemainingMinutes =
    MOD ( _Minutes, 60 )
VAR _Days =
    INT ( _Hours / 24 )
VAR _RemainingHours =
    MOD ( _Hours, 24 )
RETURN
    FORMAT ( _Days, "00" ) & ":"
        & FORMAT ( _RemainingHours, "00" ) & ":"
        & FORMAT ( _RemainingMinutes, "00" ) & ":"
        & FORMAT ( _RemainingSeconds, "00" )

Output:-

Samarth_18_0-1655183827416.png

Note:- Minutes and seconds values are vary since i have not added seconds data in the dataset.

 

Best Regards,
Samarth

If this post helps, please consider accepting it as the solution to help the other members find it more quickly.
Appreciate your Kudos!!
Connect on Linkedin

View solution in original post

1 REPLY 1
Samarth_18
Community Champion
Community Champion

Hi @ToddMate ,

You could create a column as below:-

 

Column =
VAR current_index = 'Table (2)'[index]
VAR prev_index = current_index - 1
VAR current_ticket = [ticket_number]
VAR prev_date_rep =
    CALCULATE (
        MAX ( 'Table (2)'[date replied] ),
        FILTER (
            'Table (2)',
            'Table (2)'[index] = prev_index
                && 'Table (2)'[ticket_number] = current_ticket
        )
    )
VAR result =
    IF (
        current_index = 1,
        DATEDIFF ( 'Table (2)'[date created], 'Table (2)'[date replied], SECOND ),
        DATEDIFF ( prev_date_rep, 'Table (2)'[date replied], SECOND )
    )
VAR _Seconds = result
VAR _Minutes =
    INT ( _Seconds / 60 )
VAR _RemainingSeconds =
    MOD ( _Minutes, 60 )
VAR _Hours =
    INT ( _Minutes / 60 )
VAR _RemainingMinutes =
    MOD ( _Minutes, 60 )
VAR _Days =
    INT ( _Hours / 24 )
VAR _RemainingHours =
    MOD ( _Hours, 24 )
RETURN
    FORMAT ( _Days, "00" ) & ":"
        & FORMAT ( _RemainingHours, "00" ) & ":"
        & FORMAT ( _RemainingMinutes, "00" ) & ":"
        & FORMAT ( _RemainingSeconds, "00" )

Output:-

Samarth_18_0-1655183827416.png

Note:- Minutes and seconds values are vary since i have not added seconds data in the dataset.

 

Best Regards,
Samarth

If this post helps, please consider accepting it as the solution to help the other members find it more quickly.
Appreciate your Kudos!!
Connect on Linkedin

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

Jan25PBI_Carousel

Power BI Monthly Update - January 2025

Check out the January 2025 Power BI update to learn about new features in Reporting, Modeling, and Data Connectivity.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.