Forum Discussion
How long at each stage
Hi all,
Trying to create a measure that calculates the amount of days at each stage.
The bottom table has sample data from 2 tables.
| Table | Column |
| Table 1 | Case ID |
| Table 2 | Stage |
| Table 1 | Status Date |
I would like to add the Days column to the table below in Power BI.
The number of days should be calculated like this:
Stage 1 days = First date in Stage 2 - First date in Stage 1
Stage 2 days = First date in Stage 3 - First date in Stage 2
Stage 3 days = First date in Stage 4 - First date in Stage 3
Stage 4 days = First date in Stage 5 - First date in Stage 4
| Case ID | Stage | Status Date | Days |
| 12345 | Stage 1 | 28/06/2023 | 2 |
| 12345 | Stage 1 | 29/06/2023 | |
| 12345 | Stage 2 | 30/06/2023 | 48 |
| 12345 | Stage 2 | 07/07/2023 | |
| 12345 | Stage 2 | 21/07/2023 | |
| 12345 | Stage 2 | 28/07/2023 | |
| 12345 | Stage 3 | 17/08/2023 | 9 |
| 12345 | Stage 3 | 18/08/2023 | |
| 12345 | Stage 4 | 26/08/2023 | 20 |
| 12345 | Stage 4 | 27/08/2023 | |
| 12345 | Stage 4 | 10/09/2023 | |
| 12345 | Stage 5 | 15/09/2023 | 30 |
| 12345 | Stage 5 | 28/09/2023 | |
| 12345 | End Stage | 15/10/2023 |
Thanks all.
- Anonymous2 years ago
Hi smithub ,
According to your description, here are my steps you can follow as a solution.
(1) My test data is the same as yours.
(2) We can create a table.
Table = FILTER('Table1','Table1'[Stage] in VALUES('Table2'[Stage]))(3)We can create a column.
Days = var _min=CALCULATE(MIN('Table'[Status Date]),FILTER('Table','Table'[Stage]=EARLIER('Table'[Stage]))) var _max=CALCULATE(MIN('Table'[Status Date]),FILTER('Table','Table'[Stage]>EARLIER('Table'[Stage]))) var _max2=CALCULATE(MIN('Table'[Status Date]),FILTER('Table','Table'[Stage]=" End Stage")) return SWITCH(TRUE(), 'Table'[Status Date]=_min && [Stage] <> " Stage 5" && [Stage]<>" End Stage",DATEDIFF(_min,_max,DAY), 'Table'[Status Date]=_min && [Stage] = " Stage 5" ,DATEDIFF(_min,_max2,DAY))(4) Then the result is as follows.
Best Regards,
Neeko Tang
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
8 Replies
- brokencornets
Helper IV
You could pivot the Stage column by first removing duplicates from Case ID & Stage combined:
let
Source = <YOUR SOURCE HERE>,
#"Removed Duplicates" = Table.Distinct(Source, {"Case ID", " Stage"}),
#"Changed Type" = Table.TransformColumnTypes(#"Removed Duplicates",{{" Status Date", type date}}),
#"Pivoted Column" = Table.Pivot(#"Changed Type", List.Distinct(#"Changed Type"[#" Stage"]), " Stage", " Status Date", List.Sum)
in
#"Pivoted Column"Then you could either create further Custom Columns like
Days In Stage 1 = [#" Stage 2"]-[#" Stage 1"]
or you could just create measures to calculate these values
- smithub
Helper I
Hi brokencornets,
Thanks for your reply, unfortunately I don't understand any of this and I believe there could be an easier solution by creating a measure with DAX?
- brokencornets
Helper IV
Hi smithub - apologies, I was probably a bit too vague there.
The above snippet is from the 'Advanced Editor' screen in Power Query and is an easy way to show all the steps I completed:
All I've done to get the view above is:
STEP 1: Select both Case ID and Stage columns > right-click > remove duplicates
STEP 2: Select Stage column > Transform > Pivot column (Values column = Status Date)
From there you can add Custom Columns in Power Query, or create Calculated Columns or Measures with DAX to calculate time spent in each stage, all of which will be fairly simple as you're just subtracting one column from another.
There may well be a DAX measure you can write without this but it's going to be a lot more complex and beyond my level. But personally I think it's better to shape the data correctly with one row per Case ID.
- smithub
Helper I
Hi brokencornets,
I see now what you did, however the Case ID and Status Date come from one table (Table 1) and Stage is on another table (Table 2). I don't think this would work then?
- brokencornets
Helper IV
I don't understand how you can know which Case ID is in which Stage at any point in time if you don't have a table with Case ID, Stage and Date in?
Can you give a sample of the data in table 1 and the data in table 2?
- AnonymousNot applicable
Hi smithub ,
According to your description, here are my steps you can follow as a solution.
(1) My test data is the same as yours.
(2) We can create a table.
Table = FILTER('Table1','Table1'[Stage] in VALUES('Table2'[Stage]))(3)We can create a column.
Days = var _min=CALCULATE(MIN('Table'[Status Date]),FILTER('Table','Table'[Stage]=EARLIER('Table'[Stage]))) var _max=CALCULATE(MIN('Table'[Status Date]),FILTER('Table','Table'[Stage]>EARLIER('Table'[Stage]))) var _max2=CALCULATE(MIN('Table'[Status Date]),FILTER('Table','Table'[Stage]=" End Stage")) return SWITCH(TRUE(), 'Table'[Status Date]=_min && [Stage] <> " Stage 5" && [Stage]<>" End Stage",DATEDIFF(_min,_max,DAY), 'Table'[Status Date]=_min && [Stage] = " Stage 5" ,DATEDIFF(_min,_max2,DAY))(4) Then the result is as follows.
Best Regards,
Neeko Tang
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.