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

Microsoft is giving away 50,000 FREE Microsoft Certification exam vouchers. Get Fabric certified for FREE! Learn more

Reply
bottos
Frequent Visitor

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.

1 ACCEPTED SOLUTION
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"
)

vzhengdxumsft_0-1708401925686.pngvzhengdxumsft_1-1708401935781.png

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:

vzhengdxumsft_2-1708402050790.png

The measure:

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

vzhengdxumsft_3-1708402094708.png

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.

View solution in original post

4 REPLIES 4
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"
)

vzhengdxumsft_0-1708401925686.pngvzhengdxumsft_1-1708401935781.png

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:

vzhengdxumsft_2-1708402050790.png

The measure:

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

vzhengdxumsft_3-1708402094708.png

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.

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

danextian
Super User
Super User

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?





Dane Belarmino | Microsoft MVP | Proud to be a Super User!

Did I answer your question? Mark my post as a solution!


"Tell me and I’ll forget; show me and I may remember; involve me and I’ll understand."
Need Power BI consultation, get in touch with me on LinkedIn or hire me on UpWork.
Learn with me on YouTube @DAXJutsu or follow my page on Facebook @DAXJutsuPBI.

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.

Helpful resources

Announcements
PBIApril_Carousel

Power BI Monthly Update - April 2025

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

Notebook Gallery Carousel1

NEW! Community Notebooks Gallery

Explore and share Fabric Notebooks to boost Power BI insights in the new community notebooks gallery.

April2025 Carousel

Fabric Community Update - April 2025

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