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

To celebrate FabCon Vienna, we are offering 50% off select exams. Ends October 3rd. Request your discount now.

Reply
Anonymous
Not applicable

SamePeriodLastYear block on last day of current year.

Hello,

 

I'm trying to create a report with sales this year compare to the same period LY but I would like to block it at the same day.

As example below I would like that to compare 2020 and 2019 in August and to be comparable, 2019 need to be stop at the last day of 2020.

 

My formula is

Net Sales LY = 
    CALCULATE(sum('B&M_Database_All'[Net Sales]),
    SAMEPERIODLASTYEAR('Dim Date'[Date]))

How I can do that ?

Annotation 2020-08-14 111006.png

Thanks in advance for your help.

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Net Sales LY =
var __latestDateAvailable =
    CALCULATE(
        MAX( 'B&M_Database_All'[Date] ),
        ALL( 'B&M_Database_All' )
    )
RETURN
CALCULATE(
    [Total Sales],
    SAMEPERIODLASTYEAR(
        FILTER(
            VALUES( 'Dim Date'[Date] ),
            'Dim Date'[Date] <= __latestDateAvailable
        )
    )
)

[Total Sales] should be the very simple measure that just returns the total sales. Something like SUM( FactTable[Sales Amount] ).

View solution in original post

6 REPLIES 6
Anonymous
Not applicable

Net Sales LY =
var __latestDateAvailable =
    CALCULATE(
        MAX( 'B&M_Database_All'[Date] ),
        ALL( 'B&M_Database_All' )
    )
RETURN
CALCULATE(
    [Total Sales],
    SAMEPERIODLASTYEAR(
        FILTER(
            VALUES( 'Dim Date'[Date] ),
            'Dim Date'[Date] <= __latestDateAvailable
        )
    )
)

[Total Sales] should be the very simple measure that just returns the total sales. Something like SUM( FactTable[Sales Amount] ).

Anonymous
Not applicable

Thanks, this one is ok and @amitchandak if I add to your formula the code as below, it's ok.

 

var _max1 = calculate(maxx('B&M_Database_All',[Date - Billing]),All('B&M_Database_All'))

 

Thank you both for your help.

amitchandak
Super User
Super User

@Anonymous , hope you formula working fine.

Try like

Net Sales LY=
var _max = date(year(today())-1,month(today()),day(today()))
return
CALCULATE(sum('B&M_Database_All'[Net Sales]),
SAMEPERIODLASTYEAR('Dim Date'[Date]), 'Dim Date'[Date] <=_max)

 

make sure date and year in visual table come from Date dim

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube
Anonymous
Not applicable

@amitchandakthanks your proposal function 🙂

Another question, for example in this case, I have not update my database since August 9th, but actual day is August 14th.

Is it possible to block it to the last data fill in 2020 ?

 

Annotation 2020-08-14 111006.pngThanks a lot for your help !

@Anonymous , Take a date from fact

 

Net Sales LY= 
var _max1 =maxx('B&M_Database_All',[date])
var _max = date(year(_max1)-1,month(_max1),day(_max1))
return
  CALCULATE(sum('B&M_Database_All'[Net Sales]),
    SAMEPERIODLASTYEAR('Dim Date'[Date]), 'Dim Date'[Date] <=_max)
Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube
Anonymous
Not applicable

@amitchandak 

 

Thanks, I have try it, but it generate an error :

 

Net Sales LY Test 2 = 
    var _max1 =maxx('B&M_Database_All',[Date - Billing])
    var _max = date(year(_max1)-1,month(_max1),day(_max1))
    return 
    CALCULATE(sum('B&M_Database_All'[Net Sales]),
    SAMEPERIODLASTYEAR('Dim Date'[Date]),'Dim Date'[Date]<=_max)

 

 

On total level, it's ok, but when I want to check day by day or filter on a specific month it generate an error.

 

Annotation 2020-08-14 135922.png

Helpful resources

Announcements
September Power BI Update Carousel

Power BI Monthly Update - September 2025

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

August 2025 community update carousel

Fabric Community Update - August 2025

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