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
Anonymous
Not applicable

How to get the relative date this year and a year ago

Hi everyone,

Hope you are all well.

I used the following two calculations to get my Date Numbered column:

 

MaxDate = CALCULATE(MAX('Table'[Date]),ALL('Table'[Date]))
 
DateNumbered = CALCULATE(DATEDIFF(SELECTEDVALUE('Table'[Date]),[MaxDate],DAY)+1)
 
It worked fine when I just had 2020 dates but now I have 2020 & 2019.
 
I need the table below to do 1-15 for 2019 and 2020 does anyone have any ideas?
 
Thanks in advance!
 
Lauren.

 

Untitled.png

1 ACCEPTED SOLUTION
DataZoe
Employee
Employee

@Anonymous I think this measure may give you the correct result (edited to change the switch year to SELECTEDVALUE() instead of MIN())

 

DateNumbered =
VAR _maxDate_2020 =
    CALCULATE (
        MAX ( 'Table'[Date] ),
        ALL ( 'Table'[Date] ),
        YEAR ( 'Table'[Date] ) = 2020
    )
VAR _maxDate_2019 =
    CALCULATE (
        MAX ( 'Table'[Date] ),
        ALL ( 'Table'[Date] ),
        YEAR ( 'Table'[Date] ) = 2019
    )
VAR _dayspast =
    SWITCH (
        YEAR ( SELECTEDVALUE ( 'Table'[Date] ) ),
        2020,
            CALCULATE (
                DATEDIFF ( SELECTEDVALUE ( 'Table'[Date] )_maxDate_2020DAY ) + 1
            ),
        2019,
            CALCULATE (
                DATEDIFF ( SELECTEDVALUE ( 'Table'[Date] )_maxDate_2019DAY ) + 1
            ),
        BLANK ()
    )
RETURN
    _dayspast

 

DataZoe_0-1601924767867.png

 

Respectfully,
Zoe Douglas (DataZoe)



Follow me on LinkedIn at https://www.linkedin.com/in/zoedouglas-data
See my reports and blog at https://www.datazoepowerbi.com/

View solution in original post

4 REPLIES 4
DataZoe
Employee
Employee

@Anonymous I think this measure may give you the correct result (edited to change the switch year to SELECTEDVALUE() instead of MIN())

 

DateNumbered =
VAR _maxDate_2020 =
    CALCULATE (
        MAX ( 'Table'[Date] ),
        ALL ( 'Table'[Date] ),
        YEAR ( 'Table'[Date] ) = 2020
    )
VAR _maxDate_2019 =
    CALCULATE (
        MAX ( 'Table'[Date] ),
        ALL ( 'Table'[Date] ),
        YEAR ( 'Table'[Date] ) = 2019
    )
VAR _dayspast =
    SWITCH (
        YEAR ( SELECTEDVALUE ( 'Table'[Date] ) ),
        2020,
            CALCULATE (
                DATEDIFF ( SELECTEDVALUE ( 'Table'[Date] )_maxDate_2020DAY ) + 1
            ),
        2019,
            CALCULATE (
                DATEDIFF ( SELECTEDVALUE ( 'Table'[Date] )_maxDate_2019DAY ) + 1
            ),
        BLANK ()
    )
RETURN
    _dayspast

 

DataZoe_0-1601924767867.png

 

Respectfully,
Zoe Douglas (DataZoe)



Follow me on LinkedIn at https://www.linkedin.com/in/zoedouglas-data
See my reports and blog at https://www.datazoepowerbi.com/

Anonymous
Not applicable

@DataZoe that has worked perfectly thank you!

amitchandak
Super User
Super User

@Anonymous , Not very clear, Try like


MaxDate =
var _max CALCULATE(MAX('Table'[Date]),ALL('Table'[Date]))
return
date(year(_max)-1,month(_max), day(_max))

DateNumbered = CALCULATE(DATEDIFF(SELECTEDVALUE('Table'[Date]),[MaxDate],DAY)+1)

Anonymous
Not applicable

Hello @amitchandak ,

Thank you for your quick response and your help.

I am now getting the following:

Untitled2.png

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.