Expression in a calculated field
Asked by ?????:

Hello!

I need to calculate the percentage of related issues of a certain type and status to the total number of related issues of the same type. And then display the value in the calculated field.

To do this, I need to create an expression in a calculated field, but I don't understand how to do this at all.

The formula should look like this:

Number of related tasks with a certain issuetype and status / total number of related tasks with this particular issuetype \* 100

Help me please :)


Answers:
author: ?????
body: [@Dick](/t5/user/viewprofilepage/user-id/4586768)

Thanks, but for me it's more priority to configure this through an expression rather than through automation

comments: author: Dick
body: The expression would fill the custom field using automation.

It would kick-off your calculation and saving the result, keeping it up-to-date for you to enjoy...

more documentation on [math expressions available in automation](https://support.atlassian.com/cloud-automation/docs/jira-smart-values-math-expressions/)


author: Dick
body: It's the automation that would gather two arrays of issues that are described in your formula, then using the .size operator you can construct the equation and store the outcome in a similar manner to this:

edit issue

customfield

{{#=}} {{lookupissuesA.size.divide(lookupIsssuesB.size).multiply(100)}}


author: Bill Sheboy
body: What is the source of information for the expression you show? A rule may only have *one Lookup Issues result at a time*, so the above expression cannot work.

Instead a single lookup issue might be used with smart value, list filtering and a math expression, or repeated lookups could save intermediate results in created variables or with a lookup table. All of these depend upon which version of Jira (and automation rules) is used, and that information was not provided by the original poster.


author: Dick
body: Hi Bill,   

So it would be something similar to:

lookupissues = JQL query A

create variable SizeA = LookupIssues.size

Lookupissues = JQL query B

create variable SizeB = LookupIssues.size

followed by:

edit issue

customfield

{{#=}} {{SizeA.divide(SizeB).multiply(100)}}


author: Bill Sheboy
body: Yes, that is almost correct. The variables would need to be converted using [asNumber](https://support.atlassian.com/cloud-automation/docs/jira-smart-values-text-fields/#asNumber) before the inline math operation will work:

```
{{SizeA.asNumber.divide(SizeB.asNumber).multiply(100)}}
```

Although for this scenario, I instead recommend using [smart value, list filtering](https://community.atlassian.com/t5/Automation-articles/Filtering-smart-value-lists/ba-p/1827588) with a [math expression](https://support.atlassian.com/cloud-automation/docs/jira-smart-values-math-expressions/) as it only requires one read of the issues from the database. When the issues gathered or the JQL are very different, the multiple calls to Lookup Issues seems better.

For example, this would produce the percentage completed by story points:

```
{{#=}}ROUND( ( 0{{#lookupIssues}}{{#if(equals(status.statusCategory.name,"Done"))}}+{{Story points|0}}{{/}}{{/}} ) / {{lookupIssues.Story points.sum|0}} * 100, 0){{/}}
```

How that works, working from inside to outward...

* iterate over the lookup results
* using a conditional filter to check if the status is a "Done" one
* and if so, add the story points, with a default of 0 for issues with no value
* with a leading 0 and surrounding parentheses to sum the values
* that is then divided by the total story points for the issues in the lookup result
* which is multiplied by 100 and then rounded to create a whole number percentage
* finally wrapping everything in a math expression

Additional criteria could be added using the other [conditional logic functions](https://support.atlassian.com/cloud-automation/docs/jira-smart-values-conditional-logic/).


author: Dick
body: Hi Arina,

You can use an automation to do the math for you.

the lookup issues can be referenced using .size as is given in the [Atlassian documentation](https://support.atlassian.com/cloud-automation/docs/jira-smart-values-lists/)


Tags: calculated-field