Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
6 years ago
Solved

Calculating the difference between dates in the same column

Hi All, I am new to power BI. To explain my request is very simple (Dataset:financial data set from microsoft) Create 2 date slicer based on the same "Date" column. Display the profits based on ...
  • jdbuchanan71's avatar
    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.