# Get “Year To Date” and “Last Year To Date” in DAX

To get “Year To Date” and “Last year to date” in DAX is really simple. You can use the function TotalYTD and mix this with DATESBETWEEN or SAMEPERIODLASTYEAR.

This is an example with DATESBETWEEN

```DEFINE
MEASURE 'Internet Sales'[Previous Year To Date] =
CALCULATE (
TOTALYTD (
'Internet Sales'[Internet Total Sales],
DATESBETWEEN (
'Date'[Date],
FIRSTDATE ( DATEADD ( 'Date'[Date], -12, MONTH ) ),
LASTDATE ( DATEADD ( 'Date'[Date], -12, MONTH ) )
)
)
)
MEASURE 'Internet Sales'[Year To Date] =
CALCULATE (
TOTALYTD (
'Internet Sales'[Internet Total Sales],
'Date'[Date]
)
)
MEASURE 'Internet Sales'[Change] =
FORMAT (
'Internet Sales'[Year To Date]
/ 'Internet Sales'[Previous Year To Date],
"Percent"
)
EVALUATE
FILTER (
SUMMARIZE (
'Date',
'Date'[Date],
"Previous Year To Date", 'Internet Sales'[Previous Year To Date],
"Sales Year To Date", 'Internet Sales'[Year To Date],
"Change", 'Internet Sales'[Change]
),
'Date'[Date] = DATE ( 2007, 01, 15 )
)
```

And this is an example with SAMEPERIODLASTYEAR

```DEFINE
MEASURE 'Internet Sales'[Previous Year To Date] =
CALCULATE (
TOTALYTD (
'Internet Sales'[Internet Total Sales],
SAMEPERIODLASTYEAR ( 'Date'[Date] )
)
)
MEASURE 'Internet Sales'[Year To Date] =
CALCULATE (
TOTALYTD (
'Internet Sales'[Internet Total Sales],
'Date'[Date]
)
)
MEASURE 'Internet Sales'[Change] =
FORMAT (
'Internet Sales'[Year To Date]
/ 'Internet Sales'[Previous Year To Date],
"Percent"
)
EVALUATE
FILTER (
SUMMARIZE (
'Date',
'Date'[Date],
"Previous Year To Date", 'Internet Sales'[Previous Year To Date],
"Sales Year To Date", 'Internet Sales'[Year To Date],
"Change", 'Internet Sales'[Change]
),
'Date'[Date] = DATE ( 2007, 01, 15 )
)```

# Define a measure. Make your code more readable

To make your code more readable you can create measures outside the evaluate statement. Let’s see an example on how we can do this.

Take a look at the query that returns sales per year versus sales last year.

```EVALUATE
FILTER (
SUMMARIZE (
'Date',
'Date'[Calendar Year],
"Sales this year", SUM ( 'Internet Sales'[Sales Amount] ),
"Sales last year", CALCULATE(SUM ( 'Internet Sales'[Sales Amount] ),
SAMEPERIODLASTYEAR ( 'Date'[Date] ))
),
[Sales last year] <> 0
)
```

To move the calculation for “Sales last year” you can define a measure outside the evaluate statement like below. This can be very useful if you have lots of logic inside your query.

```DEFINE
MEASURE 'Internet Sales'[Sales last year] =
CALCULATE ( SUM ( 'Internet Sales'[Sales Amount] ), SAMEPERIODLASTYEAR ( 'Date'[Date] ))

EVALUATE
FILTER (
SUMMARIZE (
'Date',
'Date'[Calendar Year],
"Sales this year", SUM ( 'Internet Sales'[Sales Amount] ),
"Sales last year", 'Internet Sales'[Sales last year]
),
[Sales last year] <> 0
)```

If you want more than one measure you just add new measures below the first one. But the “DEFINE” should just written once at the top.