Join us at FabCon Atlanta from March 16 - 20, 2026, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.
Register now!Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes! Register now.
Need to get the value of the second last month in the dataset. The last date does not correspond to todays date so cannot use today but can use the max date - one month. I have tried lots of functions getting closest with the datediff and datesinperiod functions.
I have got the code for the last month in my dataset.
This Month =
CALCULATE(
SUM('Sales'[Profit]),
DATESINPERIOD(
'Date'[Date],
MAX('Date'[Date]),
-1,
MONTH
)
)
The above works but when I add a -2 for the month before this I am getting the sum of the last month and the month before just want the individual value.
Solved! Go to Solution.
Hi @akhaliq7
Best practice is to always have a YearMonthNumber column in the date table which is a sequential number that keeps adding 1 every month even over different years. If you don't have it then it can be simply created using
YearMonthNumber =
RANKX ( 'Date', FORMAT ( 'Date'[Date], "YYYYMM" ),, ASC, DENSE )
Then you can simply use
This Month =
CALCULATE (
SUM ( 'Sales'[Profit] ),
'Date'[YearMonthNumber] = MAX ( 'Date'[YearMonthNumber] ) - 2,
ALLSELECTED ( 'Date' )
)
Hi @akhaliq7
Best practice is to always have a YearMonthNumber column in the date table which is a sequential number that keeps adding 1 every month even over different years. If you don't have it then it can be simply created using
YearMonthNumber =
RANKX ( 'Date', FORMAT ( 'Date'[Date], "YYYYMM" ),, ASC, DENSE )
Then you can simply use
This Month =
CALCULATE (
SUM ( 'Sales'[Profit] ),
'Date'[YearMonthNumber] = MAX ( 'Date'[YearMonthNumber] ) - 2,
ALLSELECTED ( 'Date' )
)
Thanks you have saved me so much time this is a good solution as it is dynamic as well.
Hi @akhaliq7 ,
I have a written a measure for this please do check,
/*
Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes!
Check out the September 2025 Power BI update to learn about new features.
User | Count |
---|---|
9 | |
8 | |
6 | |
4 | |
3 |