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
Anonymous
Not applicable

Need help regarding one of the custom column created to calculate time!

Hello PowerBI Community,

 

I have been working on one of the projects where I am supposed to calculate time difference for each entry of log data to find out how long a demand stayed in one status.

 

Below is my formula which was working fine until I got a record where two records are created at exactly the same time(could be due to system error) and the result should reflect 00:00:00 but it is not the case.

 

Below is my formula to calculate time:

 

Duration(HH:MM:SS) = if(ISBLANK(CALCULATE(MIN(Data[Created]),FILTER(Data,Data[Created]>EARLIER(Data[Created])&&Data[DemandId]=EARLIER(Data[DemandId])))),NOW()-[Created],CALCULATE(MIN(Data[Created]),FILTER(Data,Data[Created]>EARLIER(Data[Created])&&Data[DemandId]=EARLIER(Data[DemandId])))-[Created])

 

Please find snapshot of the dataset for your reference.

 

Capture.JPG

 

As you can see the second last record shows duration as 00 hours 38 minutes and 21 seconds when it should be 00:00:00 as the status for demand was changed right away to "Being Processed at Local Office".

Is there something wrong with my formula? Will really appreciate if someone can help me on this!

The link to the csv file is below

https://www.dropbox.com/s/vjx7nby0d9gzujv/data.csv?dl=0

Will really appreciate some help on this!

1 ACCEPTED SOLUTION
v-yulgu-msft
Employee
Employee

Hi @Anonymous,

 

Do you want the highlighted value to be 00:00:00?

1.PNG

 If so, you could add an index column in Query Editor mode first. Then, please modify the formula as below:

Duration2(HH:MMSmiley FrustratedS) =
VAR nextStatus =
    CALCULATE (
        FIRSTNONBLANK ( data[DemandSubStatus], 1 ),
        FILTER (
            Data,
            data[Index]
                = EARLIER ( data[Index] ) + 1
                && data[DemandId] = EARLIER ( Data[DemandId] )
        )
    )
VAR nextCreatedTime =
    CALCULATE (
        MIN ( Data[Created] ),
        FILTER (
            Data,
            Data[Created] > EARLIER ( Data[Created] )
                && Data[DemandId] = EARLIER ( Data[DemandId] )
        )
    )
RETURN
    IF (
        nextStatus = "Being Processed at Local Office",
        IF (
            ISBLANK ( nextCreatedTime ),
            NOW () - [Created],
            nextCreatedTime - [Created]
        ),
        TIME ( 0, 0, 0 )
    )

Best regards,

Yuliana Gu

Community Support Team _ Yuliana Gu
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
v-yulgu-msft
Employee
Employee

Hi @Anonymous,

 

Do you want the highlighted value to be 00:00:00?

1.PNG

 If so, you could add an index column in Query Editor mode first. Then, please modify the formula as below:

Duration2(HH:MMSmiley FrustratedS) =
VAR nextStatus =
    CALCULATE (
        FIRSTNONBLANK ( data[DemandSubStatus], 1 ),
        FILTER (
            Data,
            data[Index]
                = EARLIER ( data[Index] ) + 1
                && data[DemandId] = EARLIER ( Data[DemandId] )
        )
    )
VAR nextCreatedTime =
    CALCULATE (
        MIN ( Data[Created] ),
        FILTER (
            Data,
            Data[Created] > EARLIER ( Data[Created] )
                && Data[DemandId] = EARLIER ( Data[DemandId] )
        )
    )
RETURN
    IF (
        nextStatus = "Being Processed at Local Office",
        IF (
            ISBLANK ( nextCreatedTime ),
            NOW () - [Created],
            nextCreatedTime - [Created]
        ),
        TIME ( 0, 0, 0 )
    )

Best regards,

Yuliana Gu

Community Support Team _ Yuliana Gu
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Greg_Deckler
Super User
Super User

Seems to me that you need to add an Index to your table in Query Editor and then change your formula to be >= EARLIER(Data[Created]) but exclude your current index <>EARLIER(Data[Index])


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...
Anonymous
Not applicable

HI @Greg_Deckler

 

I did add index column but I am not sure how to modify the formula correctly. Could you please tell me the formula so I will test it out.

 

Thanks!

I believe it should be something like this.

 

Duration(HH:MM:SS) = 
VAR __previous = 
CALCULATE(
	MIN(Data[Created]),
	FILTER(
		Data,
		Data[Created]>=EARLIER(Data[Created])
		&&
		Data[DemandId]=EARLIER(Data[DemandId])
		&&
		Data[Index]<>EARLIER(Data[Index])
	)
)
RETURN
IF(ISBLANK(__previous),NOW()-[Created],__previous-[Created])

Note, I totally restructured your code because you need to format that stuff and you had a bunch of duplicate code and all that. This assumes that your column is [Index]. As you can see, it is really just your code with a modification that the Created date is >= than the current line but with the exclusion that the Index is <> to the current line, otherwise it would always just match itself.


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

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.

Top Solution Authors