cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Kumshan450
Helper III
Helper III

Dax measure to calculate time difference between two data/time values

Require your help in finding out time difference between two dates in the same column.

 

I want to know how much is the time gap between the receipts cut by a person (Objective: It is to find an anomaly between receipts cut by a person – less than one minute).

 

I have attached the sample data.

 

In excel, I used to follow below steps to find the time gap between the receipts cut by a person. I want to replicate the same using DAX measures. Is it possible to do the same without any additional columns in power pivot data model containing million rows of data (100 MB).

 

  • First, do multiple sorting by Column collected by and then by column Collection DTM.

 Screee.jpg

 

  • Second create new column (TimeSpan) and apply new formula. 
    • collection(dtm)- previous(Collection(dtm))
  • Third apply below formula in another column:
    • if(Collectedby<>previous(Collectedby),0, if(TImeSpan<60 minutes),1,0)

I wanted to replicate the same steps in power pivot. I know the step of doing it in power query (indexing and doing vlookup) but dont want to follow that method as my data size is 32 lakhs of data. It will block the memory.

 

So interested in doing it in DAX measures without creating any additonal columns in power pivot data model.

 

Please find attatched the sample data for your perusal 

 

https://drive.google.com/folderview?id=1gZit23aVRwC5aVMTp7ohiBeSx14rwBFZ

8 REPLIES 8
Anonymous
Not applicable

Also can use a measure, i feel little faster loading time with this 

I use this for time tracking in call center, 


TimeDiff = CONVERT(SELECTEDVALUE(Table[Date1],0)-SELECTEDVALUE(Table[Date2],0),DATETIME)

 
and then change the format to time hh:mm:ss. You can create additional measure based on this 

Cheers
v-yuta-msft
Community Support
Community Support

Hi Kumshan450,

 

"

  • Second create new column (TimeSpan) and apply new formula. 
    • collection(dtm)- previous(Collection(dtm))
  • Third apply below formula in another column:
    • if(Collectedby<>previous(Collectedby),0, if(TImeSpan<60 minutes),1,0)

"

 

<--- Are collection and previous a function or a table you've created? What's "Collectedby"? In addtion, could you share your pbix file via onedrive for further analysis?

 

Regards,

Jimmy Tao

Sorry i am accepting solution on subtracting current row from previous row using dax measures technique not calculated column. 

 

I am able to crack the solution by creating calculated column. However already mentioned data size is huge. Hence interest in using dax measures. 

 

I am able to create calculated column. below is the code. Please replicate the above code using dax measure not calculated column. 

 

TS<1min = if(CALCULATE(max(Collectiondata[COLLECTED BY]),filter(Collectiondata,Collectiondata[Index]=earlier(Collectiondata[Index])-1))<>Collectiondata[COLLECTED BY],0,if(DATEDIFF(CALCULATE(max(Collectiondata[COLLECTION DTM]),filter(Collectiondata,Collectiondata[Index]=EARLIER(Collectiondata[Index])-1)),Collectiondata[COLLECTION DTM],second)<60,1,0))
 
Sample data file link
 

Additional information, I have used rankx function to sort the data..

 

 

Rankx function to sort the data in ascending orderRankx function to sort the data in ascending order

 

Then created a Time Span less than one minute column using below code. 

 

Time Span less than one minute calculated columnTime Span less than one minute calculated column

This additional columns increases the file size. So is there any dax measures which can do do both sorting and subract current row from previous row. I heard that it is better to use dax measures than calculated column when you have one million rows of data.

 

@Kumshan450,

 

DAX row context statement like earlier could not be used directly in calculate column, however, you may modify your calculate column to change to measure with formula below:

 

Measure = 
IF (
    CALCULATE (
        MAX ( Collectiondata[COLLECTED BY] ),
        FILTER (
            ALL ( Collectiondata ),
            COUNTROWS (
                FILTER (
                    Collectiondata,
                    Collectiondata[Index]
                        = EARLIER ( Collectiondata[Index] ) - 1
                )
            )
        )
    )
        <> MAX ( Collectiondata[COLLECTED BY] ),
    0,
    IF (
        DATEDIFF (
            CALCULATE (
                MAX ( Collectiondata[COLLECTION DTM] ),
                FILTER (
                    ALL ( Collectiondata ),
                    COUNTROWS (
                        FILTER (
                            Collectiondata,
                            Collectiondata[Index]
                                = EARLIER ( Collectiondata[Index] ) - 1
                        )
                    )
                )
            ),
            MAX ( Collectiondata[COLLECTION DTM] ),
            SECOND
        )
            < 60,
        1,
        0
    )
)

Regards,

Jimmy Tao

Hi Thank you very much for providing the code.

 

I have put your code (measure name: TS) and compared the result with the calculated column result (TS<1min). Its not matching. 

 

Please find here the screenshot. 

Sample picture.JPG

 

I have send the file data link. You can refer my previous post. 

 

Omega
Impactful Individual
Impactful Individual

Sorry i am accepting solution on subtracting current row from previous row using dax measures technique not calculated column. 

 

I am able to crack the solution by creating calculated column. However already mentioned data size is huge. Hence interest in using dax measures. 

 

I am able to create calculated column. below is the code. Please replicate the above code using dax measure not calculated column. 

 

TS<1min = if(CALCULATE(max(Collectiondata[COLLECTED BY]),filter(Collectiondata,Collectiondata[Index]=earlier(Collectiondata[Index])-1))<>Collectiondata[COLLECTED BY],0,if(DATEDIFF(CALCULATE(max(Collectiondata[COLLECTION DTM]),filter(Collectiondata,Collectiondata[Index]=EARLIER(Collectiondata[Index])-1)),Collectiondata[COLLECTION DTM],second)<60,1,0))

Helpful resources

Announcements
PBI User Groups

Welcome to the User Group Public Preview

Check out new user group experience and if you are a leader please create your group!

MBAS Attendee Badge

Claim Your Badge & Digital Swag!

Check out how to claim yours today!

secondImage

Are You Ready?

Test your skills now with the Cloud Skills Challenge.

Top Solution Authors