# Sorting pivot table calculation field

**URL:** <https://community.amazonquick.com/t/sorting-pivot-table-calculation-field/32855>\
**Category:** Q&A\
**Tags:** dashboard-embed, developer, quick-sight\
**Created:** [July 21, 2024, 12:36pm UTC](https://community.amazonquick.com/t/sorting-pivot-table-calculation-field/32855 "2024-07-21T12:36:05Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Amit\_Raj](https://avatars.discourse-cdn.com/v4/letter/a/cc9497/32.png) [@Amit\_Raj](https://community.amazonquick.com/u/Amit_Raj)\
**Post date:** [July 21, 2024, 12:36pm UTC](https://community.amazonquick.com/t/sorting-pivot-table-calculation-field/32855/1 "2024-07-21T12:36:05Z")

</div>

Hi Team,

I am using `Percentage difference` calculation using [table calculation in a pivot table](https://docs.aws.amazon.com/quicksight/latest/user/add-a-calculation.html).

I would like to sort the table with `Percentage difference` i.e. “Delta” in below table-

 ![Screenshot 2024-07-21 at 8.30.50 PM](https://d1wl4jav5dp96l.cloudfront.net/original/3X/9/5/9534be7f45f4b32c12733b0ca8a2af2b65108bb1.png)

How can we do that?

Thank you  
Amit

---

<div class="post-metadata">

**Author:** ![jose\_burrull](https://d2orw1595917va.cloudfront.net/user_avatar/community.amazonquick.com/jose_burrull/32/16401_2.png) [@jose\_burrull](https://community.amazonquick.com/u/jose_burrull)\
**Post date:** [July 21, 2024, 3:52pm UTC](https://community.amazonquick.com/t/sorting-pivot-table-calculation-field/32855/2 "2024-07-21T15:52:48Z")

</div>

@Amit_Raj Hello … I can solve creating new calculated fields …

 ![image](https://d1wl4jav5dp96l.cloudfront.net/original/3X/4/5/4532023e1e36264eb4cb335ebeed92cecdbd1c26.png)

![image](https://d1wl4jav5dp96l.cloudfront.net/original/3X/7/c/7c3e8483df56b44fd738dfc6a6b6a5b0150dffc9.png)

![image](https://d1wl4jav5dp96l.cloudfront.net/original/3X/2/d/2d4c00799ec71d9752c763182875747e569ca341.png)

![image](https://d1wl4jav5dp96l.cloudfront.net/original/3X/f/e/fe9e2080d05e7aed5ce6d73b55b30bb841a4437e.png)

![image](https://d1wl4jav5dp96l.cloudfront.net/original/3X/b/1/b1c97ab4404647b4be827c378d76c8569e0370a1.png)

… and finally ordering by last column.

Let me know if that resolves the issue!

Regards, José Burrull

---

<div class="post-metadata">

**Author:** ![Amit\_Raj](https://avatars.discourse-cdn.com/v4/letter/a/cc9497/32.png) [@Amit\_Raj](https://community.amazonquick.com/u/Amit_Raj)\
**Post date:** [July 24, 2024, 8:54am UTC](https://community.amazonquick.com/t/sorting-pivot-table-calculation-field/32855/3 "2024-07-24T08:54:09Z")

</div>

Hi @jose_burrull  
Thank you for replying. I attempted it, but unfortunately doesn’t work for me.

I tried as below - but this results in no data, if I remove `parseDate` I get the error.

```auto
 sumIf({line_item_unblended_cost}, {line_item_usage_date}=parseDate("2024-07-06 - 2024-07-12"))

```

even a simple calculation does not work-

```auto
 sumIf({line_item_unblended_cost}, {line_item_usage_date}=parseDate("2024-07-06"))

```

I would like to add here that I create the custom date range from the line\_item\_usage\_date column using below custom field.

cal\_current\_week-

```auto
concat(
      formatDate(truncDate('DD', {cal_current_week_start}), 'yyyy-MM-dd'), 
      ' - ',
      formatDate(truncDate('DD', ${SelectEndDate}), 'yyyy-MM-dd')
  )

```

where cal\_current\_week\_start is

```auto
addDateTime(-6, 'DD', ${SelectEndDate})

```

and SelectEndDate is the user input from a parameter.

and calculating cost like this certainly does not work-

```auto
 sumIf({line_item_unblended_cost}, {line_item_usage_date}={cal_current_week})

```

```auto
 sumIf({line_item_unblended_cost}, {line_item_usage_date}=parseDate({cal_current_week}))

```

The dates in the column line\_item\_usage\_date are in below format-  
2024-07-06T00:00:00.000Z  
2024-07-07T00:00:00.000Z  
2024-07-08T00:00:00.000Z

Thank you

---

<div class="post-metadata">

**Author:** ![jose\_burrull](https://d2orw1595917va.cloudfront.net/user_avatar/community.amazonquick.com/jose_burrull/32/16401_2.png) [@jose\_burrull](https://community.amazonquick.com/u/jose_burrull)\
**Post date:** [July 24, 2024, 1:31pm UTC](https://community.amazonquick.com/t/sorting-pivot-table-calculation-field/32855/4 "2024-07-24T13:31:11Z")

</div>

HI @Amit_Raj ;

¿Did you try to transform the format of date to only month/day/year, even creating some calculated fields and the use of concatenate, format and parse functions to create a new date field in correct format i.e. in date format?. Please read my last post, maybe it will be useful for your issue. [Difference between Dates which are in String Format](https://community.amazonquick.com/t/difference-between-dates-which-are-in-string-format/18981)

---

<div class="post-metadata">

**Author:** ![Amit\_Raj](https://avatars.discourse-cdn.com/v4/letter/a/cc9497/32.png) [@Amit\_Raj](https://community.amazonquick.com/u/Amit_Raj)\
**Post date:** [July 29, 2024, 3:38am UTC](https://community.amazonquick.com/t/sorting-pivot-table-calculation-field/32855/5 "2024-07-29T03:38:07Z")

</div>

Hi @jose_burrull  
Thank you, this helped! % diff calculated field is working now.  
in your example screenshot, How do you keep the date range as column in the pivot-table?

I am able to achieve the Percentage\_diff sorting but I lost the date range, when I add date range as column, my table view gets changed.

without date range-

 ![Screenshot 2024-07-29 at 11.29.42 AM](https://d1wl4jav5dp96l.cloudfront.net/original/3X/2/8/28a6f0bcc4afb49dcfbdb6797d5171281cc97685.png)

when I add date range-

 ![Screenshot 2024-07-29 at 11.34.57 AM](https://d1wl4jav5dp96l.cloudfront.net/original/3X/c/7/c7ebc7b83b41d9d397d6060b2acc8fd1a5a9a5f5.png)

How you manage to keep the date range at the same time printing the calculations? I see you don’t add date\_range in the columns… how do you show date\_range without adding it to the column?

Thank you  
Amit

---

<div class="post-metadata">

**Author:** ![Xclipse](https://d2orw1595917va.cloudfront.net/user_avatar/community.amazonquick.com/xclipse/32/20455_2.png) [@Xclipse](https://community.amazonquick.com/u/Xclipse)\
**Post date:** [July 30, 2024, 3:20am UTC](https://community.amazonquick.com/t/sorting-pivot-table-calculation-field/32855/6 "2024-07-30T03:20:04Z")

</div>

Hi @Amit_Raj, please see if these links help:

> **[Sorting pivot tables in Amazon QuickSight - Amazon QuickSight](https://docs.aws.amazon.com/quicksight/latest/user/sorting-pivot-tables.html)**
>
> Sort pivot table visuals in Amazon QuickSight.

> [@Hidden fields in pivot table should flatten the table](https://community.amazonquick.com/t/hidden-fields-in-pivot-table-should-flatten-the-table/11572):
>
> Continuing the discussion from [Amazon Quicksight now supports show/hide fields on pivot table](https://community.amazonquick.com/t/amazon-quicksight-now-supports-show-hide-fields-on-pivot-table/4239): When a pivot table field (aka column) is hidden, it would make a lot of sense to also flatten the hierarchy, i.e. sorting should no longer happen inside the hidden groups.

![Screenshot 2023-03-29 at 19.16.44 Authorizations KPIs](https://d1wl4jav5dp96l.cloudfront.net/original/2X/2/2267f328643276e66d20666aefedf29bb8cba497.gif)

Did this solution work for you? I am marking this reply as, “Solution,” but let us know if this is not resolved. Thanks for posting your questions on the Quick Sight Community!

---

<div class="post-metadata">

**Author:** ![Amit\_Raj](https://avatars.discourse-cdn.com/v4/letter/a/cc9497/32.png) [@Amit\_Raj](https://community.amazonquick.com/u/Amit_Raj)\
**Post date:** [July 31, 2024, 3:40am UTC](https://community.amazonquick.com/t/sorting-pivot-table-calculation-field/32855/7 "2024-07-31T03:40:32Z")

</div>

Thank you @jose_burrull and @Xclipse Yes I am able to sort correctly.  
Thank you for being awesome!
