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

Calculation duration over rows with condition

Hello,

 

I have been looking around on the forum for a solution on my query but I only found parts.

Below a part of my data set. I want to calculate the duration of a phase, under the condition of k_fin_out_header_id.

For the first key the status was changed from 1 to 2 on 27/02/2018 15:36 (= end date) (A)

Next it was changed from status 2 to 3 on 28/02/2018 16:19 (B) So duration of phase 2 was B -/- A

So in chronological order the duration needs to be calculated 

For the start date of phase 1 the value is presented in a separate column X

 

 

 

PowerBI capture.JPG

 

 

 

 

 

 

Example data

 

k_fin_out_header_idvalue_oldvalue_newcreated_date
001D5B3A-619D-4695-A95C-99393C2B46ED1210-4-2018 13:05
001D5B3A-619D-4695-A95C-99393C2B46ED2310-4-2018 13:21
001D5B3A-619D-4695-A95C-99393C2B46ED3410-4-2018 15:37
001D5B3A-619D-4695-A95C-99393C2B46ED4513-4-2018 16:16
001D5B3A-619D-4695-A95C-99393C2B46ED5616-4-2018 11:57
003AC412-1AC4-414C-BB0D-437240D8BB2F21014-3-2018 10:12
003AC412-1AC4-414C-BB0D-437240D8BB2F101119-3-2018 09:58
003AC412-1AC4-414C-BB0D-437240D8BB2F1214-3-2018 10:04
00721B41-9E38-40DD-8A3A-94EA164C04F121114-2-2018 09:00
00721B41-9E38-40DD-8A3A-94EA164C04F11213-2-2018 16:57
008196BB-E375-4235-ACEF-522797828E1E1231-1-2018 11:17
008196BB-E375-4235-ACEF-522797828E1E431-2-2018 19:30
008196BB-E375-4235-ACEF-522797828E1E2431-1-2018 13:20
009B3688-120B-4601-8777-14BA71F9E36E3119-7-2018 11:49
009B3688-120B-4601-8777-14BA71F9E36E129-7-2018 10:19
009B3688-120B-4601-8777-14BA71F9E36E239-7-2018 10:38


  

I know that indexing may be a solution but I don't know how to make the index start over when a new k_fin_out_header shows up.

 

 

Thanks,

 

Leon

1 ACCEPTED SOLUTION

Hi @Anonymous,

 

That case, my above solution would not work. And your solution with an extra Index column is much better.

 

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

6 REPLIES 6
v-yulgu-msft
Employee
Employee

Hi @Anonymous,

 

Please new a calculated column:

duration =
DATEDIFF (
    LOOKUPVALUE (
        'Example Data'[created_date],
        'Example Data'[k_fin_out_header_id], 'Example Data'[k_fin_out_header_id],
        'Example Data'[value_new], 'Example Data'[value_old]
    ),
    'Example Data'[created_date],
    MINUTE
)

1.PNG

 

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

Hi, 

 

Thanks for the solution.

However I am not sure if this will work when a status hits multiple times.

So for example

 

1-4  10:00

4-3  10:15

3-4  10:20

4-5  10:45

 

 

Hi @Anonymous,

 

That case, my above solution would not work. And your solution with an extra Index column is much better.

 

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

I found a solution:

 

1: Sorted the table on fin_out_header_id

2.Sorted the table on creation date

3. Included an index column

4. added a lookupformula: lookupvalue(GenLog[End_date];GenLog[k_fin_out_header_id];GenLog[k_fin_out_header_id];GenLog[Index];GenLog[Index]-1) 

and the previous date is presented in a new colomn.

 

 

Greg_Deckler
Super User
Super User

Index is probably advisable. You need something like my MTBF calculation...See my article on Mean Time Before Failure (MTBF) which uses EARLIER: http://community.powerbi.com/t5/Community-Blog/Mean-Time-Between-Failure-MTBF-and-Power-BI/ba-p/3395...

 

Example data in text form is preferrable so that it can be copied and pasted. Please see this post regarding How to Get Your Question Answered Quickly: https://community.powerbi.com/t5/Community-Blog/How-to-Get-Your-Question-Answered-Quickly/ba-p/38490


@ 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

Thanks for the suggestion however it is more challeging as I don't have a start date available of each phase. Only the end date is available.

I have added a sample of the data.

 

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.