當你需要用 兩個欄位的組合 才能唯一對應另一個表的紀錄(也就是 複合鍵關聯),Power BI 的「模型關聯」介面本身不支援直接用多欄位建立關聯。但有幾種方法可以達成相同效果
方法一:建立「複合鍵欄位」再建立關聯(最常用)
原理:在兩個表都建立一個新的「組合欄位」,用 & 將多個欄位串起來,然後用這個新欄位作為關聯鍵。
步驟:
假設有兩個表:
- TableA:欄位
CustomerID,ProductID - TableB:欄位
CustomerID,ProductID
你想要根據 CustomerID + ProductID 建立關聯。
- 在 TableA 新增一個 計算欄位:
DAX
CompositeKey = TableA[CustomerID] & "-" & TableA[ProductID]2. 在 TableB 同樣新增一個:
DAX
CompositeKey = TableB[CustomerID] & "-" & TableB[ProductID]3. 在「模型檢視」中,用這兩個 CompositeKey 欄位建立關聯。
DAX
CompositeKey = FORMAT(TableA[CustomerID], "0000") & "-" & FORMAT(TableA[ProductID], "0000")小技巧:
如果欄位是數字型別,最好先轉成文字再串接:
方法二:建立中介表(Bridge Table)
當你有多對多或需要彈性關聯時,可以建立一個「橋接表」。
例如:
建立一個新表(可用 DAX 建立):
DAX
BridgeTable =
DISTINCT(
SELECTCOLUMNS(
UNION(
SELECTCOLUMNS(TableA, "CustomerID", TableA[CustomerID], "ProductID", TableA[ProductID]),
SELECTCOLUMNS(TableB, "CustomerID", TableB[CustomerID], "ProductID", TableB[ProductID])
),
"CustomerID", [CustomerID],
"ProductID", [ProductID]
)
)然後在模型中建立:
DAX
TableA[CustomerID, ProductID] → BridgeTable[CustomerID, ProductID] ← TableB[CustomerID, ProductID]實際上要拆成兩條一對多的關聯線
方法三:在 Power Query 中合併欄位
若不想用 DAX,可以在「Power Query 編輯器」中操作:
- 開啟 Power Query 編輯器。
- 選擇
TableA→ 「新增欄位」→「自訂欄位」。 - 輸入:
DAX
[CustomerID] & "-" & [ProductID]- 命名為
CompositeKey。 - 對
TableB做同樣的事。 - 回到模型後,用這個欄位建立關聯。
這方法效能通常比在 DAX 模型層建立計算欄位更好。
注意事項
- 確保組合後的欄位在主表(通常是「一」的那一端)是唯一的,否則關聯會出錯。
- 若關聯是多對多,可以用 中介表 + 雙向篩選 或 Many-to-Many 關聯(但需謹慎)。
- 若欄位可能有
NULL值,記得用COALESCE()或IF處理。




