Custom sort a pivot table but not data table
Asked by Kelsey Lubbe:

Hi all!

I manage a program that uses tickets. My page currently has full data table displaying information about all tickets in the program with a pivot table that summarizes all tickets by status. The Table Filter macro will filter both pivot table and data table.

I would like to sort the pivot in an order defined by me rather than alphabetically. I tried to use Table Transformer with SQL query that does this (WHEN T1.'Status' LIKE "Escalation Review" THEN "1"...) but I think the Table Transformer is attempting to sort the data table and not the pivot table.

Is there a way for me to use Table Transformer to apply only to pivot table? Or another macro to use?

![2.PNG](https://community.atlassian.com/t5/image/serverpage/image-id/154754i9D09CEB4F3949813/image-size/large?v=v2&px=999 "2.PNG")![1.PNG](https://community.atlassian.com/t5/image/serverpage/image-id/154755i6D443D1BC294CD92/image-size/large?v=v2&px=999 "1.PNG")


Answers:
author: Katerina Rudkovskaya [Stiltsoft]
body: Hello [@Kelsey Lubbe](/t5/user/viewprofilepage/user-id/4504162)  

Please try this SQL query:  

SELECT \*  
FROM T\*  
ORDER BY  
CASE  
WHEN T1.'Status' LIKE "Escalation Review" THEN 1  
WHEN T1.'Status' LIKE "Root Cause Analysis" THEN 2  
WHEN T1.'Status' LIKE "Corrective Action Plan" THEN 3  
WHEN T1.'Status' LIKE "Pending Approval" THEN 4  
ELSE 5  
END  

Katerina  
Stiltsoft

comments: author: Kelsey Lubbe
body: Thanks, [@Katerina Rudkovskaya \[Stiltsoft\]](/t5/user/viewprofilepage/user-id/3738229) . This is similar to the output I got previously. Screenshot here compared to output without Table Transformer.

![3.PNG](https://community.atlassian.com/t5/image/serverpage/image-id/154845i3A4B1D5E43E44B98/image-size/large?v=v2&px=999 "3.PNG")

No Table Transformer

![4.PNG](https://community.atlassian.com/t5/image/serverpage/image-id/154846iCBBE34B1F82B8568/image-size/large?v=v2&px=999 "4.PNG")


author: Katerina Rudkovskaya [Stiltsoft]
body: Hi [@Kelsey Lubbe](/t5/user/viewprofilepage/user-id/4504162) ,  

Note that in your case, you can change the order of columns just choosing them with the help of autocompletion in Table Transformer according to your needs.  

Please see my exampe below:  

![2021-08-06_17h11_50.png](https://community.atlassian.com/t5/image/serverpage/image-id/154951i09C7617BE2F09DA8/image-size/large?v=v2&px=999 "2021-08-06_17h11_50.png")![2021-08-06_17h12_13.png](https://community.atlassian.com/t5/image/serverpage/image-id/154952i1B93F760005C2374/image-size/large?v=v2&px=999 "2021-08-06_17h12_13.png")

Use autocompletion: start typing, e.g., **T1.** and choose the column.

![2021-08-06_17h14_13.png](https://community.atlassian.com/t5/image/serverpage/image-id/154953i32D79244B28AD068/image-size/large?v=v2&px=999 "2021-08-06_17h14_13.png")![2021-08-06_17h12_04.png](https://community.atlassian.com/t5/image/serverpage/image-id/154954i7CC2C436820B590F/image-size/large?v=v2&px=999 "2021-08-06_17h12_04.png")  

Katerina  
Stiltsoft


author: Kelsey Lubbe
body: Hi Katerina,

Thank you! That did sort the top pivot table in the appropriate order.

Is it expected that the full data table (from Table from CSV in original screenshot) is hidden? The reason why I have the Pivot Table and data table nested inside the Table Transformer macro is because I want the Table Filter macro to apply to both Pivot Table and data table.


author: Andrey Khaneev _StiltSoft_
body: Hi [@Kelsey Lubbe](/t5/user/viewprofilepage/user-id/4504162),

You can set it to show or hide the source table in both [Pivot Table](https://docs.stiltsoft.com/display/TFAC/Managing+pivot+table+options#Managingpivottableoptions-Show/Hidethesourcetable.1) and [Table Transformer](https://docs.stiltsoft.com/pages/viewpage.action?pageId=42241562#Defaultautomaticpresets-Viewoptions) macros.


author: Kelsey Lubbe
body: Oh my, I was expecting a checkbox to display/hide and did not expect a dropdown option. My bad on missing that! Thanks so much for the help [@Andrey Khaneev _StiltSoft_](/t5/user/viewprofilepage/user-id/781696) [@Katerina Rudkovskaya \[Stiltsoft\]](/t5/user/viewprofilepage/user-id/3738229) !


author: Sean M
body: [@Andrey Khaneev _StiltSoft_](/t5/user/viewprofilepage/user-id/781696) I think I discovered an issue that simply needs some error handling. It looks like if any of the defined columns does not have a task of that status (meaning count 0), it throws an error and the whole table will not display:

Error: Column does not exist: Count STATUSNAME

Does anyone have a recommendation on how to handle that scenario? I have removed the column with a 0 count and the table displays correctly again. However, we would like to display that column count when it does have tasks in that status...

Thanks so much, the above info has been great in getting us this far!


author: Andrey Khaneev _StiltSoft_
body: [@Sean M](/t5/user/viewprofilepage/user-id/863164), please try to use COALESCE function in this case:

```
COALESCE(T1.'Count STATUSNAME')
```


author: Randall Hinds
body: [@Katerina Rudkovskaya \[Stiltsoft\]](/t5/user/viewprofilepage/user-id/3738229) Thanks for the answer above. It definitely solved for my similar column sorting challenge.

I am using this solution to show Status Counts across a number of work efforts.   
Each Pivot Table is pulling Status Counts from a specific JQL statement from Jira Macro.   

SELECT T1.'Assignee',  
T1.'Count Open',  
T1.'Count In Progress',  
T1.'Count On Hold',  
T1.'Count Resolved',  
T1.'Count Closed',  
T1.'Count',  
T1.\*  
FROM T\*  

When a JQL return does find any cards in Resolved Status, the Transformer SQL tells me so with **Error: Column does not exist: Count Resolved**   
--\> Is there a way I can sort all columns for Status, regardless of a card being in that status?


author: Stiltsoft support
body: Hi [@Randall Hinds](/t5/user/viewprofilepage/user-id/5231752) ,

Isn't the Sean's question from Sep 21, 2021 similar to your case?

Please check the two comments right above yours: the question by Sean and the answer by Andrey about the COALESCE function.


author: Randall Hinds
body: Thank you, [@Stiltsoft support](/t5/user/viewprofilepage/user-id/5062270) ! That function does let me arrange Status columns even when there are no cards to count in a particular Status.

However, the header row shows the COALESCE function statement \& adds so much empty space in the table.  
![2023-05-31_10-02-33.png](https://community.atlassian.com/t5/image/serverpage/image-id/260507iDB522A752D81F565/image-size/large?v=v2&px=999 "2023-05-31_10-02-33.png")

Even though the SELECT function lets me reorder Status where there are cards to Count, it is less visually appealing than simply letting a Pivot Table sort Status alphabetically.

Table Transformer example with SELECT function wrapper on Pivot  
![2023-05-31_10-11-37.png](https://community.atlassian.com/t5/image/serverpage/image-id/260511i52061852B96147A1/image-size/large?v=v2&px=999 "2023-05-31_10-11-37.png")

example with only Pivot Table  
![2023-05-31_10-10-16.png](https://community.atlassian.com/t5/image/serverpage/image-id/260510i84C4863472FA246E/image-size/large?v=v2&px=999 "2023-05-31_10-10-16.png")

I want to reorder columns showing the colored names even when there are no cards in a particular Status... It seems I can do all of these things, but I can't figure out how to do them at once.   
Is there a way I can have my cake \& eat it too?


author: Stiltsoft support
body: Hi [@Randall Hinds](/t5/user/viewprofilepage/user-id/5231752) ,

Seems that you've forgot to rename your columns:

COALESCE(T1.'Col 1') AS 'Col 1' (the same goes for every column)

For your case it may be very neat, cause you may get rid of the "Count" prefix:

COALESCE(T1.'Count Open') AS 'Open'


author: Randall Hinds
body: It's like magic! You guys rock me!

![2023-05-31_11-55-03.png](https://community.atlassian.com/t5/image/serverpage/image-id/260539i7F300E454078D2EA/image-size/large?v=v2&px=999 "2023-05-31_11-55-03.png")

SELECT T1.'Assignee',  
COALESCE(T1.'Count Open') AS 'Open',  
COALESCE(T1.'Count Defined') AS 'Defined',  
COALESCE(T1.'Count In Progress') AS 'In Progress',  
COALESCE(T1.'Count On Hold') AS 'On Hold',  
COALESCE(T1.'Count Resolved') AS 'Resolved',  
COALESCE(T1.'Count Closed') AS 'Closed',  
COALESCE(T1.'Count') AS 'Total'  
FROM T\*


author: Sean M
body: I think I discovered an issue that simply needs some error handling. It looks like if any of the defined columns does not have a task of that status (meaning count 0), it throws an error and the whole table will not display:

Error: Column does not exist: Count STATUSNAME

Does anyone have a recommendation on how to handle that scenario? I have removed the column with a 0 count and the table displays correctly again. However, we would like to display that column count when it does have tasks in that status...

Thanks so much, the above info has been great in getting us this far!

Added to reply section. Please disregard this.


Tags: confluence-cloud

server