LOOKUPVALUE in DAX: when to use it, and RELATED instead

How LOOKUPVALUE finds a value in DAX by one or more search columns, the alternate result, the error when several different values match, examples for prices by date and exchange rates, and why RELATED or a relationship is usually better.

Short answer: LOOKUPVALUE ( result_column, search_column, search_value, ... [, alternate] ) returns the value of result_column in the row where every search column equals its search value, like a VLOOKUP with several keys. No match returns BLANK (or the alternate); several matching rows with different results return an error (or the alternate). If a relationship exists between the tables, Microsoft notes RELATED is usually more efficient. Use LOOKUPVALUE for lookups without a relationship: a rate valid on a date, a value by two keys.

People coming from Excel reach for LOOKUPVALUE for every lookup. In a model with relationships, most lookups are RELATED, and the rest are often better solved in Power Query.

What does it look like?

-- calculated column on Sales: the exchange rate for the order's currency and date
Rate =
LOOKUPVALUE (
    Rates[Rate],
    Rates[Currency], Sales[Currency],
    Rates[Date], Sales[Order Date],
    1
)

Amount EUR = Sales[Amount] * Sales[Rate]

The last argument, 1, is the alternate result: rows with no rate get 1. Without it they would get BLANK.

How does it behave with matches?

Matching rowsResult valuesReturns
noneBLANK or alternate
one0.920.92
severalall 0.920.92
several0.92 and 0.95error, or the alternate if given
LOOKUPVALUE outcomes

The error is "A table of multiple values was supplied where a single value was expected". It usually means the lookup table has duplicates; fix the data rather than hiding it with an alternate.

-- with a relationship Sales[Product Key] to Product[Product Key]
Category = RELATED ( 'Product'[Category] )

-- the same without using the relationship, slower
Category Lookup = LOOKUPVALUE ( 'Product'[Category], 'Product'[Product Key], Sales[Product Key] )

Relationships are indexed; LOOKUPVALUE searches. A relationship also filters in visuals, which a looked-up column does not.

What about "the price valid on that date"?

Price lists with valid-from dates have no exact date match. LOOKUPVALUE needs exact matches, so use the latest row on or before the date:

-- calculated column on Sales
Unit List Price =
VAR D = Sales[Order Date]
VAR P = Sales[Product Key]
RETURN
    MAXX (
        TOPN (
            1,
            FILTER ( Prices, Prices[Product Key] = P && Prices[Valid From] <= D ),
            Prices[Valid From], DESC
        ),
        Prices[Price]
    )

For large tables, do this join in Power Query or the source.

What goes wrong?

  • Duplicates in the lookup table. The error appears only for keys with different results; check with COUNTROWS per key.
  • Using it in measures over many rows. Each call searches; in an iterator over millions of rows this is slow.
  • Types that differ. A text key against a number key never matches; align data types.
  • Instead of a relationship. If the key is unique on one side, create the relationship and use RELATED.

Where can you check this?

Where do you start?

Replace each LOOKUPVALUE that follows an existing relationship with RELATED, and move date-range lookups to Power Query. Relationship functions covers RELATED and inactive relationships.

Keep reading

More from the blog.

Every post

Next

Building this? Say hello.

A question about this post, a pattern you would build differently, or the next thing you want written out: Behnia reads every message.