Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
4 years ago
Solved

Create date difference value column ?

Hello,    I want to create a column like created column( current date - last date by country) in power bi.  I have country/Region, date and death column.    How do I create created column?    ...
  • VahidDM's avatar
    4 years ago

    Hi Anonymous 

     

    Try this code to add a new column : 

    Column = 
    VAR _CD = 'Table'[Date]
    VAR _YD =
        CALCULATE (
            MAX ( 'Table'[Date] ),
            FILTER (
                ALL ( 'Table' ),
                'Table'[Date] < _CD
                    && 'Table'[Country/Region] = EARLIER ( 'Table'[Country/Region] )
            )
        )
    VAR _YDV =
        CALCULATE (
            MAX ( 'Table'[Deaths] ),
            FILTER (
                ALL ( 'Table' ),
                'Table'[Date] = _YD
                    && 'Table'[Country/Region] = EARLIER ( 'Table'[Country/Region] )
            )
        )
    RETURN
        IF ( ISBLANK ( _YD ), 0, 'Table'[Deaths] - _YDV )

     

    output:

     

     

     

    If this post helps, please consider accepting it as the solution to help the other members find it more quickly.
    Appreciate your Kudos!!
    LinkedIn: 
    www.linkedin.com/in/vahid-dm/