Creating jmeter transaction comparison table using Runtime Fields

I am trying to build a table in lens for the Jmeter test results.
The table is supposed to have 4 columns:

  1. SampleLabel
  2. 90th Perc of ResponseTime for Test1
  3. 90th Perc of ResponseTime for Test2
  4. Difference of above 2 ResponseTime field aggregation

The table looks like this at the moment.

Each test is uniquely identified by TestId field

I am trying to add the 4th column which shows difference of both aggregated ResponseTime columns but my Runtime Painless script is returning null

// Retrieve the dynamic 'TestId' values from Kibana control
def testId1 = params.testId1; // First TestId selected
def testId2 = params.testId2; // Second TestId selected

// Initialize variables to store ResponseTime for each TestId
def responseTimeTest1 = null;
def responseTimeTest2 = null;

// Check if 'TestId' matches testId1 or testId2 and capture the ResponseTime
if (doc['TestId.keyword'].size() > 0 && doc['SampleLabel.keyword'].size() > 0) {
    def sampleLabel = doc['SampleLabel.keyword'].value;
    def testId = doc['TestId.keyword'].value;
    def responseTime = doc['ResponseTime'].value;

    // Check if document belongs to TestId1
    if (testId == testId1) {
        responseTimeTest1 = responseTime; // Store ResponseTime for TestId1
    }
    // Check if document belongs to TestId2
    else if (testId == testId2) {
        responseTimeTest2 = responseTime; // Store ResponseTime for TestId2
    }
}

// Calculate the difference if both TestId ResponseTimes are captured
if (responseTimeTest1 != null && responseTimeTest2 != null) {
    emit(responseTimeTest2 - responseTimeTest1) // Return the difference
}

What am I missing in the script ? Is there a better way to achieve the difference column ?

A sample document has been provided below. All documents for all tests are sent to a single index : jmetertest in this case

{
  "AllThreads": [
    2
  ],
  "BodySize": [
    114
  ],
  "Build": [
    "2024Q3"
  ],
  "Build.keyword": [
    "2024Q3"
  ],
  "Bytes": [
    114
  ],
  "ConnectTime": [
    2
  ],
  "ContentType": [
    ""
  ],
  "ContentType.keyword": [
    ""
  ],
  "DataType": [
    "text"
  ],
  "DataType.keyword": [
    "text"
  ],
  "ElapsedTime": [
    1705775400000
  ],
  "ErrorCount": [
    0
  ],
  "FailureMessage": [
    ""
  ],
  "FailureMessage.keyword": [
    ""
  ],
  "GrpThreads": [
    2
  ],
  "IdleTime": [
    0
  ],
  "InjectorHostname": [
    "HM74C24-PC"
  ],
  "InjectorHostname.keyword": [
    "HM74C24-PC"
  ],
  "Latency": [
    21
  ],
  "ResponseCode": [
    "200"
  ],
  "ResponseCode.keyword": [
    "200"
  ],
  "ResponseTime": [
    337
  ],
  "SampleCount": [
    1
  ],
  "SampleEndTime": [
    "2024-11-20T22:26:01.996Z"
  ],
  "SampleLabel": [
    "Transaction_1"
  ],
  "SampleLabel.keyword": [
    "Transaction_1"
  ],
  "SampleStartTime": [
    "2024-11-20T22:26:01.659Z"
  ],
  "SentBytes": [
    0
  ],
  "Success": [
    true
  ],
  "TenantName": [
    "Sony"
  ],
  "TenantName.keyword": [
    "Sony"
  ],
  "TestElement.name": [
    "Thread-6"
  ],
  "TestElement.name.keyword": [
    "Thread-6"
  ],
  "TestId": [
    "2024Q3005"
  ],
  "TestId.keyword": [
    "2024Q3005"
  ],
  "TestStartTime": [
    1732141561567
  ],
  "ThreadName": [
    "Sales_Plan_Yearly 1-1"
  ],
  "ThreadName.keyword": [
    "Sales_Plan_Yearly 1-1"
  ],
  "Timestamp": [
    1732141561659
  ],
  "_id": "nzeuS5MBvp8rWDMIynZJ",
  "_index": "jmetertest",
  "_score": 0
}

|