KNOWLEDGE BASE

Calculating the difference using lookup function ignoring null values


Published: 28 Jun 2023
Last Modified Date: 03 Jul 2023

Question

Calculating the difference using lookup function ignoring null values:
For example, 
User-added imageUser-added image

Environment

  • Tableau Desktop 
  • Windows 10
  • Excel

Answer

Using PREVIOUS_VALUE function to resolve it.
1. Create a calculated field
name: TS_NULL0
value: IFNULL(SUM([Value]),0)
2. Create a calculated field
name: TS_presentvalue
value: 
IF ZN(([TS_NULL0]))=0 THEN PREVIOUS_VALUE([TS_NULL0] )
ELSE [TS_NULL0] END
3. Create a calculated field
name: TS_difference
value: [TS_presentvalue]-LOOKUP([TS_presentvalue],-1)
4. Drag [TS_difference] into the Measure Values card.
See the attached workbook.
Did this article resolve the issue?