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 last year value based on filter selection?

Hello,

Can anybody help me to get the last year's value based on the year selected on the filter? I am trying to create variance dynamically. Whenever a user selects a year it will reference by formula, (last year - current year )/ last year. But I am having a hard time getting the last year's value. It is coming blank. 
Current Year - 

 

 

Current_Year = 
    VAR CurrrentYear = CALCULATE([Line Amount (Formatted)], 
        FILTER('Calendar - Fiscal Date',
                'Calendar - Fiscal Date'[FiscalYr Year Number] = SELECTEDVALUE('Calendar - Fiscal Date'                         [FiscalYr Year Number])))
        return CurrrentYear 

 

 

Last Year - 

 

 

Last Year = VAR LastYear = CALCULATE([Line Amount (Formatted)], 
        FILTER('Calendar - Fiscal Date',
                'Calendar - Fiscal Date'[FiscalYr Year Number] = SELECTEDVALUE('Calendar - Fiscal Date'[FiscalYr Year Number]) -1))
        return LastYear

 

 

Variance- 

 

 

  VAR CurrrentYear = CALCULATE([Line Amount (Formatted)], 
        FILTER('Calendar - Fiscal Date',
                'Calendar - Fiscal Date'[FiscalYr Year Number] = SELECTEDVALUE('Calendar - Fiscal Date'                         [FiscalYr Year Number])))
    VAR LastYear = CALCULATE([Line Amount (Formatted)], 
        FILTER('Calendar - Fiscal Date',
                'Calendar - Fiscal Date'[FiscalYr Year Number] = SELECTEDVALUE('Calendar - Fiscal Date'[FiscalYr Year Number]) -1))
    return LastYear - CurrrentYear

 

 

My Data -  You can see below, last year is blank. Not sure why is that. 
Capture.PNG
Any help will be highly appreciated. 
Thank you 
 
1 ACCEPTED SOLUTION
v-alq-msft
Community Support
Community Support

Hi, @Anonymous 

 

I'd like to sugggest you modify the measure as below.

measure =
VAR CurrrentYear =
    CALCULATE (
        [Line Amount (Formatted)],
        FILTER (
            ALL ( 'Calendar - Fiscal Date' ),
            'Calendar - Fiscal Date'[FiscalYr Year Number]
                = SELECTEDVALUE ( 'Calendar - Fiscal Date'[FiscalYr Year Number] )
        )
    )
VAR LastYear =
    CALCULATE (
        [Line Amount (Formatted)],
        FILTER (
            ALL ( 'Calendar - Fiscal Date' ),
            'Calendar - Fiscal Date'[FiscalYr Year Number]
                = SELECTEDVALUE ( 'Calendar - Fiscal Date'[FiscalYr Year Number] ) - 1
        )
    )
RETURN
    LastYear - CurrrentYear

 

Best Regards

Allan

 

If this post helps,then consider Accepting it as the solution to help other members find it faster.

View solution in original post

5 REPLIES 5
v-alq-msft
Community Support
Community Support

Hi, @Anonymous 

 

I'd like to sugggest you modify the measure as below.

measure =
VAR CurrrentYear =
    CALCULATE (
        [Line Amount (Formatted)],
        FILTER (
            ALL ( 'Calendar - Fiscal Date' ),
            'Calendar - Fiscal Date'[FiscalYr Year Number]
                = SELECTEDVALUE ( 'Calendar - Fiscal Date'[FiscalYr Year Number] )
        )
    )
VAR LastYear =
    CALCULATE (
        [Line Amount (Formatted)],
        FILTER (
            ALL ( 'Calendar - Fiscal Date' ),
            'Calendar - Fiscal Date'[FiscalYr Year Number]
                = SELECTEDVALUE ( 'Calendar - Fiscal Date'[FiscalYr Year Number] ) - 1
        )
    )
RETURN
    LastYear - CurrrentYear

 

Best Regards

Allan

 

If this post helps,then consider Accepting it as the solution to help other members find it faster.

Anonymous
Not applicable

This works. Thank you so much for your help. 

Anonymous
Not applicable

Hi,

 

Your logic is correct and the function I think is correct as well. The only thing when I try to update is 

FILTER( All 'Calendar - Fiscal Date',

After I add "All", it's working fine for me. Thanks!
 

amitchandak
Super User
Super User

@Anonymous , Try to use time intelligence functions for that

example

YTD Sales = CALCULATE(SUM(Sales[Sales Amount]),DATESYTD('Date'[Date],"12/31")) // change end date based on FY
Last YTD Sales = CALCULATE(SUM(Sales[Sales Amount]),DATESYTD(dateadd('Date'[Date],-1,Year),"12/31"))
This year Sales = CALCULATE(SUM(Sales[Sales Amount]),DATESYTD(ENDOFYEAR('Date'[Date]),"12/31"))
Last year Sales = CALCULATE(SUM(Sales[Sales Amount]),DATESYTD(ENDOFYEAR(dateadd('Date'[Date],-1,Year)),"12/31"))
Last to last YTD Sales = CALCULATE(SUM(Sales[Sales Amount]),DATESYTD(dateadd('Date'[Date],-2,Year),"12/31"))
Year behind Sales = CALCULATE(SUM(Sales[Sales Amount]),dateadd('Date'[Date],-1,Year))

 

//Only year vs Year, not a level below

This Year = CALCULATE(sum('order'[Qty]),filter(ALL('Date'),'Date'[Year]=max('Date'[Year])))
Last Year = CALCULATE(sum('order'[Qty]),filter(ALL('Date'),'Date'[Year]=max('Date'[Year])-1))
rolling = CALCULATE(sum('order'[Qty]),filter(ALL('Date'),'Date'[Year]>=max('Date'[Year])-2 && 'Date'[Year]<=max('Date'[Year])) )

diff = [This Year]-[Last Year ]
diff % = divide([This Year]-[Last Year ],[Last Year ])

 

To get the best of the time intelligence function. Make sure you have a date calendar and it has been marked as the date in model view. Also, join it with the date column of your fact/s. Refer :radacad sqlbi My Video Series Appreciate your Kudos.

Anonymous
Not applicable

Thank you, but this is not what I want. I want when I select 2019 o filter, I want to get 2018 values. I am not sure how many of the functions give me that. 

Any idea how I can get the last year based on filter value? Hopefully, this makes sense. 

Thank you 

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.