---
title: USERELATIONSHIP – เลือกใช้ relationship ที่ไม่ใช่ active
url: https://www.thepexcel.com/functions/dax/relationship/userelationship-dax/
type: function-explainer
program: DAX
syntax: "USERELATIONSHIP(<columnName1>, <columnName2>)"
date: 2025-12-13
updated: 2025-12-21
scores:
  popularity: 7
  difficulty: 5
  usefulness: 7
---

# USERELATIONSHIP – เลือกใช้ relationship ที่ไม่ใช่ active

> USERELATIONSHIP ช่วยให้คุณใช้ relationship ที่ไม่ใช่ active หรือเปลี่ยน relationship ที่ใช้ในการคำนว

## คำอธิบาย

USERELATIONSHIP ช่วยให้คุณใช้ relationship ที่ไม่ใช่ active หรือเปลี่ยน relationship ที่ใช้ในการคำนวณ เหมาะสำหรับสถานการณ์เช่น sales data ที่มีหลาย date columns (OrderDate, ShippingDate, DeliveryDate)

## Syntax

```excel
USERELATIONSHIP(&lt;columnName1&gt;, &lt;columnName2&gt;)
```

## Arguments

| Name | Required | Type | Default | Description |
| --- | --- | --- | --- | --- |
| columnName1 | Yes | Column |  | คอลัมน์ที่เป็น foreign key (หลายรายการ) ของ relationship ที่ต้องการใช้ ต้องเป็นคอลัมน์จริง ไม่ใช่ expression |
| columnName2 | Yes | Column |  | คอลัมน์ที่เป็น primary key (หนึ่งรายการ) ของ relationship ที่ต้องการใช้ ต้องเป็นคอลัมน์จริง ไม่ใช่ expression |

## เคสการใช้งาน

### วิเคราะห์ยอดขายตามวันที่จัดส่ง (Delivery Date)

ถ้ามีวันที่ OrderDate เป็น Active Relationship แต่ต้องการดูยอดขายตาม DeliveryDate ชั่วคราว

_เหมาะกับ:_ alternate-date-dimension

### เปรียบเทียบยอดขายหลายช่วงเวลา

ใช้ Date Table เดียวกันแต่มีหลายความสัมพันธ์กับ Fact Table เพื่อดูยอดขายตามวันที่สั่งซื้อ, วันที่จัดส่ง, วันที่ชำระเงิน พร้อมๆ กัน

_เหมาะกับ:_ multiple-relationships

## ตัวอย่าง

### 1. เปลี่ยนจาก OrderDate เป็น ShippingDate

```excel
Shipped Amount = 
CALCULATE(
    SUM(Sales[Amount]),
    USERELATIONSHIP(Sales[ShippingDate], 'Date'[Date])
)
```

**ผลลัพธ์:** `ยอดขายตามวันที่ส่ง แทนวันที่สั่งซื้อ`

ใน Sales table มี OrderDate (active) และ ShippingDate (inactive) ทั้งคู่เชื่อมต่อ Date table ด้วย USERELATIONSHIP เราอ้างถึง ShippingDate แทน OrderDate ที่ default

### 2. ใช้กับหลาย relationship ในสูตรเดียว

```excel
Order vs Ship Variance =
VAR OrderAmount = 
    CALCULATE(SUM(Sales[Amount]), 
        USERELATIONSHIP(Sales[OrderDate], 'Date'[Date]))
VAR ShipAmount = 
    CALCULATE(SUM(Sales[Amount]), 
        USERELATIONSHIP(Sales[ShippingDate], 'Date'[Date]))
RETURN
    OrderAmount - ShipAmount
```

**ผลลัพธ์:** `ความแตกต่างระหว่างยอดขายที่สั่งกับที่ส่งจริง`

ใช้ VAR เก็บค่าอันดับแรกจาก OrderDate แล้วค่าอันดับสอง ShippingDate แล้วหาผลต่าง ทำให้เห็นว่าในช่วงเวลานี้มีออร์เดอร์เท่าไหร่แต่ส่งมากขนาดไหน

### 3. กับ All() เพื่อ compare across date

```excel
Current Month Orders = 
CALCULATE(
    SUM(Sales[Amount]),
    USERELATIONSHIP(Sales[OrderDate], 'Date'[Date]),
    'Date'[MonthYear] = MAX('Date'[MonthYear])
)

All Time Orders =
CALCULATE(
    SUM(Sales[Amount]),
    USERELATIONSHIP(Sales[OrderDate], 'Date'[Date]),
    ALL('Date')
)
```

**ผลลัพธ์:** `สามารถเปรียบเทียบเดือนปัจจุบันกับสะสมทั้งหมด`

USERELATIONSHIP ทำงานได้ดีเมื่อรวมกับ ALL() เป็นการเลือก date relationship และแล้วลบ filter date ทั้งหมดออก

### 4. สำหรับการแมป Role-Playing Dimensions

```excel
Delivery Time = 
VAR DeliveryAmount = 
    CALCULATE(SUM(Sales[Amount]), 
        USERELATIONSHIP(Sales[DeliveryDate], 'Date'[Date]))
VAR OrderAmount = 
    CALCULATE(SUM(Sales[Amount]), 
        USERELATIONSHIP(Sales[OrderDate], 'Date'[Date]))
RETURN
    IF(OrderAmount > 0, 
        DIVIDE(DeliveryAmount, OrderAmount, 0), 
        BLANK())
```

**ผลลัพธ์:** `เปอร์เซ็นต์ของ delivery ต่อ order ในวันเดียวกัน`

เมื่อ Date table แสดงหลายบทบาท (order date, ship date, delivery date) ใช้ USERELATIONSHIP สำหรับแต่ละวาระ DAX จะ activate relationship ที่ต้องการเท่านั้น

## หมายเหตุเพิ่มเติม

- USERELATIONSHIP ช่วยให้ Data Model ยืดหยุ่น ไม่ต้องเปลี่ยน active relationship ทั้งหนึ่ง

- ใช้ VAR เก็บค่าจาก USERELATIONSHIP ต่างๆ เพื่อเปรียบเทียบหรือคำนวณผลต่าง

- USERELATIONSHIP ทำงานกับทั้ง active และ inactive relationship

- ตรวจสอบการแมป (mapping) ระหว่าง Foreign Key และ Primary Key ให้ถูกต้อง

## คำถามที่พบบ่อย

**Q: USERELATIONSHIP ใช้ได้กับ inactive relationship หรือ active relationship เท่านั้น?**

ใช้ได้ทั้งคู่ ไม่ว่า relationship จะ active หรือ inactive ก็ตาม เพราะ USERELATIONSHIP ระบุ relationship โดยอ้างอิงคอลัมน์ของมันเองตามคำจำกัดความของ relationship

**Q: ใช้ USERELATIONSHIP ได้กับ function อะไรบ้าง?**

ใช้ได้เฉพาะใน CALCULATE, CALCULATETABLE, และ time-intelligence functions เช่น TOTALMTD, TOTALYTD เนื่องจากฟังก์ชันเหล่านี้ยอมรับ filter predicates

**Q: ถ้าใช้ USERELATIONSHIP หลายตัวในสูตรเดียว ตัวไหนจะ override?**

USERELATIONSHIP ที่อยู่ใน CALCULATE ชั้นในสุด (innermost) จะมีลำดับความสำคัญมากกว่า ถ้าปะทะกัน USERELATIONSHIP ตัวชั้นในจะชนะ

**Q: ใช้ USERELATIONSHIP ได้กับ 1-to-1 relationship หรือไม่?**

ใช้ได้ แต่ 1-to-1 relationship ระหว่างตาราง A และ B จะ activate เฉพาะทิศทางเดียว ถ้าต้องการทั้งสองทิศทาง ต้องใช้ USERELATIONSHIP สองครั้ง

**Q: มี limitation ในการใช้ USERELATIONSHIP หรือไม่?**

ใช่ ห้ามใช้กับ Row-Level Security (RLS) ที่กำหนดบน table ที่มี relationship ต้องการใช้ และสูงสุด 10 nested USERELATIONSHIP functions ต่อสูตร

## ฟังก์ชันที่เกี่ยวข้อง

- [CALCULATE – ฟังก์ชันหลักของ DAX ที่ควบคุม Filter Context](https://www.thepexcel.com/functions/dax/filter/calculate-dax/)
- [CALCULATETABLE – Evaluate Table Expression ใน Modified Filter Context](https://www.thepexcel.com/functions/dax/filter/calculatetable-dax/)
- [FILTER – กรองตารางด้วยเงื่อนไขที่ซับซ้อน (Iterator Function)](https://www.thepexcel.com/functions/dax/filter/filter-dax/)
- [ALL – ลบ Filter หรือคืนค่าทุกแถว](https://www.thepexcel.com/functions/dax/filter/all-dax/)
- [RELATED – ดึงค่าจากตารางที่มีความสัมพันธ์ (Many → One)](https://www.thepexcel.com/functions/dax/relationship/related-dax/)

## แหล่งข้อมูลเพิ่มเติม

- [Microsoft Learn: USERELATIONSHIP](https://learn.microsoft.com/en-us/dax/userelationship-function-dax) _(official)_
- [DAX Guide: USERELATIONSHIP](https://dax.guide/userelationship/) _(guide)_
- [SQLBI: Role-Playing Dimensions](https://www.sqlbi.com/articles/role-playing-dimensions-in-power-bi/) _(guide)_

---

_Source: [https://www.thepexcel.com/functions/dax/relationship/userelationship-dax/](https://www.thepexcel.com/functions/dax/relationship/userelationship-dax/)_
