Forum Discussion

FelipeScheihing's avatar
FelipeScheihing
Frequent Visitor
2 years ago
Solved

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?

 

  • Anonymous's avatar
    Anonymous
    2 years ago

    Thanks for the reply fromlbendlin ,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:

    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.

     

     

    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.

     

2 Replies

  • 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-Forum/ba-p/963216

    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/1447523

  • Anonymous's avatar
    Anonymous
    Not applicable

    Thanks for the reply fromlbendlin ,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:

    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.

     

     

    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.