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

Power BI is turning 10! Let’s celebrate together with dataviz contests, interactive sessions, and giveaways. Register now.

Reply
FelipeScheihing
Frequent Visitor

calculation between non consecutives rows

Hello everyone,

I am trying to calculate the total hours and months between service gaps for each employee. I am creating a list of employees who qualify for a benefit. I thought identifying the last gap to reset the count of months and hours was enough, but I realized an employee qualifies if they meet the rules even if they have a gap in service later.

Could you guys help me out?

FelipeScheihing_0-1720219913839.png

 

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Thanks for the reply from@lbendlin ,please allow me to provide another insight:

HI,@FelipeScheihing 

Regarding the issue you raised, my solution is as follows:

1.First I have created the following table and the column names and data are the data you have given:

vlinyulumsft_0-1720426044614.png

2.Create calculated column references:

predate =
VAR currentdate = 'kk'[date]
RETURN
    IF (
        'kk'[gap] = "gap",
        MAXX (
            FILTER (
                'kk',
                'kk'[gap] = "gap"
                    && 'kk'[date] < currentdate
                    && 'kk'[id] = EARLIER ( kk[id] )
            ),
            'kk'[date]
        ),
        BLANK ()
    )
diffhour = DATEDIFF('kk'[predate],'kk'[date].[Date],HOUR)
diffmonth = DATEDIFF('kk'[predate],'kk'[date].[Date],MONTH)

 

3.Here's my final result, which I hope meets your requirements.

 

 

vlinyulumsft_1-1720426110444.png

4.Here is the relevant documentation:

DATEDIFF function (DAX) - DAX | Microsoft Learn

 

Can you share sample data and sample output in tabular format if I am misunderstanding? Or a sample pbix after removing sensitive data. We can better understand the problem and help you.

 

Please find the attached pbix relevant to the case.

 

Best Regards,

Leroy Lu

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

2 REPLIES 2
Anonymous
Not applicable

Thanks for the reply from@lbendlin ,please allow me to provide another insight:

HI,@FelipeScheihing 

Regarding the issue you raised, my solution is as follows:

1.First I have created the following table and the column names and data are the data you have given:

vlinyulumsft_0-1720426044614.png

2.Create calculated column references:

predate =
VAR currentdate = 'kk'[date]
RETURN
    IF (
        'kk'[gap] = "gap",
        MAXX (
            FILTER (
                'kk',
                'kk'[gap] = "gap"
                    && 'kk'[date] < currentdate
                    && 'kk'[id] = EARLIER ( kk[id] )
            ),
            'kk'[date]
        ),
        BLANK ()
    )
diffhour = DATEDIFF('kk'[predate],'kk'[date].[Date],HOUR)
diffmonth = DATEDIFF('kk'[predate],'kk'[date].[Date],MONTH)

 

3.Here's my final result, which I hope meets your requirements.

 

 

vlinyulumsft_1-1720426110444.png

4.Here is the relevant documentation:

DATEDIFF function (DAX) - DAX | Microsoft Learn

 

Can you share sample data and sample output in tabular format if I am misunderstanding? Or a sample pbix after removing sensitive data. We can better understand the problem and help you.

 

Please find the attached pbix relevant to the case.

 

Best Regards,

Leroy Lu

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

 

lbendlin
Super User
Super User

Please provide sample data that covers your issue or question completely, in a usable format (not as a screenshot).

Do not include sensitive information or anything not related to the issue or question.

If you are unsure how to upload data please refer to https://community.fabric.microsoft.com/t5/Community-Blog/How-to-provide-sample-data-in-the-Power-BI-...

Please show the expected outcome based on the sample data you provided.

Want faster answers? https://community.fabric.microsoft.com/t5/Desktop/How-to-Get-Your-Question-Answered-Quickly/m-p/1447...

Helpful resources

Announcements
June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

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

June 2025 community update carousel

Fabric Community Update - June 2025

Find out what's new and trending in the Fabric community.