Use IF inside COUNT to count rows that meet selected conditions, then combine those counts and divide by the total row count from COUNTR . This pattern supports metrics based on the difference between two categories relative to all rows.
Track customer willingness to recommend your service with Net Promoter Score (NPS), calculated from the balance of promoters and detractors.
Source data: The surveys dataset contains one row per response, with score and response_date columns. Every response has a non-null integer score from 0 to 10.
Formula : (COUNT(IF(score > 8, 1, null)) - COUNT(IF(score < 7, 1, null))) * 100 / COUNTR()
Count promoters: COUNT(IF(score > 8, 1, null)) counts responses scoring 9 or 10. IF returns 1 for matches and null otherwise. COUNT counts the non-null values.
Count detractors: COUNT(IF(score < 7, 1, null)) counts responses scoring 0 to 6 in the same way.
Count all responses: COUNTR() includes every response, including passive scores of 7 or 8.
Calculate NPS: Subtract detractors from promoters, divide by all responses, and scale to the NPS range of -100 to 100. Use numeric formatting for this score.
Example result: 60 promoters and 20 detractors among 100 responses gives an NPS of 40 .
Measure: In a column chart grouped by response month, shows monthly NPS.
Having filter: Keep months with more detractors than promoters using formula < 0 .
npm install @luzmo/nodejs-sdk{
"type": "numeric",
"valid": true
}