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

Calling all Data Engineers! Fabric Data Engineer (Exam DP-700) live sessions are back! Starting October 16th. Sign up.

Reply
angsoka
Frequent Visitor

Getting the Last Date Data of Previous Year

I want to create two index which are the ratios of :
1. the price and the base price (the price of 1 December 2018) and
2. the price and the last year price (the price of December of last year) 

However, using DAX, I could not get these two columns (Base Price and Last Year Price). I have tried DATEADD, SAMEPERIODLASTYEAR, and didn't get the correct answers. 

DatePriceBase PriceLast Year Price
12/1/2018100.00  
1/1/2019102.23100.00100.00
2/1/2019

103.01

100.00100.00
............
12/1/2019108.99100.00100.00
1/1/2020110.00100.00108.99
2/1/2020

112.22

100.00108.99
............
12/1/2020115.53100.00108.99


Thanks for all your help

1 ACCEPTED SOLUTION

@angsoka 

Sorry I'm not not on my computer right now. I don't remember if the product name is in the same table or not but I will assume it is. I think you want to have this calculation applied separately on each product. In this cas I would suggest to replace ALLSELECTED ( Data ) with ALLEXCEPT ( Data, Data[Product Name] ) this way the logic will be applied to each product separately. 

View solution in original post

4 REPLIES 4
tamerj1
Super User
Super User

Hi @angsoka 
Here is a sample file with the solution https://www.dropbox.com/t/FmGZyCUtYuzgWT9E

1.png2.png

Base Price = 
VAR AllSelectedData = ALLSELECTED ( Data )
VAR FirstYear = YEAR ( MINX ( AllSelectedData, Data[Date] ) )
VAR FirstYearTable = FILTER ( AllSelectedData, YEAR ( Data[Date] ) = FirstYear )
VAR FirstYearLastDate = MAXX ( FirstYearTable, Data[Date] )
VAR FirstYearLastDateTable = FILTER ( FirstYearTable, Data[Date] = FirstYearLastDate )
VAR Result = SUMX ( FirstYearLastDateTable, Data[Price] )
RETURN
    Result
Last Year Price = 
VAR CurrentYear = YEAR ( MAX ( Data[Date] ) )
VAR PreviousYearLastDate = 
    CALCULATE ( MAX ( Data[Date] ), YEAR ( Data[Date] ) = CurrentYear - 1, ALLSELECTED ( Data ) )
RETURN
    CALCULATE ( MAX ( Data[Price] ), Data[Date] = PreviousYearLastDate, ALLSELECTED ( Data ))
angsoka
Frequent Visitor

Dear @tamerj1 

Thank you so much. It works like a charm. However, when I filtered the price with the product's name. It doesn't give the correct result. I think because of this ALLSELECTED(), which clears the filter. 

ALLSELECTED ( Data ) 

Could you please advise the right filter so that it will give a different December price for a different product?

 

@angsoka 

Sorry I'm not not on my computer right now. I don't remember if the product name is in the same table or not but I will assume it is. I think you want to have this calculation applied separately on each product. In this cas I would suggest to replace ALLSELECTED ( Data ) with ALLEXCEPT ( Data, Data[Product Name] ) this way the logic will be applied to each product separately. 

CNENFRNL
Community Champion
Community Champion

CNENFRNL_0-1659195465261.png


Thanks to the great efforts by MS engineers to simplify syntax of DAX! Most beginners are SUCCESSFULLY MISLED to think that they could easily master DAX; but it turns out that the intricacy of the most frequently used RANKX() is still way beyond their comprehension!

DAX is simple, but NOT EASY!

Helpful resources

Announcements
November Fabric Update Carousel

Fabric Monthly Update - November 2025

Check out the November 2025 Fabric update to learn about new features.

Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Users online (27)