cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Kumshan450 Regular Visitor
Regular Visitor

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

7 REPLIES 7
Omega Established Member
Established Member

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

Community Support Team
Community Support Team

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

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

Kumshan450 Regular Visitor
Regular Visitor

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

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))
Kumshan450 Regular Visitor
Regular Visitor

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

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
 
Kumshan450 Regular Visitor
Regular Visitor

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

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

 

 

Capture.JPGRankx function to sort the data in ascending order

 

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

 

Timespan.JPGTime 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.

 

Community Support Team
Community Support Team

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

@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

Kumshan450 Regular Visitor
Regular Visitor

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

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. 

 

Helpful resources

Announcements
Back to School Contest

Back to School Contest

Engage and empower students with Power BI!

MBAS Gallery

Watch Sessions On Demand!

Continue your learning in our online communities.

Summit Australia 2019

Summit Australia 2019

Travel to Melbourne and network with thousands of peers!

PBI Community Highlights

PBI Community Highlights

Check out what's new in the Power BI Community!

Top Ideas
Users Online
Currently online: 6 members 962 guests
Please welcome our newest community members: