# Using a Level Aware Calculation with an Aggregate in a Calculated Field

**URL:** <https://community.amazonquick.com/t/using-a-level-aware-calculation-with-an-aggregate-in-a-calculated-field/17328>\
**Category:** Q&A\
**Tags:** level-aware, calculations, quick-sight\
**Created:** [August 23, 2023, 2:02pm UTC](https://community.amazonquick.com/t/using-a-level-aware-calculation-with-an-aggregate-in-a-calculated-field/17328 "2023-08-23T14:02:25Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![baramichelle](https://avatars.discourse-cdn.com/v4/letter/b/c57346/32.png) [@baramichelle](https://community.amazonquick.com/u/baramichelle)\
**Post date:** [August 23, 2023, 2:02pm UTC](https://community.amazonquick.com/t/using-a-level-aware-calculation-with-an-aggregate-in-a-calculated-field/17328/1 "2023-08-23T14:02:25Z")

</div>

I am trying to create a calculated field for a visual that will show a ratio of defective units per total total units produced broken down by the defect type. I would like the denominator to show the total volume produced over all and **not** by the defect\_type. I would like the numerator to calculate defective units broken down by the defect\_type.

Here is my current calculated field:  
sum({defective\_units }, [{defect\_type}]) / sum({total\_units},[] )  
I have also tried:  
sum({defective\_units }, [${visualDimensions}]) / sum({total\_units},[] )

Quick Sight allows me to save the calculated field, but once it is added to the visual I get the same error message for both: “The Level Aware Calculation (LAC) aggregate functions inside one visual aggregate functions should always share the same grouping key”

I understand the error message, so I am looking for other ways to get the same result. Are there any other ways to achieve this type of calculation?

Thank you.

For additional context, I got the numerator and denominator working as columns. I feel like it is so close to working!

 ![image](https://d1wl4jav5dp96l.cloudfront.net/original/2X/a/a95b3225ab1cea2e909647d3ec79c30bd4bb6b4d.png)

---

<div class="post-metadata">

**Author:** ![Max](https://d2orw1595917va.cloudfront.net/user_avatar/community.amazonquick.com/max/32/2288_2.png) [@Max](https://community.amazonquick.com/u/Max)\
**Post date:** [August 23, 2023, 10:28pm UTC](https://community.amazonquick.com/t/using-a-level-aware-calculation-with-an-aggregate-in-a-calculated-field/17328/2 "2023-08-23T22:28:23Z")

</div>

> [@baramichelle](#):
>
> sum({defective\_units }, [${visualDimensions}]) / sum({total\_units}, )

Instead of sum’s can you try sumOver?

```auto
sumOver({defective_units }, [${visualDimensions}]) / sumOver({total_units},[] )

```

---

<div class="post-metadata">

**Author:** ![Max](https://d2orw1595917va.cloudfront.net/user_avatar/community.amazonquick.com/max/32/2288_2.png) [@Max](https://community.amazonquick.com/u/Max)\
**Post date:** [August 28, 2023, 8:15pm UTC](https://community.amazonquick.com/t/using-a-level-aware-calculation-with-an-aggregate-in-a-calculated-field/17328/3 "2023-08-28T20:15:59Z")

</div>

@baramichelle

We have not heard back from you regarding your question. We would still like to help. If we do not hear back in the next 3 days, we will archive the question

---

<div class="post-metadata">

**Author:** ![baramichelle](https://avatars.discourse-cdn.com/v4/letter/b/c57346/32.png) [@baramichelle](https://community.amazonquick.com/u/baramichelle)\
**Post date:** [August 29, 2023, 12:52pm UTC](https://community.amazonquick.com/t/using-a-level-aware-calculation-with-an-aggregate-in-a-calculated-field/17328/4 "2023-08-29T12:52:38Z")

</div>

Changing the calculation to sumOver did not work. I am still getting the same error message.

---

<div class="post-metadata">

**Author:** ![Max](https://d2orw1595917va.cloudfront.net/user_avatar/community.amazonquick.com/max/32/2288_2.png) [@Max](https://community.amazonquick.com/u/Max)\
**Post date:** [August 29, 2023, 1:01pm UTC](https://community.amazonquick.com/t/using-a-level-aware-calculation-with-an-aggregate-in-a-calculated-field/17328/5 "2023-08-29T13:01:54Z")

</div>

```auto
sumOver({defective_units }, [{defect_type}]) / sumOver({total_units},[] )

```

This gives you the exact same error?

---

<div class="post-metadata">

**Author:** ![baramichelle](https://avatars.discourse-cdn.com/v4/letter/b/c57346/32.png) [@baramichelle](https://community.amazonquick.com/u/baramichelle)\
**Post date:** [August 29, 2023, 2:10pm UTC](https://community.amazonquick.com/t/using-a-level-aware-calculation-with-an-aggregate-in-a-calculated-field/17328/6 "2023-08-29T14:10:15Z")

</div>

Sorry not the same exact error. In this case the error message appears and prevents me from saving the calculated field with the new formula.

This is the exact text, “Field “defective\_units” must be aggregated for table calculations.”

---

<div class="post-metadata">

**Author:** ![David\_Wong](https://d2orw1595917va.cloudfront.net/user_avatar/community.amazonquick.com/david_wong/32/15630_2.png) [@David\_Wong](https://community.amazonquick.com/u/David_Wong)\
**Post date:** [August 31, 2023, 4:44am UTC](https://community.amazonquick.com/t/using-a-level-aware-calculation-with-an-aggregate-in-a-calculated-field/17328/7 "2023-08-31T04:44:06Z")

</div>

Can you try one of these? I think either one should work but with the first one the percentages will always add up to 100% even if you apply filters.

sum({defective\_units}) / sum({total\_units}, [])  
sum({defective\_units}) / min(sumOver({total\_units}, [], PRE\_FILTER))

---

<div class="post-metadata">

**Author:** ![WLS-D](https://d2orw1595917va.cloudfront.net/user_avatar/community.amazonquick.com/wls-d/32/36109_2.png) [@WLS-D](https://community.amazonquick.com/u/WLS-D)\
**Post date:** [September 8, 2023, 7:21pm UTC](https://community.amazonquick.com/t/using-a-level-aware-calculation-with-an-aggregate-in-a-calculated-field/17328/8 "2023-09-08T19:21:53Z")

</div>

Hello @baramichelle ! Did David’s response answer your question?

We would still like to help answer your question, but we haven’t heard from you in a few days. If we do not hear from you in 3 days this topic will be archived.

---

<div class="post-metadata">

**Author:** ![WLS-D](https://d2orw1595917va.cloudfront.net/user_avatar/community.amazonquick.com/wls-d/32/36109_2.png) [@WLS-D](https://community.amazonquick.com/u/WLS-D)\
**Post date:** [September 12, 2023, 2:03pm UTC](https://community.amazonquick.com/t/using-a-level-aware-calculation-with-an-aggregate-in-a-calculated-field/17328/9 "2023-09-12T14:03:44Z")

</div>

Hello @baramichelle !

This question is being archived, if you still need help please reach out in the community.

---

<div class="post-metadata">

**Author:** ![WLS-D](https://d2orw1595917va.cloudfront.net/user_avatar/community.amazonquick.com/wls-d/32/36109_2.png) [@WLS-D](https://community.amazonquick.com/u/WLS-D)\
**Post date:** [September 12, 2023, 2:03pm UTC](https://community.amazonquick.com/t/using-a-level-aware-calculation-with-an-aggregate-in-a-calculated-field/17328/10 "2023-09-12T14:03:47Z")

</div>

Archived on September 12, 2023, 2:03pm UTC


