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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
CamillaDiPassio
Frequent Visitor

DATEDIFF with conditions

Hello everyone, 

I have an issue with a dataset. I have this table type:

 

MaterialCodeCreationdateUserNameType
Krd_01722/07/2022 14:12ITDASCI1
Krd_01722/07/2022 12:07ITDASCI2
Krd_01703/08/2022 11:29ITSTFUR2
Krd_00529/07/2022 18:27ITDASCI1
Krd_00522/07/2022 18:20admin2
1000000579303/08/2022 12:00ITSTFUR2
Krd_02102/08/2022 10:56ITENCOL21
Krd_02102/08/2022 10:21ITENCOL22
Krd_02721/07/2022 12:50admin1
Krd_02103/08/2022 11:28ITSTFUR2
Krd_02721/07/2022 12:49admin2
Krd_09822/07/2022 19:02admin1
Krd_09802/08/2022 08:43ITREANZ1
Krd_09818/07/2022 10:03admin2
Krd_09802/08/2022 07:43ITREANZ2
Krd_65002/08/2022 11:50ITLUDEL21
Krd_65001/08/2022 15:22ITLUDEL22
Krd_64811/07/2022 10:15admin2
1000002660803/08/2022 14:57ITALBER1
1000002660803/08/2022 14:49ITALBER2
1000002913613/07/2022 11:00admin1
1000002913601/08/2022 15:30ITCLCIC11
1000002913613/07/2022 09:42admin2
Krd_34122/07/2022 17:51admin1
1000002913601/08/2022 14:28ITCLCIC12
Krd_34122/07/2022 17:49admin2
Krd_34222/07/2022 17:49admin2
Krd_58522/07/2022 17:48admin2
Krd_69703/08/2022 08:47ITSTFUR2
Krd_69603/08/2022 10:32ITSTFUR2
Krd_05925/07/2022 14:58ITCLCIC11
Krd_05925/07/2022 14:46ITCLCIC12

 

Where:

- MaterialCode is the code of the material I have in the warehouse;

- CreationDate is the only column of date I have;

- UserName is the code of the worker who pick or put the material inside the shelf;

- Type is a number by which the worker's action is represented. One if he put material and two if he pick it.

 

I need to calculate how many days the material is outside the shelf before it is put back inside. The difficulty I have is that all this information is contained in one table so I will have to enter more than one condition. 

I need to calculate the difference in days from when the material is picked (type 2) to when it is put (type 1) taking into account that this operation is unique to UserName. In fact, the same material can be withdrawn and repositioned several times, but the subtraction has to be done only for one operator at a time. I hope I have explained my problem well, would you guys be able to help me?

 

Thanks,

Camilla

1 ACCEPTED SOLUTION

Hi @CamillaDiPassio 
Please try this version with your data. I cannot test as the sample data that I have does not reflect this case.

2.png

View solution in original post

3 REPLIES 3
tamerj1
Super User
Super User

Hi @CamillaDiPassio 
Please refer to sample file attached with two solutions, one based on a calculated column and one is based on a measure

1.png

Days Outside Shelf Measure = 
VAR TotalMinutes =
    SUMX ( 
        Data,
        VAR CurrentType = Data[Type]
        VAR CurrentDate = Data[Creationdate]
        VAR CurrentTable = CALCULATETABLE ( Data, ALLEXCEPT ( Data, Data[MaterialCode], Data[UserName] ) )
        VAR PreviousTable = FILTER ( CurrentTable, Data[Creationdate] < CurrentDate )
        VAR Previous1Date = MAXX ( FILTER ( PreviousTable, Data[Creationdate] = 1 ), Data[Creationdate] )
        VAR Previous2Date = MINX ( FILTER ( PreviousTable, Data[Creationdate] > Previous1Date && Data[Type] = 2 ), Data[Creationdate] )
        VAR Minutes = DATEDIFF ( Previous2Date, CurrentDate, MINUTE )
        RETURN
            IF ( CurrentType = 1, Minutes )
    )
VAR Days = QUOTIENT ( TotalMinutes, 1440 )
VAR Hours = QUOTIENT ( MOD ( TotalMinutes, 1440 ), 60 )
VAR Minutes = MOD ( MOD ( TotalMinutes, 1440 ), 60 )
RETURN
    IF ( 
        TotalMinutes <> BLANK ( ),
        Days & " Days, " & Hours & "Hr & " & Minutes & "Min"
    )

Thank you for your reply!

But in this case I lose the detail of the material that is out and has not yet returned, so I should also add the TODAY() condition, how can I do that?

Also, I don't need the MINUTES but to round everything up to DAYS so if the material has been out 5 minutes for me it is the same as saying it has been out 1 day.

 

 

Hi @CamillaDiPassio 
Please try this version with your data. I cannot test as the sample data that I have does not reflect this case.

2.png

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

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

April Fabric Community Update

Fabric Community Update - April 2024

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