Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
AnthonyJoseph
Resolver III
Resolver III

Calculate previous year value based on slicer selection

Hello Community,

 

Need your help in implementing a functionality. I have a table as given below:

AnthonyJoseph_1-1650911045891.png

 


The task here is to display the sum (based on country and city) of previous year of Sales, Profit and difference as different columns along with the current year values as given below.

Expected result:

AnthonyJoseph_0-1650911014805.png

 

Please can anyone help me here.


Thanks

2 ACCEPTED SOLUTIONS

Then you refer to it as 'Table' not Table becuase it is a resereved word in DAX

View solution in original post

Ah! Thanks @tamerj1 Works perfectly now 🙂

View solution in original post

8 REPLIES 8
tamerj1
Super User
Super User

Hi @AnthonyJoseph 

you may try creating measures similar to 

Prior Year Sales =
VAR CurrentYear =
    MAX ( Table[Year] )
VAR CountryCityTable =
    CALCULATETABLE ( Table, ALLEXCEPT ( Table, Table[Country], Table[City] ) )
VAR PriorYear =
    MAXX ( FILTER ( CountryCityTable, Table[Year] < CurrentYear ), Table[Year] )
VAR PeriorYearSales =
    MAXX ( FILTER ( CountryCityTable, Table[Year] = PriorYear ), Table[Sales] )
RETURN
    PeriorYearSales

Thanks @tamerj1 

I tried to use the dax but looks like something is incorrect in the measure. Please refer the screenshot below:

AnthonyJoseph_0-1650978237803.png

Please can you help me.
Thanks again

Please use the actual name of you table instead of "Table"

Hi @tamerj1 

 

Yes, the name of the table that I m using is "Table" only but still I'm facing the same error. If possible can you share the pbix?

 

Thanks,

Joseph

Then you refer to it as 'Table' not Table becuase it is a resereved word in DAX

Hi @tamerj1 

Sorry, if my example was misguiding. I think in the solution you have provided we are returning the maximum values of the previous year but Ideally we need to sum the prior year sales value for example:

 

Input: 

 

YearCountryCitySalesProfit
2020IndiaDelhi10050
2021IndiaMumbai20090
2021IndiaDelhi3300300
2020IndiaDelhi200050

 

Expected output:

 

YearCountryCitySalesProfitDifferencePrior year SalesPrior year ProfitPrior year Difference
2020IndiaDelhi10050-50000
2021IndiaMumbai20090-110000
2021IndiaDelhi3300300-30002100100-2000
2020IndiaDelhi200050-1950000

 

@AnthonyJoseph 

If I correctly understand then you just need to replace MAXX with SUMX 

Prior Year Sales =
VAR CurrentYear =
    MAX ( 'Table'[Year] )
VAR CountryCityTable =
    CALCULATETABLE ( 'Table', ALLEXCEPT ( Table, 'Table'[Country], 'Table'[City] ) )
VAR PriorYear =
    MAXX ( FILTER ( CountryCityTable, 'Table'[Year] < CurrentYear ), 'Table'[Year] )
VAR PeriorYearSales =
    SUMX ( FILTER ( CountryCityTable, 'Table'[Year] = PriorYear ), 'Table'[Sales] )
RETURN
    PeriorYearSales

Ah! Thanks @tamerj1 Works perfectly now 🙂

Helpful resources

Announcements
July 2025 community update carousel

Fabric Community Update - July 2025

Find out what's new and trending in the Fabric community.

July PBI25 Carousel

Power BI Monthly Update - July 2025

Check out the July 2025 Power BI update to learn about new features.