---
# System prepended metadata

title: Introduction to DAX - Part 5 - Bonus Contents
tags: [dax-foundation-course]

---

---
title: Introduction to DAX - Part 5 - Bonus Contents
tags: dax-foundation-course
---
# Bonus Contents

### DAX variables

Variables offer several benefits:

- Improving the readability and maintenance of your formulas.
- Improving performance because variables are evaluated once and only when or if they're needed.
- Allowing (at design time) straightforward testing of a complex formula by returning the variable of interest.

![](https://hackmd.io/_uploads/HJJ9HdmNY.png)

### Use relationship effectively

:question: How to manipulate relationship in data model using DAX?

**Practice file**: Resellers Sales Model of AdventureWork.

In this example, you have to open Power Query Editor and choose 2 additional columns in Sales table:

    - DueDate
    - ShipDate
    
In the Model view, create relationships between Sales table and Date table:

    - Sales | DueDate to Date | Date
    - Sales | ShipDate to Date | Date

![](https://hackmd.io/_uploads/HkNgwqdVY.png)

**Task 1**: Calculate total sales amount that shipped, and total sales amount that due.

```python=
Sales (Shipped) = 
CALCULATE(
    SUM(Sales[Sales]),
    USERELATIONSHIP('Date'[Date], Sales[ShipDate]))
```

```python=
Sales (Due) = 
CALCULATE(
    SUM(Sales[Sales]),
    USERELATIONSHIP('Date'[Date], Sales[DueDate]))
```

![](https://hackmd.io/_uploads/Sk1EDqO4t.png)

**Task 2**: Fix Sales Shipped measure

```python=
Sales Shipped = 
CALCULATE(
    SUM(Sales[Sales]),
    USERELATIONSHIP('Date'[Date], Sales[ShipDate]),
    FILTER(
        'Date',
        'Date'[Year] <> BLANK())
    )
```

![](https://hackmd.io/_uploads/SyjLDcOEY.png)