Forum Discussion

bottos's avatar
bottos
Frequent Visitor
2 years ago
Solved

Compare dates within same department, same ID, different status

I have searched for a similar issue, but I am struggling to find a code that I can adapt. So any help would be greatly appreciated.

 

I have the following table. This is just an extract, because there are multiple slot numbers within the same department, and multiple departments with their own slot numbers.

 

What is required is to identify  when "Pre" and "In" overlap for more than 30 days, and count how many times it will happen. I need to do this comparison within the same department and within the same slot number.

 

The data looks like this:

 

DepartmentSlot numberEmployee IDStatusStart DateEnd Date
Sales732231111-APre3/12/20248/26/2025
Sales732231111-BPre3/12/20248/26/2025
Sales732231112-AIn1/31/20237/16/2024
Sales732231112-BIn1/31/20237/16/2024
Sales732231113-AIn8/22/20232/7/2025
Sales732231113-BIn8/22/20232/7/2025
Sales732231114-AIn12/12/20235/27/2025
Sales732231114-BIn12/12/20235/27/2025
Sales732231115-APre3/12/20248/28/2025
Sales732231115-BPre3/12/20248/28/2025
Sales732231116-APre7/16/20241/1/2026
Sales732231116-BPre7/16/20241/1/2026

 

The comparison does not need to happen between "In"s, but only with "Pre"s with "In"s. Also "Pre" means people that will join (start and end date are alway future) and "In" have joined already (start date is always in the past, end date future).

 

Thank you.

  • Anonymous's avatar
    Anonymous
    2 years ago

    Hi bottos 

     

    Please try this:

    First of all, I create 2 table with dax:

    Table 2 =
    CALCULATETABLE (
        SELECTCOLUMNS (
            'Table',
            "Department1", 'Table'[Department],
            "Employee1", 'Table'[Employee ID],
            "Pre-StartDate", 'Table'[Start Date]
        ),
        'Table'[Status] = "Pre"
    )
    Table 3 =
    CALCULATETABLE (
        SELECTCOLUMNS (
            'Table',
            "Department2", 'Table'[Department],
            "Employee2", 'Table'[Employee ID],
            "In-endDate", 'Table'[End Date]
        ),
        'Table'[Status] = "In"
    )

    Then create a new table:

    Table 4 = CROSSJOIN('Table 2','Table 3')

    Then add a calculate column:

    diff =
    IF (
        'Table 4'[Department2] = 'Table 4'[Department1],
        DATEDIFF ( 'Table 4'[In-endDate], 'Table 4'[Pre-StartDate], DAY )
    )
    

    The result is as follow:

    The measure:

    count =
    CALCULATE (
        COUNTROWS ( 'Table 4' ),
        FILTER ( ALLSELECTED ( 'Table 4' ), 'Table 4'[diff] > 30 )
    )
    

    The result of the sample data is zero.

     

    Best Regards

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

4 Replies

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi bottos 

     

    Please try this:

    First of all, I create 2 table with dax:

    Table 2 =
    CALCULATETABLE (
        SELECTCOLUMNS (
            'Table',
            "Department1", 'Table'[Department],
            "Employee1", 'Table'[Employee ID],
            "Pre-StartDate", 'Table'[Start Date]
        ),
        'Table'[Status] = "Pre"
    )
    Table 3 =
    CALCULATETABLE (
        SELECTCOLUMNS (
            'Table',
            "Department2", 'Table'[Department],
            "Employee2", 'Table'[Employee ID],
            "In-endDate", 'Table'[End Date]
        ),
        'Table'[Status] = "In"
    )

    Then create a new table:

    Table 4 = CROSSJOIN('Table 2','Table 3')

    Then add a calculate column:

    diff =
    IF (
        'Table 4'[Department2] = 'Table 4'[Department1],
        DATEDIFF ( 'Table 4'[In-endDate], 'Table 4'[Pre-StartDate], DAY )
    )
    

    The result is as follow:

    The measure:

    count =
    CALCULATE (
        COUNTROWS ( 'Table 4' ),
        FILTER ( ALLSELECTED ( 'Table 4' ), 'Table 4'[diff] > 30 )
    )
    

    The result of the sample data is zero.

     

    Best Regards

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

    • bottos's avatar
      bottos
      Frequent Visitor

      Thank you for making it in a way I can understand and learm from it.

  • Hi bottos 

    Given you sample data, what is your expected result?  Which row is to be compared against which row? Count how many times it will happen based on what date - today, selected date?

    • bottos's avatar
      bottos
      Frequent Visitor

      Hi! I have to compare each row that has the status "Per" with every row that has the status "In"; for each comparison, I need to also compare if the Start Date field of the "Pre" row is 30 days more than the End Date of the "In" row. If it is, I need to flag it, and count it in the end.