cancel
Showing results for
Did you mean:
Frequent Visitor

## Calculate the no of days for a Epic which was spent in each state

Hi,

I would like to know how to calcuate the days which was spent in each state. Please find the find the sample data below. Please help on how to create a index field in power Query and caculated column in the front end

 Work Item Id Date State 1143 6/17/21 Build 1143 6/18/21 Build 1143 6/19/21 Build 1143 6/20/21 Build 1143 6/21/21 Build 1143 6/22/21 Build 1143 6/23/21 UAT 1143 6/24/21 UAT 1143 6/25/21 UAT 1143 6/26/21 UAT 1143 6/27/21 UAT 1143 6/28/21 UAT 1143 6/29/21 UAT 1143 6/30/21 Closed 1143 7/1/21 Closed 1143 7/2/21 Closed

@Baskar I have see your response to the other thread for the same kind of question. Can you please help on how to create a index for this one in the power Query and create a new calcuated column in the front end.

Power Query for Index =
CALCULATE(DISTINCTCOUNT('Epic - All History Daily'[Date]),
FILTER('Epic - All History Daily',
'Epic - All History Daily'[State]= EARLIER('Epic - All History Daily'[State]) &&
'Epic - All History Daily'[Date] <=EARLIER( 'Epic - All History Daily'[Date])))

Front end Calculated Column

Diff =
VAR vKEY = 'Epic - All History Daily'[Work Item Id]
VAR vINDEX = 'Epic - All History Daily'[Index]

RETURN

DATEDIFF(
CALCULATE( VALUES('Epic - All History Daily'[Date]),
FILTER( ALL('Epic - All History Daily'),
'Epic - All History Daily'[Work Item Id] = vKEY &&
'Epic - All History Daily'[Index] = (vINDEX - 1)
)
),'Epic - All History Daily'[Date],DAY)

I have got the error in front end. Attached the screenshot for reference

1 ACCEPTED SOLUTION
Community Support

Your Power Query for Index is a DAX formula for a calculated column like below image, not in Power Query.

At present you have the error you highlighted because VALUES function get multiple dates that have the same work item id and index while DATEDIFF expects a single date value there.

If the Index column already returns the correct result, you can try below code for Diff column.

``````Diff =
VAR vKEY = 'Epic - All History Daily'[Work Item Id]
VAR vINDEX = 'Epic - All History Daily'[Index]
VAR vState = 'Epic - All History Daily'[State]
RETURN
DATEDIFF(
CALCULATE( VALUES('Epic - All History Daily'[Date]),
FILTER( ALL('Epic - All History Daily'),
'Epic - All History Daily'[Work Item Id] = vKEY &&
'Epic - All History Daily'[Index] = (vINDEX - 1) &&
'Epic - All History Daily'[State] = vState
)
),'Epic - All History Daily'[Date],DAY)``````

If this doesn't give you correct result, can you provide the expected output in Diff column?

Best Regards,
Community Support Team _ Jing
If this post helps, please Accept it as Solution to help other members find it.

3 REPLIES 3
Community Support

Your Power Query for Index is a DAX formula for a calculated column like below image, not in Power Query.

At present you have the error you highlighted because VALUES function get multiple dates that have the same work item id and index while DATEDIFF expects a single date value there.

If the Index column already returns the correct result, you can try below code for Diff column.

``````Diff =
VAR vKEY = 'Epic - All History Daily'[Work Item Id]
VAR vINDEX = 'Epic - All History Daily'[Index]
VAR vState = 'Epic - All History Daily'[State]
RETURN
DATEDIFF(
CALCULATE( VALUES('Epic - All History Daily'[Date]),
FILTER( ALL('Epic - All History Daily'),
'Epic - All History Daily'[Work Item Id] = vKEY &&
'Epic - All History Daily'[Index] = (vINDEX - 1) &&
'Epic - All History Daily'[State] = vState
)
),'Epic - All History Daily'[Date],DAY)``````

If this doesn't give you correct result, can you provide the expected output in Diff column?

Best Regards,
Community Support Team _ Jing
If this post helps, please Accept it as Solution to help other members find it.

Frequent Visitor

Hi @v-jingzhang ,

I have got requirement, where I dont want to calculate the no. of days for "Closed" State. Can you please help me on this. Thank you somuch for all your help.

Frequent Visitor

Hello @v-jingzhang Thank you so so much. It worked 😊 Sorry about the confusion, Yes it is a DAX query for Index. I'm new to power BI, so got confused with terminology.

Announcements

#### The Power BI Community Show

Watch the playback when Priya Sathy and Charles Webb discuss Datamarts! Kelly also shares Power BI Community updates.

#### Ted's Dev Camp - July 28, 2022

Watch Session 24 of Ted's Dev Camp along with past sessions!

The first Microsoft-sponsored Power Platform Conference is coming in September. 100+ speakers, 150+ sessions, and what's new and next for Power Platform.

Top Solution Authors
Top Kudoed Authors