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

Time Difference between Two Rows or Two Columns

Hi all,

I need your help to create a column with returns the # of Months acculumated based on when an employee starts/returns from work. 

Here's a sample table, where the last column "Months Accumulated" is the resulting column which I want to create:

 

Company BranchEmployeeEntry DateContract Start DateMonths Accumulated
ABob2019-12-012017-10-0117
ABob2018-08-022017-10-0110
AJack2019-12-012018-05-0119
BHannah2019-12-012017-01-0112
BHannah2018-12-012017-01-0124

 

ased on this, there are two conditions that would affect how the column "Months Accumulated" can be calculated:


- If Entry Date = Contract Start Date -> Entry Date - Contract Start Date
- If there's an existing entry record taking place before the considered row/entry date -> Considered Entry Date - Previous closest Entry Date

 

How best should I do this? Please help! 

Thank you!

 

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

Hi @nerokasai ,

 

1. Insert an index column in power query.

2. Create the calculated column as below.

Column = 
VAR ind = 'Table'[Index] + 1
VAR currecntemp = 'Table'[Employee]
VAR next =
    CALCULATE (
        MAX ( 'Table'[Employee] ),
        FILTER (
            'Table',
            'Table'[Index] = ind
                && 'Table'[Company Branch] = EARLIER ( 'Table'[Company Branch] )
        )
    )
VAR nextentrydate =
    CALCULATE (
        MAX ( 'Table'[Entry Date] ),
        FILTER (
            'Table',
            'Table'[Index] = ind
                && 'Table'[Company Branch] = EARLIER ( 'Table'[Company Branch] )
        )
    )
RETURN
    IF (
        currecntemp = next,
        DATEDIFF ( nextentrydate, 'Table'[Entry Date], MONTH ) + 1,
        DATEDIFF ( 'Table'[Contract Start Date], 'Table'[Entry Date], MONTH )
    )

Capture.PNG

For more details, please check the pbix as attached.

 

 

Community Support Team _ Frank
If this post helps, then please consider Accept it as the solution to help the others find it more quickly.

View solution in original post

3 REPLIES 3
v-frfei-msft
Community Support
Community Support

Hi @nerokasai ,

 

1. Insert an index column in power query.

2. Create the calculated column as below.

Column = 
VAR ind = 'Table'[Index] + 1
VAR currecntemp = 'Table'[Employee]
VAR next =
    CALCULATE (
        MAX ( 'Table'[Employee] ),
        FILTER (
            'Table',
            'Table'[Index] = ind
                && 'Table'[Company Branch] = EARLIER ( 'Table'[Company Branch] )
        )
    )
VAR nextentrydate =
    CALCULATE (
        MAX ( 'Table'[Entry Date] ),
        FILTER (
            'Table',
            'Table'[Index] = ind
                && 'Table'[Company Branch] = EARLIER ( 'Table'[Company Branch] )
        )
    )
RETURN
    IF (
        currecntemp = next,
        DATEDIFF ( nextentrydate, 'Table'[Entry Date], MONTH ) + 1,
        DATEDIFF ( 'Table'[Contract Start Date], 'Table'[Entry Date], MONTH )
    )

Capture.PNG

For more details, please check the pbix as attached.

 

 

Community Support Team _ Frank
If this post helps, then please consider Accept it as the solution to help the others find it more quickly.
FrankAT
Community Champion
Community Champion

Hi @nerokasai 

create the calculated column like this:

DateDiff = DATEDIFF('Table'[Contract Start Date];'Table'[Entry Date];MONTH)
 
Regards FrankAT
Greg_Deckler
Community Champion
Community Champion

See my article on Mean Time Between Failure (MTBF) which uses EARLIER: http://community.powerbi.com/t5/Community-Blog/Mean-Time-Between-Failure-MTBF-and-Power-BI/ba-p/3395...



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...

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!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

Check out the October 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.