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

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply
junmin
Helper I
Helper I

Get Min and Max Date Based on Batch Number and Employee

Hi all, 

As the subject suggests, I am trying to get the minimum and maximum date based on batch number and employee. My dataset contains every transaction an employee made. So an example of my dataset looks like this:

DATE | Employee ID | Batch Number | Task 

10-Dec-2023 22:01:00 | 123456 | 000001 | Task A

10-Dec-2023 22:13:00 | 123456 | 000001 | Task B

10-Dec-2023 22:20:00 | 123456 | 000001 | Task C

10-Dec-2023 22:55:00 | 123456 | 000001 | Task D

11-Dec-2023 8:00:00 | 123456 | 000002 | Task A

11-Dec-2023 8:10:00 | 123456 | 000002 | Task B

11-Dec-2023 8:11:00 | 123456 | 000002 | Task C

11-Dec-2023 8:30:00 | 123456 | 000002 | Task D

 

One Employee can perform Tasks on several batches at different times

One Batch number has several Tasks, but only one Employee, which means, for example, Batch# 000002 can only be assigned to Employee ID# 123456

 

So, now im looking for the min and max date per Batch per Employee because i want to calculate the duration for an Employee to complete a Batch. 

 

Taking the above dataset as example, I would expect something like this:

DATE | Employee ID | Batch Number | Task | Min Date | Max Date | Duration

10-Dec-2023 22:01:00 | 123456 | 000001 | Task A | 10-Dec-2023 22:01:00 | 10-Dec-2023 22:55:00 | 54 min

10-Dec-2023 22:13:00 | 123456 | 000001 | Task B | 10-Dec-2023 22:01:00 | 10-Dec-2023 22:55:00 | 54 min

10-Dec-2023 22:20:00 | 123456 | 000001 | Task C | 10-Dec-2023 22:01:00 | 10-Dec-2023 22:55:00 | 54 min

10-Dec-2023 22:55:00 | 123456 | 000001 | Task D | 10-Dec-2023 22:01:00 | 10-Dec-2023 22:55:00 | 54 min

11-Dec-2023 8:00:00 | 123456 | 000002 | Task A | 11-Dec-2023 8:00:00 | 11-Dec-2023 8:30:00 | 30 min

11-Dec-2023 8:10:00 | 123456 | 000002 | Task B | 11-Dec-2023 8:00:00 | 11-Dec-2023 8:30:00 | 30 min

11-Dec-2023 8:11:00 | 123456 | 000002 | Task C | 11-Dec-2023 8:00:00 | 11-Dec-2023 8:30:00 | 30 min

11-Dec-2023 8:30:00 | 123456 | 000002 | Task D | 11-Dec-2023 8:00:00 | 11-Dec-2023 8:30:00 | 30 min

 

I have tried to create calculated column using this in the query:

ColumnEarliestDate =
CALCULATE(
    MIN('Table'[DATE]),
    ALLEXCEPT('Table',
    'Table'[Employee ID], 'Table'[Batch Number])
)
 
ColumnLatestDate =
CALCULATE(
    MAX('Table'[DATE]),
    ALLEXCEPT('Table',
    'Table'[Employee ID], 'Table'[Batch Number])
)
 
ColumnDuration = DATEDIFF('Table'[ColumnEarliestDate], 'Table'[ColumnLatestDate], MINUTE)
 
But it isn't giving me what I'm looking for. i don't even know what number it is giving me, doesn't make sense 😞 
 
I would truly appreciate any help i could get, thank you so much!!
1 ACCEPTED SOLUTION
amitchandak
Super User
Super User

@junmin , I think task needs to be part of that

 

 

ColumnEarliestDate =
CALCULATE(
MIN('Table'[DATE]),
ALLEXCEPT('Table',
'Table'[Employee ID], 'Table'[Batch Number], 'Table'[Task])
)

ColumnLatestDate =
CALCULATE(
MAX('Table'[DATE]),
ALLEXCEPT('Table',
'Table'[Employee ID], 'Table'[Batch Number], 'Table'[Task])
)

ColumnDuration = DATEDIFF('Table'[ColumnEarliestDate], 'Table'[ColumnLatestDate], MINUTE)

better you to have measure with required columns in table visual. Same can work as measure

You can also consider earlier for column

Power BI DAX- Earlier, I should have known Earlier: https://youtu.be/CVW6YwvHHi8


 

Or Try measures like

 

MinDateWithTask =
CALCULATE(
MIN('Table'[DATE]),
ALLSELECTED('Table'),
'Table'[Employee ID] = MAX('Table'[Employee ID]),
'Table'[Batch Number] = MAX('Table'[Batch Number]),
'Table'[Task] = MAX('Table'[Task])
)

 

MaxDateWithTask =
CALCULATE(
MAX('Table'[DATE]),
ALLSELECTED('Table'),
'Table'[Employee ID] = MAX('Table'[Employee ID]),
'Table'[Batch Number] = MAX('Table'[Batch Number]),
'Table'[Task] = MAX('Table'[Task])
)

 

DurationWithTask =
DATEDIFF(
[MinDateWithTask],
[MaxDateWithTask],
MINUTE
)

Join us as experts from around the world come together to shape the future of data and AI!
At the Microsoft Analytics Community Conference, global leaders and influential voices are stepping up to share their knowledge and help you master the latest in Microsoft Fabric, Copilot, and Purview.
️ November 12th-14th, 2024
 Online Event
Register Here

View solution in original post

2 REPLIES 2
amitchandak
Super User
Super User

@junmin , I think task needs to be part of that

 

 

ColumnEarliestDate =
CALCULATE(
MIN('Table'[DATE]),
ALLEXCEPT('Table',
'Table'[Employee ID], 'Table'[Batch Number], 'Table'[Task])
)

ColumnLatestDate =
CALCULATE(
MAX('Table'[DATE]),
ALLEXCEPT('Table',
'Table'[Employee ID], 'Table'[Batch Number], 'Table'[Task])
)

ColumnDuration = DATEDIFF('Table'[ColumnEarliestDate], 'Table'[ColumnLatestDate], MINUTE)

better you to have measure with required columns in table visual. Same can work as measure

You can also consider earlier for column

Power BI DAX- Earlier, I should have known Earlier: https://youtu.be/CVW6YwvHHi8


 

Or Try measures like

 

MinDateWithTask =
CALCULATE(
MIN('Table'[DATE]),
ALLSELECTED('Table'),
'Table'[Employee ID] = MAX('Table'[Employee ID]),
'Table'[Batch Number] = MAX('Table'[Batch Number]),
'Table'[Task] = MAX('Table'[Task])
)

 

MaxDateWithTask =
CALCULATE(
MAX('Table'[DATE]),
ALLSELECTED('Table'),
'Table'[Employee ID] = MAX('Table'[Employee ID]),
'Table'[Batch Number] = MAX('Table'[Batch Number]),
'Table'[Task] = MAX('Table'[Task])
)

 

DurationWithTask =
DATEDIFF(
[MinDateWithTask],
[MaxDateWithTask],
MINUTE
)

Join us as experts from around the world come together to shape the future of data and AI!
At the Microsoft Analytics Community Conference, global leaders and influential voices are stepping up to share their knowledge and help you master the latest in Microsoft Fabric, Copilot, and Purview.
️ November 12th-14th, 2024
 Online Event
Register Here

hi amitchandak, thank you for your reply. May I pls know why I must include tasks as well? So, if I were to have other columns (besides the one i showed here), do I have to include all of them as well?

 

thank you.

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

Dec Fabric Community Survey

We want your feedback!

Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.