OData Query to Only Return State Changes from Azure Devops - azure-devops

I have an OData Query that I am using to pull data into PowerBI that I am trying to make more efficient. I am doing a report from Azure DevOps and pulling data in from the WorkItemRevisions resource. Currently, I am pulling all the data for a Work Item and then filtering in PowerBI to only get when the State has changed. I would like to move this filtering to the Odata query so that I can minimize the data that I am pulling into the report.
Currently, I have a query like the following (simplified example used for this question)
https://analytics.dev.azure.com/{Organization}/{Project}/_odata/v3.0-preview/WorkItemRevisions?
$select=Revision,WorkItemId,WorkItemType,Title,State,ChangedDate,LeadTimeDays,ParentWorkItemId
How can this be updated so that only Revisions where the State has changed (from New to Active, Active to Done, etc) are returned?

How can this be updated so that only Revisions where the State has changed (from New to Active, Active to Done, etc) are returned?
I am afraid that OData Query could not perfectly achieve what we need.
There is a feature Revisions/any(r:r/state eq '{state}') to filter the work item has a set state in the past.
For example:
https://analytics.dev.azure.com/<Organization>/<Project>/_odata/v2.0//WorkItems?
$filter=State eq 'Closed' and Revisions/any(r:r/State eq 'Active')
This query is similar to a Work Item query that uses the Was Ever operator.
As I said, this may not be a perfect solution. That because it can only filter whether the work item has ever had a specified states, but cannot accurately determine the states of the work item must be from New to Active, Active to Done. If we change the state of the workitem from Active to Resolved, then change it from Resolved to Closed. Then this work item will appear in the query results.
In addition, even if you use the UI query, we cannot accurately query the result of the work item status changing from A to B. To achieve this goal, we need to use REST API.
So, we could use the feature Revisions/any(r:r/state eq '{state}') to reduce the data pulled into the report to a certain extent.

You can use below query to achieve what you want.
/_odata/v4.0-preview/WorkItemRevisions?&$apply=filter(((WorkItem/WorkItemType
eq 'Bug' or WorkItem/WorkItemType eq 'Product Backlog Item') and
((WorkItem/ChangedDate ge 2022-10-05Z and WorkItem/ChangedDate le
2022-11-04Z) or (WorkItem/CreatedDate ge 2022-10-05Z and
WorkItem/CreatedDate le 2022-11-04Z))))/groupby((WorkItemId,State),
aggregate(ChangedDate with min as MinChangedDate))
To filter the group by data you need to encapsulate it under $apply as it is shown above.
Above URL will return all states and their changed dates for Bug and PBI Work Item types which are added or updated with a given date range.
Hope it helps!

if you are able to use Analytics Views instead of OData, there is a dedicated field available in Analytics Views Fields setting called "State Changed Date"

Related

Devops work item get top one revision data order by descending

I am trying to get azure devops work its history/revisions using Updates-List or Updates-Get.
How can I get latest revision of a work item? I tried with odata query to order by rev and take top 1 but the result is always top 1 with out applying filter.
My Query: myrepo/_apis/wit/workItems/30/updates?$orderby=rev desc&$top=1&api-version=6.0
Similar stackoverflow reference is here
It looks like $orderby is not supported in these type of REST API calls. As a workaround you could use the reverse and first functions after you retrieved the list of updates/revisions.
Below is an example for a single work item.
In a compose use the following expression to retrieve the last updates record.
first(reverse(body('Send_an_HTTP_request_to_Azure_DevOps')['value']))

Is it possible to query when a specific field changed in Azure DevOps?

I have a DateTime field (MyDTField) in my Azure DevOps instance (I am an admin on it, so I can change its schema if necessary). I would like to write a query that returns any items where the above field (MyDTField) changed in the last 7 days. Is this possible to do?
For e.g. the built in "Changed Date" field allows us to write a similar query, but that returns work times where any field value changed. I want to get items where a specific field changed within a specified time period.
Thanks!
You can try to set up a query like as below to see if it can work as expected.
To view more details, you can see:
Query history and discussion fields
Operators and macros supported for each data type
[UPDATE]
As a workaround, you can try like as below on each of the work item types that have the MyDTField field:
Add another custom Date/Time field, for example "MyDTField Latest change Date" field.
Add a work item rule like this. With this rule, once the MyDTField field changed, the value of the MyDTField Latest change Date field will be automatically changed to the current date/time.
Set up the query like as this. With this query, you will get the list of work items as your expectation.

In Azure DevOps portal how to query list of tasks whose Completed effort is modified today

In Azure DevOps portal how to query list of work items whose "Completed effort" is modified today. Actually I'm trying to query list of work items my team members have worked today and email them to my manager.
I'm able to query list of work items "modified" by users today, but I do not want to retrieve a work item if it's completed effort is not modified and some other filed is modified.
I am afraid it cannot be done directly. There is not such a filter of completed effort change.
However, there is a workaround to achieve this. Check below:
1, Add a custom field of Date type(see below Modified Date field). Check here for detailed steps.
2, Create a rule: Check here for detailed steps:
Condtions: When change was made to Completed work field.
Action: Set the customized Modified Date field to the Changed Date
3,Then you can add Modified Date filter in your Query. See below query, only the work items whose completed effort filed is modified today will be returned:
Another workaround is to use work item tracking Rest api. This is a little complicated:
1, First call Wiql rest api to get all the work items of your current query.
2, Second loop through the returned work items and call Updates rest api, Then filter those work items whose completed effort field has new value.

How do I set up a query in Azure DevOps to tell me which items I have transferred out of my area path today?

Right now I have the following query:
screenshot of query in DevOps
It's returning items I've transferred today, as I intended, but it's also returning items that were changed today by other teams (e.g., I transferred something out on Friday, but the other team changed it today, so it shows up).
Since there is no "Transfer Date", only "Changed Date" or "State Change Date", is there a better way to set this up?
How do I set up a query in Azure DevOps to tell me which items I have transferred out of my area path today?
That because you have set the Operator to Was Ever, which will return the work items that you have ever changed.
So, if anyone change it today, it will show up in your query result.
To resolve this issue, you need to change the Operator to =:
Now, it will not return the workitem you transferred out on Friday.
Update:
the results do not stay in the query since "Changed By =" only shows
the LAST person to make a change. So for example, I transfer the item,
then someone from another team immediately comments. That work item no
longer shows up in the query, but I want to track it until the end of
the day, so I can know "x items total have been transferred out
today".
Indeed, this is a issue. It seems there is no such out of box way to resolve this issue at this moment.
As workaround, you could add custom field in our custom process, then add a Rules to set the value of the custom field to True when area path is modified.
Just like the similar thread.
Now, we could add custom field as new clause in the query, so that we could filter out the wokitems you want. However, the limitation of this method is that it can only work on newly created wokitems, and has no effect on those previously created workitems.
Hope this helps.

Query items user was mentioned in

Is there a way to query work items where a user was mentioned? I am able to receive 'hard-coded' results by querying for
"History"-"Contains word"-"\#Username",
but I want a generic version, which works for all users. (Opposed to writing one query for every user)
Use this predicate:
Field: "ID"
Operator: "In"
Value: "#RecentMentions"
This automatically filters for work items, where current user has been mentioned.
I found it in predefined filter "Mentioned" in "Work Items" section. If you click on "Open in Queries" button, you will get a query with above filter. (This section can even remove the need for that query...)
Note: at present time, works only in VSTS.
https://{org}.visualstudio.com/{project}/_workitems/mentioned/
This would achieve the same result.
There is no way to achieve this by work item query directly just as starain mentioned. You can create a custom hub or custom widget by using VSTS Extension to show these information in web portal.
You can’t achieve that through work item query directly, you could build a app to retrieve data through REST API (https://www.visualstudio.com/en-us/docs/integrate/api/wit/wiql), change query text according different conditions (e.g. users)
Your query should be something like this
Select Id,Title From WorkItems Where ID IN (#RecentMentions) order by [System.ChangedDate] desc
here is the reference for rest of the macro's available in ADO rest API.
https://learn.microsoft.com/en-us/azure/devops/boards/queries/query-operators-variables?view=azure-devops