cancel
Showing results for
Did you mean:
Highlighted
Member

## Month of today or last month specified in data

I have a table of data which tracks various cars and their costs over thier lifespan, which looks something like this:

Car - - - - - - - - - - - Date - - - - - - - - Category -- - Amount

Ford Mondeo --- 01/01/2016 --- Purchase - - - £5000

Ford Mondeo - -  08/08/2018 - - Repairs - - - - £300

Ford Mondeo - -  09/09/2018 - - Sale - - - - - -  £3000

Toyata Yaris - - - 10/09/2018 - - Purchase - - - £4000

Toyata Yaris - - - 10/12/2018 - - Repairs - - - - £400

I am trying to work out the monthly cost of each car, and have created the following measure for months owned:

`Months Owned = DATEDIFF(FIRSTDATE(Cars[Date]),LASTDATE(Cars[Date]),MONTH)`
This is accurate for the Ford Mondeo, because this car has been sold - which is defined as their being an entry for the Ford Mondeo with the Category "Sale". The Toyota Yaris however has not been sold but the above formula detects December 2018 as the "LastDate" (I would want the current date in this instance).

I want the latter part of the formula to work out: if the respective car has been sold, use the lastdate function, if not use today's month. But I don't know how to do this - to find out if the car has been sold or not.

I'm expecting the result to look something like:
`IF( car has been sold??  ,MONTH(TODAY()),LASTDATE(Cars[Date]))`
Thanks.
1 ACCEPTED SOLUTION

Accepted Solutions
Member

## Re: Month of today or last month specified in data

I already have a dates table but I didn't pesonally see the benefit of linking that to the measure I'm trying to create.

Since my post I have managed to create the measure and it seems to be working:

`Months Owned = DATEDIFF(FIRSTDATE(Cars[Date]),IF(FILTER(VALUES(Cars[Category]),Cars[Category] = "Sale")="Sale",LASTDATE(Cars[Date]),TODAY()),MONTH)`
2 REPLIES 2
Super User

## Re: Month of today or last month specified in data

First step is to get a dedicated calendar table and use the columns from that table in your measures,

https://docs.microsoft.com/en-us/dax/date-and-time-functions-dax

Member

## Re: Month of today or last month specified in data

I already have a dates table but I didn't pesonally see the benefit of linking that to the measure I'm trying to create.

Since my post I have managed to create the measure and it seems to be working:

`Months Owned = DATEDIFF(FIRSTDATE(Cars[Date]),IF(FILTER(VALUES(Cars[Category]),Cars[Category] = "Sale")="Sale",LASTDATE(Cars[Date]),TODAY()),MONTH)`

Announcements

#### Challenge: Can You Solve These?

Find out how to participate in the first Power BI 'Can You Solve These?' challenge.

#### Community News & Announcements

Get your latest community news and announcements.

#### Microsoft Business Applications October Virtual Launch Event

Join us for an in-depth look at the new innovations across Dynamics 365 and the Microsoft Power Platform.

#### Win Power BI Swag with Community Kudopalooza!

Each week, complete activities and be qualified in the drawing for cool Power BI Swag.

Top Kudoed Authors
Users Online
Currently online: 149 members 1,561 guests
Recent signins: