Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Grow your Fabric skills and prepare for the DP-600 certification exam by completing the latest Microsoft Fabric challenge.

Reply
smithub
Helper I
Helper I

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 128/06/2023 2
12345 Stage 129/06/2023 
12345 Stage 230/06/2023 48
12345 Stage 207/07/2023 
12345 Stage 221/07/2023 
12345 Stage 228/07/2023 
12345 Stage 317/08/2023 9
12345 Stage 318/08/2023 
12345 Stage 426/08/2023 20
12345 Stage 427/08/2023 
12345 Stage 410/09/2023 
12345 Stage 515/09/2023 30
12345 Stage 528/09/2023 
12345 End Stage15/10/2023 

 

Thanks all.

 

1 ACCEPTED SOLUTION
v-tangjie-msft
Community Support
Community Support

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.

vtangjiemsft_0-1698388642714.png

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. 

View solution in original post

8 REPLIES 8
v-tangjie-msft
Community Support
Community Support

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.

vtangjiemsft_0-1698388642714.png

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. 

brokencornets
Helper III
Helper III

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:

brokencornets_0-1698313259421.png

 

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.

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?

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?

Table 1 contains Case ID, Status Date and a Stage column that contains over 40 different stages so I created another table - Table 2 with 9 high level stages and mapped the 9 stages in Table 2 to the 40 stages in Table 1.

In my visualisation I want to only show the 9 high level stages coming from Table 2.

Hope this makes sense

Okay yes, that does make sense. 

 

My advice would be to merge Table 2 to Table 1 on the low-level stage field to bring in the high-level stage and then pivot the data as above. (Maybe best to use Merge Queries as New to create a brand new table so you don't lose the original table structures)

brokencornets
Helper III
Helper III

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

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?

 

 

Helpful resources

Announcements
Europe Fabric Conference

Europe’s largest Microsoft Fabric Community Conference

Join the community in Stockholm for expert Microsoft Fabric learning including a very exciting keynote from Arun Ulag, Corporate Vice President, Azure Data.

RTI Forums Carousel3

New forum boards available in Real-Time Intelligence.

Ask questions in Eventhouse and KQL, Eventstream, and Reflex.