msyavuz commented on code in PR #35619:
URL: https://github.com/apache/superset/pull/35619#discussion_r2468442228


##########
superset-frontend/src/visualizations/TimeTable/utils/valueCalculations/valueCalculations.ts:
##########
@@ -42,25 +42,47 @@ export function calculateTimeValue(
       errorMsg: `The time lag set at ${timeLag} is too large for the length of 
data at ${reversedEntries.length}. No data available.`,
     };
 
-  let laggedValue: number | null;
+  let laggedValue: number | null = null;
 
-  if (timeLag < 0)
-    laggedValue = reversedEntries[totalLag + timeLag][valueField];
-  else laggedValue = reversedEntries[timeLag][valueField];
-
-  if (typeof laggedValue !== 'number' || typeof recent !== 'number')
-    return { value: null };
+  if (timeLag < 0) {
+    const index = totalLag + timeLag;
+    if (index >= 0 && index < totalLag) {
+      laggedValue = reversedEntries[index][valueField];
+    }
+  } else if (timeLag === 0) {
+    laggedValue = recent;
+  } else {
+    // Find the Nth actual data point, skipping null values
+    let dataPointsFound = 0;
+    reversedEntries.slice(1, totalLag).some(entry => {
+      const searchValue = entry[valueField];
+      if (typeof searchValue === 'number') {
+        dataPointsFound += 1;
+        if (dataPointsFound === timeLag) {
+          laggedValue = searchValue;
+          return true;
+        }
+      }
+      return false;
+    });
+  }
 
-  let calculatedValue: number;
+  // For comparison operations, both values must be numbers
+  if (typeof laggedValue === 'number' && typeof recent === 'number') {
+    if (column.comparisonType === 'diff')
+      return { value: recent - laggedValue };
+    if (column.comparisonType === 'perc')
+      return { value: recent / laggedValue };
+    if (column.comparisonType === 'perc_change')
+      return { value: recent / laggedValue - 1 };
+  }
 
-  if (column.comparisonType === 'diff') calculatedValue = recent - laggedValue;
-  else if (column.comparisonType === 'perc')
-    calculatedValue = recent / laggedValue;
-  else if (column.comparisonType === 'perc_change')
-    calculatedValue = recent / laggedValue - 1;
-  else calculatedValue = laggedValue;
+  // If recent is null, return null (can't do meaningful calculations)
+  if (recent === null) {

Review Comment:
   Nice catch, fixed!



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to