Sub topic 11
Using TREATAS as a virtual relationship
This chapter assumes you can read a Power BI model view and write basic DAX with SUM and CALCULATE. It also assumes you know the bridge table from the previous chapters. Here the bridge stays in the model, but you do not wire it to the fact table with a physical relationship. A measure does that wiring at query time.
What TREATAS does
TREATAS takes a table expression and makes its values act as a filter on columns of another table. Those columns do not need a relationship to the source of the values. The result is a "virtual relationship" that exists only inside one calculation.
The syntax is:
TREATAS ( table_expression, target_column [, target_column ...] )The first argument supplies the values. The remaining arguments name the columns that receive them. TREATAS is normally used as a filter argument inside CALCULATE.
Two rules apply:
- The number of target columns must equal the number of columns in the table expression.
- Values are matched by position, not by name. The first column of the expression filters the first target column, and so on.
The model for this chapter
The examples use four tables:
| Table | Role | Key column |
|---|---|---|
| Customers | Dimension | CustomerID |
| Accounts | Dimension | AccountID |
| Transactions | Fact | AccountID (many rows per account) |
| AccountBridge | Bridge | CustomerID and AccountID pairs |
One customer can own several accounts, and one account can have several owners, so Customers and Accounts have a true many-to-many relationship. The bridge holds one row for each valid pair. The earlier chapter built it with a calculated table:
AccountBridge =
DISTINCT (
SELECTCOLUMNS (
AccountCustomer,
"AccountID", AccountCustomer[AccountID],
"CustomerID", AccountCustomer[CustomerID]
)
)The only relationship this chapter needs is a standard one-to-many from Customers[CustomerID] to AccountBridge[CustomerID], with single-direction filtering. There is no relationship from the bridge to Accounts or Transactions. The base measure on the fact table is:
Total Amount = SUM ( Transactions[Amount] )Without a path from the bridge to Transactions, a Customer selection stops at the bridge. Total Amount then shows the same unfiltered number for every customer.
The measure
This measure carries the customer filter across the gap:
Amount for Selected Customers =
CALCULATE (
SUM ( Transactions[Amount] ),
TREATAS ( VALUES ( AccountBridge[AccountID] ), Transactions[AccountID] )
)The measure evaluates in four steps:
- A visual or slicer filters
Customers. The one-to-many relationship passes that filter toAccountBridge, so only that customer's rows stay visible. VALUES ( AccountBridge[AccountID] )returns the distinctAccountIDvalues still visible in the bridge.TREATASre-labels those values asTransactions[AccountID]values and applies them as a filter.SUMadds the amounts on the remainingTransactionsrows.
Why the filter comes from VALUES
VALUES respects the current filter context. Inside a matrix row for one customer, it returns only that customer's accounts. In the total row, no single customer is selected, so it returns every account in the bridge.
This explains how totals behave. TREATAS hands Transactions a list of distinct account values, not a list of customer-account pairs. If two customers share an account, that account appears once in the filter, and the total row counts its transactions once. The grand total therefore does not double count.
Row values still repeat shared accounts. Each customer's row includes the full amount of every account they own, so the rows can add up to more than the total row. That is expected for many-to-many data. It means the numbers are non-additive, not wrong.
Direct column filters are replaced
A TREATAS filter on Transactions[AccountID] replaces any other filter applied directly to that column. For example, if a visual filters Transactions[AccountID] itself, that filter is discarded.
Filters that reach Transactions through a relationship are separate. If a slicer on Accounts filters Transactions through the Accounts relationship, that filter still applies, and the result is the intersection of the two.
To keep a direct filter on the target column and intersect with it, wrap TREATAS in KEEPFILTERS:
Amount for Selected Customers (Intersect) =
CALCULATE (
SUM ( Transactions[Amount] ),
KEEPFILTERS (
TREATAS ( VALUES ( AccountBridge[AccountID] ), Transactions[AccountID] )
)
)Another use: a disconnected slicer table
TREATAS is not limited to bridges. A common second use is a table that has no relationships at all, such as a slicer table that lists account IDs for the user to pick from. The measure applies the user's choice to the fact table:
Amount for Picked Accounts =
CALCULATE (
SUM ( Transactions[Amount] ),
TREATAS ( VALUES ( AccountPicker[AccountID] ), Transactions[AccountID] )
)The pattern is the same: read the visible values from one table, then apply them as a filter on another column. The sections below compare it with CROSSFILTER and cover its failure modes.
TREATAS and CROSSFILTER compared
Both approaches let a customer selection reach Transactions. They differ in how the model is built and where the work happens.
| Aspect | CROSSFILTER with a wired bridge | TREATAS with an unwired bridge |
|---|---|---|
| Relationships needed | Bridge related to both dimensions | Bridge related to one dimension only |
| Model complexity | More relationships, with a risk of ambiguity | Fewer relationships, so the model diagram stays simple |
| Where the connection lives | Model plus a per-measure direction override | Entirely inside each measure |
| Visible to report authors | Yes, as relationships in model view | No, only in the measure's code |
| Every measure needs it | Only measures that need both-way flow | Every measure that must reach the fact table |
The drawback of TREATAS is that every measure needing this behavior must repeat the pattern. One option is to define the pattern once in a base measure, such as Amount for Selected Customers, and build other measures on top of it with DIVIDE or time-intelligence functions. A model with many such measures can still turn into maintenance work.
Common errors
- The same number appears for every customer. The
TREATASfilter is missing, or the measure being used isTotal Amount. Confirm that the visual uses theTREATASmeasure. - Blank results everywhere.
VALUESreturned a list that does not match the target column, often because of different data types (text on one side, whole number on the other) or leading zeros. Check that the types match. - Wrong results with several columns.
TREATASmatches by position. A table expression that yieldsYear, Monthmust filterYear, Monthcolumns in that same order. - A slicer on the fact column is ignored. The
TREATASfilter replaced it. UseKEEPFILTERSif the filters should intersect. - Values that do not exist in the target column.
TREATASignores them without an error. A bridge with a stale or misspelledAccountIDloses those rows silently, so check for orphans.
Checking for double counting
Because shared accounts appear in several customer rows, check whether the rows exceed the real total. This measure uses the TREATAS result for each visible customer and compares the sum with the unfiltered grand total:
Rows Check TREATAS =
VAR GrandTotal = CALCULATE ( [Total Amount], ALL ( Customers ) )
VAR VisibleSum =
SUMX ( VALUES ( Customers[CustomerID] ), [Amount for Selected Customers] )
RETURN
VisibleSum - GrandTotalA result of zero means each account belongs to one customer, so nothing is counted twice. A positive result is the amount counted more than once through shared accounts. A negative result usually means some accounts have no customer in the bridge, so their transactions never appear for any customer.
When to prefer TREATAS
Choose TREATAS when you want few relationships and an explicit calculation, or when no stable relationship should exist, as with a disconnected slicer. Choose a wired bridge with CROSSFILTER or bidirectional filtering when many measures and visuals need the connection and report authors should be able to see it in the model. The choosing chapter returns to this decision with the native many-to-many option included.
Exercise
A matrix shows Customers[CustomerName] on rows. The measure Total Amount = SUM ( Transactions[Amount] ) shows the same number on every row. The model has Customers related one-to-many to AccountBridge, and no relationship between AccountBridge and Transactions. Write a measure that gives each customer only the amounts on their accounts, and say what the total row shows when two customers share an account.
Show answer
Use TREATAS to apply the bridge's visible AccountID values to Transactions[AccountID]: Amount for Selected Customers = CALCULATE ( SUM ( Transactions[Amount] ), TREATAS ( VALUES ( AccountBridge[AccountID] ), Transactions[AccountID] ) ) In a customer row, the Customers filter reaches the bridge, VALUES returns that customer's accounts, and TREATAS filters Transactions to them. In the total row, VALUES returns each account once, so a shared account's amount is counted once. Each of the two customers' rows still includes the full amount of the shared account, so the rows add up to more than the total row.