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

Calling all Data Engineers! Fabric Data Engineer (Exam DP-700) live sessions are back! Starting October 16th. Sign up.

Reply
jingxiawang
Regular Visitor

Calculate End Time to use the Start Time of the event for the same unit

Hi,

 

I'm looking for a DAX to calculate below End Time.

 

Here is the example:

Unit                              Start Time                               End Time

Unit 1                           1/1/2022 7:00:00am

Unit 1                           1/1/2022 7:05:00am

Unit 2                            1/1/2022 7:00:00am

Unit 2                            1/1/2022 7:10:00am

 

  • Start Time: The table data has the start time
  • End Time: Needs to be calculated, I need to build the logic to look at the same unit, have the End equals to the Start of the next event for the same unit.

If there’s no other event for the same unit, use the last Power BI dataset refresh time stamp as the End.

If the two events for the same unit spans different months, use the midnight time stamp 00:00 on the last day of the month as the End for the 1st event of the two events.

 

Can anyone help me with the DAX to achieve this?

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi @jingxiawang ,

 

Please try this calculated column

End Time =
VAR _next =
    MAXX (
        FILTER (
            'Table',
            [Unit] = EARLIER ( 'Table'[Unit] )
                && [Start Time] > EARLIER ( 'Table'[Start Time] )
        ),
        [Start Time]
    )
VAR _last =
    MAXX (
        FILTER (
            'Table',
            [Unit] = EARLIER ( 'Table'[Unit] )
                && [Start Time] < EARLIER ( 'Table'[Start Time] )
        ),
        [Start Time]
    )
RETURN
    IF (
        ISBLANK ( _next ),
        IF (
            DATEDIFF ( _last, [Start Time], MONTH ) > 0,
            EOMONTH ( [Start Time], 0 ),
            NOW ()
        ),
        _next
    )

Here's the results:

vstephenmsft_0-1648716927892.png

 

 

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
jingxiawang
Regular Visitor

Thanks, Amit. It worked perfectly to handle the 1st part of the calculation. But it still does not handle below logic:

  •  If there’s no other event for the same unit, use the last Power BI dataset refresh time stamp as the End.
  • If the two events for the same unit spans different months, use the midnight time stamp 00:00 on the last day of the month as the End for the 1st event of the two events.

Is there anyway we can include these two logic in the DAX?

amitchandak
Super User
Super User

@jingxiawang , a new column

 

minx(filter(Table, [Unit] = EARLIER([Unit]) && [Start Time] > earlier([[Start Time]) ),[Start Time])

 

or

 

 

coalesce(minx(filter(Table, [Unit] = EARLIER([Unit]) && [Start Time] > earlier([[Start Time]) ),[Start Time]), [Start Time])

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

Thanks, Amit. It worked perfectly to handle the 1st part of the calculation. But it still does not handle below logic:

  •  If there’s no other event for the same unit, use the last Power BI dataset refresh time stamp as the End.
  • If the two events for the same unit spans different months, use the midnight time stamp 00:00 on the last day of the month as the End for the 1st event of the two events.

Is there anyway we can include these two logic in the DAX?

Anonymous
Not applicable

Hi @jingxiawang ,

 

Please try this calculated column

End Time =
VAR _next =
    MAXX (
        FILTER (
            'Table',
            [Unit] = EARLIER ( 'Table'[Unit] )
                && [Start Time] > EARLIER ( 'Table'[Start Time] )
        ),
        [Start Time]
    )
VAR _last =
    MAXX (
        FILTER (
            'Table',
            [Unit] = EARLIER ( 'Table'[Unit] )
                && [Start Time] < EARLIER ( 'Table'[Start Time] )
        ),
        [Start Time]
    )
RETURN
    IF (
        ISBLANK ( _next ),
        IF (
            DATEDIFF ( _last, [Start Time], MONTH ) > 0,
            EOMONTH ( [Start Time], 0 ),
            NOW ()
        ),
        _next
    )

Here's the results:

vstephenmsft_0-1648716927892.png

 

 

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.

Helpful resources

Announcements
FabCon Global Hackathon Carousel

FabCon Global Hackathon

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes!

September Power BI Update Carousel

Power BI Monthly Update - September 2025

Check out the September 2025 Power BI update to learn about new features.

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Solution Authors
Top Kudoed Authors