Table Transformer - Calculation NPS out of Pivot data
Asked by Escmarc:

Hi community

I have a table and would like to calculate the NPS (Net Promoter Score) per quarter with the "table transformer" plugin. Data input is dynamic and data are grouped automatically from a CSV. Can someone help me to create a working sql statement?

#### Input: Table transformer table![2024-05-22 10_11_21-Kopie von Migration Feedback results - Technology Domain Engineering - SwisscomP.png](/t5/image/serverpage/image-id/325242iC3FC5C916865861D/image-size/large?v=v2&px=999 "2024-05-22 10_11_21-Kopie von Migration Feedback results - Technology Domain Engineering - SwisscomP.png") {#toc-hId-344027724}

#### Table transformer Formula from a working example {#toc-hId--1463426739}

SELECT  
/\*Calc NPS  
<https://omf.ai/lexikon/nps/>  
\*/  
ROUND(  
((COUNT(CASE WHEN 'Literal of How likely are you to recommend the TDE App Party to a colleague?' \>= 9 THEN 1 END)  
/COUNT(\*))\*100)-  

((COUNT(CASE WHEN 'Literal of How likely are you to recommend the TDE App Party to a colleague?' \<= 6 THEN 1 END)  
/COUNT(\*))\*100),2)

AS NPS  
FROM T1

#### Expected Output {#toc-hId-1024086094}

Fertigstellungszeit : NPS

2023 Q1 : 0

2023 Q3 : -25

#### Data input from CSV {#toc-hId--783368369}

![2024-05-22 10_28_31-Kopie von Migration Feedback results - Technology Domain Engineering - SwisscomP.png](/t5/image/serverpage/image-id/325248i28780F8C15D6091A/image-size/large?v=v2&px=999 "2024-05-22 10_28_31-Kopie von Migration Feedback results - Technology Domain Engineering - SwisscomP.png")


Answers:
author: Stiltsoft support
body: Hi [@Escmarc](/t5/user/viewprofilepage/user-id/5503051) ,

We can suggest trying the following workaround:

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

Here my manually created table is a replication of your pivot table.

Then in the internal Table Transformer macro we flatten the array:

SEARCH / AS @a EX(TEXT('Col 2')-\>split(";")) /  
RETURN(@a-\>'Col 1' AS 'Col 1', _ AS 'Col 2') FROM T1

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

In the second Table Transformer macro we calculate the types of the answers:

SELECT  
'Col 1',  
SUM(IF('Type' = "Promoter", 1, 0)) AS 'Promoter',  
SUM(IF('Type' = "Detractor", 1, 0)) AS 'Detractor',  
SUM(IF('Type' = "Medium number", 1, 0)) AS 'Medium number'  
FROM  
(SELECT \*,  
CASE  
WHEN 'Col 2' \>= 9 THEN "Promoter"  
WHEN 'Col 2' \<= 6 THEN "Detractor"  
ELSE "Medium number"  
END  
AS 'Type'  
FROM T\*)  
GROUP BY 'Col 1'

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

And in the external Table Transformer macro we calculate the NPS:

SELECT 'Col 1',  
'Promoter'/('Promoter'+'Detractor'+'Medium number')\*100 -  
'Detractor'/('Promoter'+'Detractor'+'Medium number')\*100 AS 'NPS'  
FROM T\*

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

Hope it helps your case.

comments: author: Escmarc
body: [@Stiltsoft support](/t5/user/viewprofilepage/user-id/5062270) ,

Ingenious approach. I didn't realise that this kind of nesting worked. I tested and it worked. Thank you very much!


Tags: confluence-data-center

data-center

filter

nps

sql

table

table-filter

table-filter-and-charts

table-filter-charts-spreadsheets

table-transformer