Sub topic 11
Using TREATAS as a Virtual Relationship
This chapter assumes you know star schemas, one-to-many relationships, and basic DAX with SUM and CALCULATE. It also assumes you have a bridge table that links Customers to Accounts. The previous chapter used CROSSFILTER to change filter direction. Here you use TREATAS to avoid that change altogether.
The idea
A normal relationship moves filters between two columns that the model connects. TREATAS moves a filter without a relationship. It takes a set of values and tells DAX to treat them as values of a column you name.
The syntax is TREATAS ( table_expression, column1 [, column2 ...] ). The columns in the table expression are matched by position to the target columns. You use it as a filter argument inside CALCULATE.
The model
The example uses these tables:
Customersis a dimension with one row per customer.Accountsis a dimension with one row per account.Transactionsis a fact table with one row per transaction and anAccountIDcolumn.AccountBridgeholds unique pairs ofAccountIDandCustomerID.
AccountBridge has a one-to-many relationship from Customers to the bridge. In this version, the bridge has no active relationship to Accounts or to Transactions. A selection of customers filters the bridge, and TREATAS passes the surviving account IDs to the fact table.
The bridge comes from a calculated table that removes duplicate pairs.
AccountBridge =
DISTINCT (
SELECTCOLUMNS (
AccountCustomer,
"AccountID", AccountCustomer[AccountID],
"CustomerID", AccountCustomer[CustomerID]
)
)The base measure is the same one used in earlier chapters.
Total Amount = SUM ( Transactions[Amount] )The TREATAS measure
Amount for Selected Customers =
CALCULATE (
SUM ( Transactions[Amount] ),
TREATAS ( VALUES ( AccountBridge[AccountID] ), Transactions[AccountID] )
)The measure works in three steps:
- A slicer or visual selects customers. The relationship from
CustomerstoAccountBridgefilters the bridge to those customers' rows. VALUES ( AccountBridge[AccountID] )returns the account IDs still visible in the bridge.TREATASapplies those IDs as a filter onTransactions[AccountID], so SUM only adds matching transactions.
Comparing with CROSSFILTER
| Point | CROSSFILTER | TREATAS |
|---|---|---|
| Needs a relationship between the columns | Yes | No |
| What it changes | Direction of an existing relationship | Adds a filter from a list of values |
| Model diagram | Needs a relationship path to Transactions | Needs only the link from Customers to the bridge |
| Typical risk | Ambiguous paths if used with other bidirectional relationships | Values must match in data type and content |
Common errors
- Mismatched data types. If
AccountIDis text in one table and a whole number in the other, the filter matches nothing. Check the types in Power Query. - Wrong column order. With several columns, TREATAS matches by position, not by name. Keep the order the same on both sides.
- Expecting totals to be unique. If one account belongs to two customers, it appears under both, and the grand total can count it twice. The next chapters cover this.
- Using it where a relationship is better. TREATAS must be repeated in every measure that needs it. If many measures need the link, a real relationship is easier to maintain.
Exercise
A visual shows Customers[CustomerName] and the measure Amount for Selected Customers. The amounts are blank for every customer. AccountBridge[AccountID] is text and Transactions[AccountID] is a whole number. What is the likely cause, and how do you fix it?
Show answer
TREATAS compares values, and text "101" does not match the number 101, so no transactions are filtered in. Change both columns to the same data type, for example whole number, in Power Query, then refresh the model.