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

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Request now

Reply
Flpowerbi01
Frequent Visitor

Sales of Closest Date Last Year

Hi all, 

 

I am trying to get the closest sales last year to a date in a row. Please see below of what I want to achieve,

Here are my columns 

 

Date - On sale Date of a product

Sales - Sales of the product on the on sale date 

Date minus 365 - Date is subtracted by a year

 

Sales of Closest Date Last Year - This is the column I am trying to get. How do I get the sales of the date that is closest to the "Date minus 365 column". I manually filled out some of the values of the colmun. I cannot use a max or min function for cases when the difference between the two dates is less than 365. 

 

 

date         Sales   Date minus 365   Closest Date Last Year Sales

11/11/22  9624  11/11/21   
18/11/22815118/11/21 
25/11/22711025/11/21 
02/12/22828002/12/21 
09/12/22520609/12/21 
23/12/22660323/12/21 
06/01/23778606/01/22 
13/01/23951913/01/22 
20/01/23852520/01/22 
27/01/23668027/01/22 
03/02/23502003/02/22 
10/02/23650710/02/22 
17/02/23536517/02/22 
24/02/23834724/02/22 
03/03/23507003/03/22 
10/03/23737710/03/22 
17/03/23651917/03/22 
24/03/23820624/03/22 
31/03/23789331/03/22 
07/04/23845907/04/22 
14/04/23583214/04/22 
21/04/23788121/04/22 
28/04/23645328/04/22 
05/05/23615805/05/22 
12/05/23791612/05/22 
19/05/23921419/05/22 
26/05/23991226/05/22 
02/06/23952202/06/22 
09/06/23963709/06/22 
16/06/23581716/06/22 
23/06/23967123/06/22 
30/06/23748030/06/22 
07/07/23936607/07/22 
14/07/23816914/07/22 
21/07/23895221/07/22 
28/07/23639028/07/22 
04/08/23566904/08/22 
11/08/23640811/08/22 
18/08/23537318/08/22 
25/08/23667825/08/22 
01/09/23905501/09/22 
08/09/23618908/09/22 
15/09/23956915/09/22 
22/09/23525622/09/22 
29/09/23981229/09/22 
06/10/23872706/10/22 
13/10/23844613/10/22 
20/10/23893620/10/22 
27/10/23539627/10/22 
03/11/23903203/11/22 
10/11/23963610/11/22 
17/11/23764717/11/22 
24/11/23775824/11/22 
01/12/23965401/12/22 
08/12/23950408/12/229624
22/12/23990722/12/227110
05/01/24791905/01/237786
12/01/24850612/01/239519
19/01/24504219/01/238525
26/01/24819526/01/236680
02/02/24690302/02/235020
1 ACCEPTED SOLUTION

Hi @talespin , 

 

thank you so much for this. If there was also a product column in the table. Where will I put an additional filter for the product colum in your Dax code? 

 

SInce what is happeneing now is that it is getting the closest date even if it is not that specific product? Does that make sense? Or I can give you another example with the product

View solution in original post

5 REPLIES 5
Flpowerbi01
Frequent Visitor

Thank you so much Talespin! 

You're welcome.

talespin
Solution Sage
Solution Sage

hi @Flpowerbi01 

 

Closest Sales before of after Date minus 365?

 

If Yes, this is a Calculated Column

Closest Sales =
VAR _CurrMinus365Dt = TestTbl6[Date minus 365]
VAR _Cross = CROSSJOIN( ALL(TestTbl6[date], TestTbl6[Sales]), FILTER(ALL(TestTbl6[Date minus 365]), [Date minus 365] = _CurrMinus365Dt))
VAR _Tbl = ADDCOLUMNS(_Cross, "@Difference", ABS(INT([date]-[Date minus 365])))
VAR _MinDiff = MINX( _Tbl, [@Difference])
RETURN  SELECTCOLUMNS( FILTER( _Tbl, [@Difference] = _MinDiff), "NewCol", [Sales])
 

talespin_1-1707393328299.png

 

 

Hi @talespin , 

 

thank you so much for this. If there was also a product column in the table. Where will I put an additional filter for the product colum in your Dax code? 

 

SInce what is happeneing now is that it is getting the closest date even if it is not that specific product? Does that make sense? Or I can give you another example with the product

 
Please test it.
Added Product to first table in crossjoin.
 
Closest Sales =
VAR _CurrMinus365Dt = TestTbl6[Date minus 365]
VAR _Product = TestTbl6[Product]
VAR _Cross = CROSSJOIN( FILTER( ALL(TestTbl6[date], TestTbl6[Sales], TestTbl6[Product]), [Product] = _Product ), FILTER(ALL(TestTbl6[Date minus 365]), [Date minus 365] = _CurrMinus365Dt))
VAR _Tbl = ADDCOLUMNS(_Cross, "@Difference", ABS(INT([date]-[Date minus 365])))
VAR _MinDiff = MINX( _Tbl, [@Difference])
RETURN  SELECTCOLUMNS( FILTER( _Tbl, [@Difference] = _MinDiff), "NewCol", [Sales])

Helpful resources

Announcements
November Power BI Update Carousel

Power BI Monthly Update - November 2025

Check out the November 2025 Power BI 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.