Tableau Funtion: LOOKUP( )

Tableau Function: LOOKUP( )

Category: Table Calculation Functions

Purpose

The LOOKUP() function in Tableau is a table calculation used to reference values from other rows within the same partition. It allows you to retrieve a value from a row that is a specified number of positions before or after the current row, based on the view’s sort order.

In simple terms, LOOKUP() answers:
“What is the value in another row relative to this one?”

Type of Calculations

  • Table calculations

  • Offset-based calculations

  • Relative row comparisons

  • Time-series and trend analysis

LOOKUP() operates after aggregation and depends on the layout, sorting, and partitioning of the view.

Practical Use Cases

  • Calculating period-over-period differences

  • Creating running or cumulative comparisons

  • Comparing current values to previous or next values

  • Performing trend, variance, and growth analysis

  • Supporting advanced calculations with FIRST(), LAST(), or INDEX()


LOOKUP(expression, offset)

ParameterTypeDescription
expressionAggregate or table calculationThe value to retrieve from another row. Must be aggregated when used in a view.
offsetScalar (integer)Number of rows away from the current row to reference. Negative values look backward; positive values look forward.

How It Works?

Logical Principle

LOOKUP() retrieves a value based on relative position:

LOOKUP(expression, offset)

It does not search by key or value. Instead, it:

  1. Identifies the current row

  2. Moves the specified number of rows forward or backward

  3. Returns the value of the expression at that position

Example Formula

SUM([Sales]) - LOOKUP(SUM([Sales]), -1)

Calculates the difference between current and previous sales

What Does It Return?

  • Data Type: Same as the expression parameter

  • Meaning:

    • Returns the value of the expression from a row at the specified offset

    • Returns NULL if the referenced row does not exist

Offset Behavior

OffsetMeaning
-1Previous row
0Current row
1Next row

When Should We Use It?

Use LOOKUP() when you need to:

  • Compare values between rows

  • Calculate growth or change over time

  • Reference prior or future records

  • Perform advanced table calculations

  • Build custom trend or variance logic

Basic Usage

Get the previous row’s value


LOOKUP(SUM([Sales]), -1)

  • Returns the sales value from the previous row

Column Usage

Period-over-period change


SUM([Sales]) - LOOKUP(SUM([Sales]), -1)

Common for month-over-month or year-over-year analysis

Advanced Usage

Compare current value to the first value


SUM([Sales]) - LOOKUP(SUM([Sales]), FIRST())

Uses FIRST() to dynamically reference the first row

Compare current value to the last value


SUM([Sales]) - LOOKUP(SUM([Sales]), LAST())

Useful for end-of-period variance analysis

Custom running difference


IF INDEX() = 1 THEN 0
ELSE SUM([Sales]) - LOOKUP(SUM([Sales]), -1)
END

Prevents NULL values for the first row

Tips and Tricks

  • Always define sorting explicitly

  • Verify Compute Using settings

  • Handle NULL values when offsets exceed boundaries

  • Combine with FIRST() or LAST() for dynamic offsets

  • LOOKUP() does not work without a visible view

  • Cannot be used in row-level or LOD expressions

Related Functions You Might Need

Functions commonly used alongside or as alternatives to LOOKUP():

  • FIRST()

  • LAST()

  • INDEX()

  • PREVIOUS_VALUE()

  • RUNNING_SUM()

  • WINDOW_SUM()

We’ve got plenty of resources to help you master Tableau functions. For more details, check out the official Tableau documentation. Or, if you’re ready for more practice, let’s dive into related functions and build your Tableau skills further!

If you’re ready to harness the full power of Tableau and elevate your data analytics capabilities, our expert Tableau consulting services are here to guide you. Whether you need support with building advanced calculated fields, creating dynamic visual dashboards, or optimizing your data sources for peak performance, our team of experienced Tableau consultants delivers customized solutions designed for your business needs. Visit our Tableau Consulting page to discover how we can help your organization turn data into impactful, insight-driven decisions.

1. What does LOOKUP() do in Tableau?

It retrieves a value from another row based on a relative position (offset).

2. What happens if the offset row does not exist?

LOOKUP() returns NULL.

3. Is LOOKUP() affected by sorting?

Yes, sorting determines which rows are before or after the current row.

4. Is LOOKUP() a table calculation?

Yes, it is evaluated after aggregation and depends on the view layout.

5. Can LOOKUP() be used for time-based analysis?

Yes, it is commonly used for period-over-period comparisons.