Table Transformer Macro - custom SQL
Asked by Megan:

I am trying to calculate two values using the Table Transformer Macro and custom SQL.

The values are Cost of Delay and WSJF. The calculated sum of Cost of Delay is used to calculate WSJF.

Here is a sample table I am utilizing:  

|      Variable      | Score Value |
|--------------------|-------------|
| Revenue            | 8           |
| Cost Savings       | 1           |
| Development Effort | 13          |
| Strategic Value    | 20          |
| Legal Risk         | 5           |
| Time Criticality   | 5           |

I was able to get the two values calculated and displayed separately, and displayed next to one another, but ideally I'd like them to be part of the same table.

To calculate Cost of Delay I used the following function

    SELECT SUM ('Score Value') As 'Cost of Delay'FROM T1WHERE Variable IN ("Revenue", "Cost Savings", "Strategic Value", "Time Criticality");

On it's own it prints out as expected  

| Cost of Delay | 34 |
|---------------|----|

To calculate the other value I need, WSJF, I used the following

    `SELECT 100* (    (SELECT SUM ('Score Value') As 'Cost of Delay'FROM T*WHERE Variable IN ("Revenue", "Cost Savings", "Strategic Value", "Time Criticality")) / (SELECT SUM ('Score Value') As 'Other'FROM T1WHERE Variable IN ("Legal Risk", "Development Effort")  )  ) AS WSJF;`

Which correctly gives me  

| WSJF | 189 |
|------|-----|

Ideally I'd like a table be displayed with the combination of these two  

|     VALUE     | SCORE |
|---------------|-------|
| Cost of Delay | 34    |
| WSJF          | 189   |

I am at a loss for how to connect these two functions into one. I have tried asking stack overflow but every suggestion leads to errors, I'm guessing due to nuances with the macro and confluence.


Answers:
author: Katerina Rudkovskaya [Stiltsoft]
body: Hi [@Megan](/t5/user/viewprofilepage/user-id/208724),  

I can suggest the following workaround for the case:

<br />

![Wed 15-1.png](https://community.atlassian.com/t5/image/serverpage/image-id/243209iD3CF9921302424D5/image-size/large?v=v2&px=999 "Wed 15-1.png")  

The internal Table Transformer macros are used for your original queries and ad additional line with future headers:  

SELECT  
"Score" AS 'Value',  
SUM ('Score Value') AS 'Cost of Delay'  
FROM T1  
WHERE Variable IN ("Revenue", "Cost Savings", "Strategic Value", "Time Criticality")  

![Wed 15-2.png](https://community.atlassian.com/t5/image/serverpage/image-id/243210i2DB71FA9BF3D250F/image-size/large?v=v2&px=999 "Wed 15-2.png")  

SELECT  
"Score" AS 'Value',  
100\*  
(  
(  
SELECT SUM ('Score Value') As 'Cost of Delay'  
FROM T\*  
WHERE Variable IN ("Revenue", "Cost Savings", "Strategic Value", "Time Criticality")  
) / (  
SELECT SUM ('Score Value') As 'Other'  
FROM T1  
WHERE Variable IN ("Legal Risk", "Development Effort")  
)  
)  
AS WSJF  

![Wed 15-3.png](https://community.atlassian.com/t5/image/serverpage/image-id/243211i0EAA591682EBBEDA/image-size/large?v=v2&px=999 "Wed 15-3.png")  

And we also use the Settings -\> Transpose result table option to transpose tables for both Transformers.

<br />

The external Table Transformer macro is used simply to merge your result tables:  

SELECT \* FROM T\*  

![Wed 15-4.png](https://community.atlassian.com/t5/image/serverpage/image-id/243212iCA94A590669BB013/image-size/large?v=v2&px=999 "Wed 15-4.png")  

Hope it helps.

comments: author: Megan
body: Hello - do you know how it is possible to do this calculation if I need to add weights to each of these values? So calculated the weighted sum


author: Stiltsoft support
body: You may switch to the [Table Spreadsheet](https://docs.stiltsoft.com/tfac/dc-server/how-to-use-table-spreadsheet-macro-83100938.html?utm_source=atl_community&utm_medium=answer&utm_campaign=table-filter-traffic-sources&utm_vendorID=1216111) macro and perform your calculations in a more user-friendly Excel-like style.


author: Megan
body: Do you have a calculation if I don't have that macro available to me?


author: Dominic Lagger
body: Hi [@Megan](/t5/user/viewprofilepage/user-id/5158774)

My first thought was to use the union statement like in [this example](https://docs.stiltsoft.com/tfac/cloud/custom-transformation-use-cases-with-advanced-sql-queries-42241587.html#CustomTransformationusecaseswithadvancedSQLqueries-%E2%96%AACreatingclickablelinkstotheJiraissues)

Here is the [UNION](https://docs.stiltsoft.com/tfac/cloud/general-syntax-and-sql-functions-available-42246251.html) documented.

Hope this helps.

Regards, Dominic


Tags: confluence-server

filter

server

table

table-filter

table-filter-and-charts

table-transformer