Forum Discussion
Calculating the difference between dates in the same column
- 6 years ago
Hello Anonymous
We jsut need a couple date tables, one inactive relationship and the right measure.
Notice that the relationship between Dates 2 in the data is inactive. We will turn it on in our mesure when needed. We can get a simple date table with this DAX
Dates 1 = VAR DateRange = CALENDARAUTO() RETURN ADDCOLUMNS( DateRange, "Year",YEAR([Date]), "Month",FORMAT([Date],"mmmm"), "Year Month", FORMAT([Date],"yyyy-mmmm"), "YearMonthSort",YEAR([Date])*100 + MONTH([Date]), "ShortName",FORMAT([Date],"ddd"), "IsWeekDay", NOT WEEKDAY( [Date] ) IN {1,7} )Dates 2 is just this.
Dates 2 = 'Dates 1'
Amount 1 is straight sum using the first date relationship.
Amount 1 = SUM ( Data[Amount] )
Amount 2 is where we turn off the relationship with the first date table and turn on the link with the second one.
Amount 2 = CALCULATE( SUM ( Data[Amount] ), CROSSFILTER ( Data[Date], 'Dates 1'[Date], None), // Turns off the link to Dates 1 USERELATIONSHIP ( Data[Date], 'Dates 2'[Date] ) // Turns on the link to Dates 2 )Then the profit measure
Profit = [Amount 2] - [Amount 1]
And we get out desired result.
I have attached my sample workbook for you to look at.
Hello Anonymous
We jsut need a couple date tables, one inactive relationship and the right measure.
Notice that the relationship between Dates 2 in the data is inactive. We will turn it on in our mesure when needed. We can get a simple date table with this DAX
Dates 1 =
VAR DateRange = CALENDARAUTO()
RETURN
ADDCOLUMNS(
DateRange,
"Year",YEAR([Date]),
"Month",FORMAT([Date],"mmmm"),
"Year Month", FORMAT([Date],"yyyy-mmmm"),
"YearMonthSort",YEAR([Date])*100 + MONTH([Date]),
"ShortName",FORMAT([Date],"ddd"),
"IsWeekDay", NOT WEEKDAY( [Date] ) IN {1,7}
)
Dates 2 is just this.
Dates 2 = 'Dates 1'
Amount 1 is straight sum using the first date relationship.
Amount 1 = SUM ( Data[Amount] )
Amount 2 is where we turn off the relationship with the first date table and turn on the link with the second one.
Amount 2 =
CALCULATE(
SUM ( Data[Amount] ),
CROSSFILTER ( Data[Date], 'Dates 1'[Date], None), // Turns off the link to Dates 1
USERELATIONSHIP ( Data[Date], 'Dates 2'[Date] ) // Turns on the link to Dates 2
)
Then the profit measure
Profit = [Amount 2] - [Amount 1]
And we get out desired result.
I have attached my sample workbook for you to look at.
Thanks a lot jdbuchanan71 Anonymous both the Methods are working.