Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
PetyrBaelish
Resolver III
Resolver III

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

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)

View solution in original post

2 REPLIES 2
Anonymous
Not applicable

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

 

 

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)

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.