Table Transformer - Merge Cells with Duplicate Rows
Asked by Emily Berg:

Hello Community!

I would like to take the following information:

|----------|----------|-----------|
| Header 1 | Header 2 | Header 3  |
| Fruit    | Letter A | Apple     |
| Fruit    | Letter A | Apricot   |
| Fruit    | Letter B | Banana    |
| Fruit    | Letter B | Blueberry |
| Fruit    | Letter C | Cherry    |

And transform it into the following:

\*Updated 6-May-2021 - Original post unable to process merged cells. Removed original content and replaced with image of desired output.

![Snag_2879231c.png](https://community.atlassian.com/t5/image/serverpage/image-id/142348iA47871910DD8FFB4/image-size/large?v=v2&px=999 "Snag_2879231c.png")

Thank you for your insight!  
Emily


Answers:
author: Andrey Khaneev _StiltSoft_
body: Hello Emily,

Here is the SQL:

```
SELECT T1.'Header 1', T1.'Header 2', 
FORMATWIKI(SUM(T1.'Header 3' + "\n")) AS 'Header 3'
FROM T1
GROUP BY T1.'Header 1', T1.'Header 2'
```

And the result:

![2021-05-07_19h31_05.png](https://community.atlassian.com/t5/image/serverpage/image-id/142579i069F6620741D4AC0/image-size/large?v=v2&px=999 "2021-05-07_19h31_05.png")

Or you can use the [Pivot Table](https://docs.stiltsoft.com/display/TFAC/How+to+use+Pivot+Table+macro?src=contextnavpagetreemode) macro (it is also shipped with the [Table Filter and Charts](https://marketplace.atlassian.com/apps/27447/table-filter-and-charts-for-confluence?hosting=datacenter&tab=overview) app):

![2021-05-07_19h32_11.png](https://community.atlassian.com/t5/image/serverpage/image-id/142581i25201CADF60E9C9C/image-size/large?v=v2&px=999 "2021-05-07_19h32_11.png")

Don't forget to hide totals:

![2021-05-07_19h34_14.png](https://community.atlassian.com/t5/image/serverpage/image-id/142582i92F55BE197808940/image-size/large?v=v2&px=999 "2021-05-07_19h34_14.png")

comments: author: Madison Small
body: Is there a way to use SQL but have each item after the merged column appear in it's own row with a border still around it? I know the "\\n" creates a line break, but it is still making my table very messy. I would prefer them to have their own row.


author: Katerina Kovriga {Stiltsoft}
body: Hi [@Madison Small](/t5/user/viewprofilepage/user-id/4812970) ,

The Table Transformer macro sees the source table as a mini SQL database, so it can merge cells into one big cell by joining their contents. If we leave a border, then they will be counted as two separate cells and you won't get an aggregated table at all.

Maybe it will be easier to read your table when using ", " instead of the "\\n": you'll get "Apple, Apricot" as a result.


author: matthew_hester94
body: [@Katerina Kovriga {Stiltsoft}](/t5/user/viewprofilepage/user-id/1464746) How would you do something like this with labels? When I try to merge cells that container labels, then labels are turned into plain strings and lose their formatting.


author: Katerina Kovriga {Stiltsoft}
body: Try to use the Pivot Table macro instead of the Table Transformer - the initial formatting of the merged data will be preserved.


author: matthew_hester94
body: Maybe this is not as similar as I thought. So I would like to keep the formatting of my result table which would be a vertical header column with the data in a column to the right of the headers. I am merging 4 tables together and need to get just the column that contains labels to merge together. Unfortunately, the pivot table manipulates the result table too much and also does a lot of header renaming.

Desired Result:

header 1 \| data

header 2 \| Label1, label2, label 3, etc (These can be on newlines too)

Originals:

Table 1

header 1 \| header 2

--------- ---------

Data. label 1, label 2

Table 2

header 1 \| header 2

--------- ---------

Data. label, 2, label 3

etc


author: Katerina Kovriga {Stiltsoft}
body: If you merge labels into one cell, then the Table Transformer macro may lose the original formatting because it is a new cell with simple text strings for it.

We have similar ticket in our backlog and added your vote to it, when the feature is implemented, we'll get back to the thread.


author: Megan
body: Has this feature been implemented?


author: Stiltsoft support
body: Hi [@Megan](/t5/user/viewprofilepage/user-id/5158774) ,

You may use the Pivot Table macro as for now.

If necessary, you may wrap the Pivot Table macro in the Table Transformer macro on top and rename the required columns.


author: Megan
body: When I add the table transformer on top, it unmerges the cells. Is there a way to keep the merge? ![Screen Shot 2023-08-29 at 10.07.06 AM.png](https://community.atlassian.com/t5/image/serverpage/image-id/277763i7136A1BC2D1FD2D1/image-size/large?v=v2&px=999 "Screen Shot 2023-08-29 at 10.07.06 AM.png")


author: Stiltsoft support
body: Unfortunately, no.


author: Emily Berg
body: Hi [@Katerina Kovriga {Stiltsoft}](/t5/user/viewprofilepage/user-id/1464746) and the [@Stiltsoft support](/t5/user/viewprofilepage/user-id/5062270) Team!

I have an expanded challenge on the above question. I was able to do the first level of grouping (see the original to the 1st level grouping images). Now, I am trying to get to the final state, to group the two UR-30 rows into a single row, and the two UR-532 into a single row.

Your suggestions are much appreciated, thank you!

![Original.png](https://community.atlassian.com/t5/image/serverpage/image-id/336819i401FE8494D61CA35/image-size/large?v=v2&px=999 "Original.png")![1st Grouping.png](https://community.atlassian.com/t5/image/serverpage/image-id/336820i671325F41FE58B11/image-size/large?v=v2&px=999 "1st Grouping.png")![Desired Output.png](https://community.atlassian.com/t5/image/serverpage/image-id/336821iB316D6A6BC50830B/image-size/large?v=v2&px=999 "Desired Output.png")


author: Nikita Kamai
body: Hello Emily,  

It's Nikita from Stiltsoft.  

Overall, what you are asking is not feasible with Table Transformer as per inability to work with merged cells, therefore let us try tweak\&trick the display a little to move to the expected result as close as possible.  

Here is the query with some slight display differences, I guess the output should still be 95-97% similar:

*CREATE TABLE TX;*   
*INSERT INTO TX*   
*SELECT ROWNUM() as 'id','UR Key' ,*   
*CONCAT_VIEW_AGGR(FORMATWIKI( 'UR - Tested by Key'+ ", ")) as 'UR - Tested by Key',*   
*'SR Key' ,*   
*'SR - Tested by Key',*   
*'PR Key',*   
*'PR - Tested by Key' FROM (SELECT T1.'UR Key' ,*   
*'UR - Tested by Key',*   
*'SR Key' ,*   
*CONCAT_VIEW_AGGR(FORMATWIKI( DISTINCT T1.'SR - Tested by Key' + ", ")) as 'SR - Tested by Key',*   
*'PR Key',*   
*'PR - Tested by Key'*   
*FROM T\* WHERE 'UR Key'IS NOT EMPTY*   
*GROUP BY T1.'UR Key', 'SR Key', 'PR Key','PR - Tested by Key','UR - Tested by Key')*   
*GROUP BY 'UR Key', 'SR Key', 'PR Key','PR - Tested by Key','SR - Tested by Key'*   
*ORDER BY 'UR Key'-\>split("-")-\>1::int;*

<br />

*SELECT CASE WHEN 'id' IN (SELECT MIN('id') from TX GROUP BY 'UR Key') THEN 'UR Key'*   
*ELSE FORMATWIKI("{cell:border-top-color: white; border-top-width: 2px;}", NULL, "{cell}") END as 'UR Key',*   
*CASE WHEN 'id' IN (SELECT MIN('id') from TX GROUP BY 'UR Key','UR - Tested by Key') THEN 'UR - Tested by Key'-\>slice(0,-2)*   
*ELSE FORMATWIKI("{cell:border-top-color: white; border-top-width: 2px;}", NULL, "{cell}") END as 'UR - Tested by Key',*

*'SR Key',*   
*CASE WHEN 'id' IN (SELECT MIN('id') from TX GROUP BY 'UR Key','SR - Tested by Key') THEN 'SR - Tested by Key'-\>slice(0,-2)*   
*ELSE FORMATWIKI("{cell:border-top-color: white; border-top-width: 2px;}", NULL, "{cell}") END as 'SR - Tested by Key',*   
*'PR Key','PR - Tested by Key'*   
*FROM TX
<br />* Hopefully, this will be helpful.  

Best wishes,  
Nikita


Tags: confluence-server

filter

macro

server

stiltsoft

table

table-filter

table-filter-and-charts

table-filter-charts-spreadsheets

table-transformer