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

The ultimate Microsoft Fabric, Power BI, Azure AI & SQL learning event! Join us in Las Vegas from March 26-28, 2024. Use code MSCUST for a $100 discount. Register Now

Reply
Dicko
Helper II
Helper II

DAX: calculate difference between date/times with conditions

Hi,

I have data of telephone calls with details of each call.

 

Dicko_0-1631010001044.png

 

Now I need to calculate the duration of each call if the reason is completed.

So for the call with call_id 123 the duration is 4 minutes.

 

I have no idea where to start ...
I really would like to learn how to do this.

1 ACCEPTED SOLUTION
Fowmy
Super User
Super User

@Dicko 

Add the following code as a new column to your table:

Difference = 

VAR __ID = Table2[call_id]
VAR __TIME = Table2[timestamp]
RETURN

IF(
    Table2[reason] = "completed",
    DATEDIFF(
        CALCULATE(
            min(Table2[timestamp]),
            Table2[call_id] = __ID,
            Table2[timestamp] < __TIME,
            REMOVEFILTERS(Table2)
        ),
        __TIME,
        MINUTE
    )
)

Fowmy_0-1631011934817.png

 



Did I answer your question? Mark my post as a solution! and hit thumbs up


Subscribe and learn Power BI from these videos

Website LinkedIn PBI User Group

View solution in original post

2 REPLIES 2
Fowmy
Super User
Super User

@Dicko 

Add the following code as a new column to your table:

Difference = 

VAR __ID = Table2[call_id]
VAR __TIME = Table2[timestamp]
RETURN

IF(
    Table2[reason] = "completed",
    DATEDIFF(
        CALCULATE(
            min(Table2[timestamp]),
            Table2[call_id] = __ID,
            Table2[timestamp] < __TIME,
            REMOVEFILTERS(Table2)
        ),
        __TIME,
        MINUTE
    )
)

Fowmy_0-1631011934817.png

 



Did I answer your question? Mark my post as a solution! and hit thumbs up


Subscribe and learn Power BI from these videos

Website LinkedIn PBI User Group

Thanks! I learned something new 🙂

Helpful resources

Announcements
Fabric Community Conference

Microsoft Fabric Community Conference

Join us at our first-ever Microsoft Fabric Community Conference, March 26-28, 2024 in Las Vegas with 100+ sessions by community experts and Microsoft engineering.

February 2024 Update Carousel

Power BI Monthly Update - February 2024

Check out the February 2024 Power BI update to learn about new features.

Fabric Career Hub

Microsoft Fabric Career Hub

Explore career paths and learn resources in Fabric.

Fabric Partner Community

Microsoft Fabric Partner Community

Engage with the Fabric engineering team, hear of product updates, business opportunities, and resources in the Fabric Partner Community.

Top Solution Authors