Find everything you need to get certified on Fabric—skills challenges, live sessions, exam prep, role guidance, and more.
Get startedGrow your Fabric skills and prepare for the DP-600 certification exam by completing the latest Microsoft Fabric challenge.
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.
Solved! Go to Solution.
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.
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.
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.
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)
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?
Join the community in Stockholm for expert Microsoft Fabric learning including a very exciting keynote from Arun Ulag, Corporate Vice President, Azure Data.
User | Count |
---|---|
91 | |
89 | |
79 | |
69 | |
68 |
User | Count |
---|---|
222 | |
128 | |
117 | |
82 | |
77 |