Ravindra BagaleCourses & study guides मराठी Track your progress

13. DAX: Data Analysis Expressions

13.22 LOOKUPVALUE, TREATAS and CROSSFILTER

TREATAS in DAX with example — in 30 seconds

What: TREATAS uses the values of one column as a filter on another table that has no relationship.

Why: City targets come from a separate Excel sheet. There is no relationship, so every city shows the full target.

Tiny example: Without TREATAS every city shows ₹3,000. With TREATAS, Pune shows its own ₹1,500.

First look at the data (before → after):

TREATAS in DAX with example: before and after tables, fictional Blinkit Maharashtra data

Same before / after tables as text

Before — CityTarget table (no relationship)

City Target Amount (₹)
Pune 1,500
Nashik 500
Mumbai 1,000

Before — Matrix with a plain SUM — wrong

City Total Sales Target
Pune ₹1,200 ₹3,000
Nashik ₹300 ₹3,000
Mumbai ₹1,100 ₹3,000

Formula / click path

City Target = CALCULATE(SUM(CityTarget[Target Amount]),
                TREATAS(VALUES(DarkStore[City]), CityTarget[City]))

After — Matrix with TREATAS — right

City Total Sales City Target
Pune ₹1,200 ₹1,500
Nashik ₹300 ₹500
Mumbai ₹1,100 ₹1,000
Total ₹2,600 ₹3,000

Do it step by step:

  1. Load CityTarget with Home › Enter data. Do not create a relationship.
  2. Build a matrix. Rows = DarkStore[City]. Add a plain sum of Target Amount. Every row shows ₹3,000.
  3. Click Home › New measure. Type the City Target formula.
  4. Replace the plain sum with City Target.
  5. Check: Pune ₹1,500, Nashik ₹500, Mumbai ₹1,000, Total ₹3,000.
  6. Check the spelling. "Pune" must be written the same way in both tables.

RELATED needs a relationship. These three functions are used when there is no relationship, or when you want to change the direction for just one measure.

LOOKUPVALUE searches a column without a relationship. It returns an error if several rows match, and blank if none match. It is fine for a small, stable lookup. It is slower and less safe than RELATED on a real relationship.

Store Pin =
LOOKUPVALUE(
    DarkStore[PIN],
    DarkStore[Store ID], Orders[Store ID]
)

Use that as a calculated column only if you cannot relate the tables. If the store id repeats, fix the dimension; do not wrap the error.

TREATAS (treat as) builds a filter from a column of values and applies it to another column that is not related. The columns must mean the same thing (both city names, both stored the same way).

City Target =
CALCULATE(
    SUM(CityTarget[Target Amount]),
    TREATAS(VALUES(DarkStore[City]), CityTarget[City])
)

CROSSFILTER changes the relationship direction for one calculation. The model relationship can stay single.

Customers With Orders =
CALCULATE(
    DISTINCTCOUNT(Customer[Customer ID]),
    CROSSFILTER(Orders[Customer ID], Customer[Customer ID], BOTH)
)

BOTH here lets the fact table filter the customer table for this measure only. Leaving the model on Both would do that for every visual.

Worked example, fictional targets (not a company plan):

City Target Amount
Pune 10000
Mumbai 4000

There is no relationship from CityTarget to DarkStore. A card filtered to Pune should show ₹10000. TREATAS passes the visible city "Pune" onto CityTarget[City]. If the target table says "pune" in a different case, you get blank. Clean the text in Power Query first. LOOKUPVALUE(CityTarget[Target Amount], CityTarget[City], "Pune") is the one-city version and will not follow a multi-select slicer. TREATAS will.

Ravindra Bagale's Tip

Many students use TREATAS to hide a missing relationship, then wonder why totals ignore typos. TREATAS is an exact filter, not a fuzzy match. If the same city list is used everywhere, build the relationship instead. Do not keep CROSSFILTER(..., BOTH) on the whole model; keep it only inside that one measure. Don't forget this.

Loading DAX practice tasks...

  1. Target on each store (Easy): Calculated column on Store: Target = the Target from CityTarget for the store City.
  2. Product name on each order (Easy): Calculated column on Orders: Product Name = ProductName from Product for the row ProductID.
  3. Target for the customer city (Easy): Calculated column on Customer: City Target = Target from CityTarget for the customer City.
  4. Pune target as a measure (Medium): Measure: Pune Target = the Target in CityTarget where City is "Pune".
  5. Why the plain SUM fails (Medium): Shown per Store City. Measure: All Targets = SUM of CityTarget[Target].
  6. Target per city with TREATAS (Medium): Shown per Store City. Measure: City Target = SUM of Target, filtered by the cities in the row through TREATAS.
  7. Target for two picked cities (Medium): Slicer: Store City = Pune and Mumbai. Measure: City Target = target total through TREATAS.
  8. Gap to target (Hard): Shown per Store City. Measure: Target Gap = [Total Sales] − the TREATAS target.
  9. Target achieved % (Hard): Shown per Store City. Measure: Achieved = [Total Sales] ÷ the TREATAS target. Use DIVIDE.
  10. Line cost by lookup (Hard): Calculated column on Orders: Line Cost = Qty × the CostPrice looked up from Product.

Ravi Sir's Cards for this lesson

मराठी cards