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!Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Request now
Hello Forum!
I'm writing here with a little problem I can't seem to solve, hoping you can lend me a hand. To put it simply, I have a database with a 'Date' field and a 'Sales' field. What I would like to do is create a visualization like this:
The column "Previous Year Sales" should return the sales for the same timeframe as that row. For example, if I have data for 2024 up to 03/28/2024, it should give me the equivalent sales from 01/01/2023 to 03/28/2024.
To achieve this, I've created this measure, but when I place it in the table, it gives me all the sales from my database for the 2023 row and calculates correctly for 2024. I understand this is due to a problem with the filter context. Is there any way to fix this?
Sales Previous Year =
VAR MaxActualDate = LASTDATE('BBDD'[Date])
VAR MinFechaActual = FIRSTDATE('BBDD'[Date])
VAR PreviousSales =
CALCULATE(
SUM('BBDD'[Sales]),
DATESBETWEEN(
'BBDD'[Date],
DATEADD(MinActualDate, -1, YEAR),
DATEADD(MaxActualDate, -1, YEAR)
),
ALLEXCEPT('BBDD', 'BBDD'[Sales])
)
RETURN
PreviousSales
Solved! Go to Solution.
Hi @Anonymous ,
Thanks for the reply from @danextian , please allow me to provide another insight:
Create measure.
MEASURE =
VAR _min_date =
CALCULATE (
MIN ( 'Table'[Date] ),
FILTER ( ALL ( 'Table' ), 'Table'[Year] = MAX ( 'Table'[Year] ) )
)
VAR _max_date =
CALCULATE (
MAX ( 'Table'[Date] ),
FILTER ( ALL ( 'Table' ), 'Table'[Year] = MAX ( 'Table'[Year] ) )
)
VAR _pre =
CALCULATE (
SUM ( 'Table'[Sales] ),
FILTER (
ALL ( 'Table' ),
'Table'[Date]
>= DATE ( YEAR ( _min_date ) - 1, MONTH ( _min_date ), DAY ( _min_date ) )
&& 'Table'[Date]
<= DATE ( YEAR ( _max_date ) - 1, MONTH ( _max_date ), DAY ( _max_date ) )
)
)
RETURN
_pre
If your Current Period does not refer to this, please clarify in a follow-up reply.
Best Regards,
Clara Gong
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Hi @Anonymous ,
Thanks for the reply from @danextian , please allow me to provide another insight:
Create measure.
MEASURE =
VAR _min_date =
CALCULATE (
MIN ( 'Table'[Date] ),
FILTER ( ALL ( 'Table' ), 'Table'[Year] = MAX ( 'Table'[Year] ) )
)
VAR _max_date =
CALCULATE (
MAX ( 'Table'[Date] ),
FILTER ( ALL ( 'Table' ), 'Table'[Year] = MAX ( 'Table'[Year] ) )
)
VAR _pre =
CALCULATE (
SUM ( 'Table'[Sales] ),
FILTER (
ALL ( 'Table' ),
'Table'[Date]
>= DATE ( YEAR ( _min_date ) - 1, MONTH ( _min_date ), DAY ( _min_date ) )
&& 'Table'[Date]
<= DATE ( YEAR ( _max_date ) - 1, MONTH ( _max_date ), DAY ( _max_date ) )
)
)
RETURN
_pre
If your Current Period does not refer to this, please clarify in a follow-up reply.
Best Regards,
Clara Gong
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Hi @Anonymous ,
It appears you are using the date from your fact table. It is always a good practice to use a separate dates/calendar table for time intelligence calculations. With a dates table, you can make a previous year calcaution with a formula this simple.
CALCULATE ( [Sales Measure], SAMEPERIODLASTYEAR ( Dates[Date] ) )
for YTD
=
CALCULATE ( [Sales Measure], DATESYTD ( Dates[Date] ) )
YTD LY
CALCULATE ( [YTD LY Measure], SAMEPERIODLASTYEAR ( Dates[Date] ) )
Attached is a sample PBIX containing a DAX calendar table. There's quite some columns in there so just select what you need.
Hi Danextian!
Thank you for your response. Associating the date with the Date table almost fixed the problem. I've noticed that for certain cases, I'll explain it here as an example and attach a table to make it easier to understand:
For 2024, my database has data from 01/02/2024 to 03/28/2024, and this equates to sales of 140M€. What I would like is for the LYD measure to return the sales for the same period in 2023, which would be 30M€. However, the result I’m getting is 32M€ because it’s taking the period from 01/01/2023 to 03/31/2023.
Taking advantage of this query and very much related to it, if I later want to do the same but with a table like this one, I now want to show the sales for that month in the previous year and another measure that shows the sales for the month immediately preceding the one indicated in the row.
Thank you so much for your help!
Hi @Anonymous
Can you please post a a workable sample data that represets your use case (not an image). Link to an Excel file in the cloud will do. Please remove confidential dat.
Sure! Here is a sample Dataset:
| Season | Supplier | Year | Quarter | Month | Date | Sales |
| 1 | Supplier A | 2023 | Qtr1 | ene | 11/01/2023 | 368,30 |
| 1 | Supplier A | 2023 | Qtr1 | ene | 13/01/2023 | - 153,97 |
| 1 | Supplier A | 2023 | Qtr1 | ene | 25/01/2023 | 1.188,24 |
| 1 | Supplier A | 2023 | Qtr1 | feb | 03/02/2023 | 3.257,29 |
| 1 | Supplier A | 2023 | Qtr1 | feb | 08/02/2023 | 10.798,85 |
| 1 | Supplier A | 2023 | Qtr1 | feb | 10/02/2023 | 4.435,98 |
| 1 | Supplier A | 2023 | Qtr1 | feb | 20/02/2023 | 9.726,95 |
| 1 | Supplier A | 2023 | Qtr1 | mar | 09/03/2023 | 2.653,02 |
| 2 | Supplier A | 2023 | Qtr1 | ene | 04/01/2023 | - 80,81 |
| 2 | Supplier A | 2023 | Qtr1 | ene | 12/01/2023 | - 106,15 |
| 2 | Supplier A | 2023 | Qtr1 | ene | 20/01/2023 | 764,82 |
| 2 | Supplier A | 2023 | Qtr1 | feb | 01/02/2023 | 4.302,45 |
| 2 | Supplier A | 2023 | Qtr1 | feb | 03/02/2023 | 9.459,34 |
| 2 | Supplier A | 2023 | Qtr1 | feb | 08/02/2023 | 9.745,48 |
| 2 | Supplier A | 2023 | Qtr1 | feb | 10/02/2023 | 9.497,13 |
| 2 | Supplier A | 2023 | Qtr1 | feb | 20/02/2023 | 15.771,80 |
| 2 | Supplier A | 2023 | Qtr1 | feb | 21/02/2023 | 16.371,64 |
| 2 | Supplier A | 2023 | Qtr1 | feb | 22/02/2023 | 35.541,47 |
| 2 | Supplier A | 2023 | Qtr1 | feb | 24/02/2023 | 61.303,83 |
| 2 | Supplier A | 2023 | Qtr1 | mar | 01/03/2023 | 65.506,64 |
| 2 | Supplier A | 2023 | Qtr1 | mar | 03/03/2023 | 93.857,79 |
| 2 | Supplier A | 2023 | Qtr1 | mar | 09/03/2023 | 66.897,74 |
| 2 | Supplier A | 2023 | Qtr1 | mar | 10/03/2023 | 51.143,15 |
| 2 | Supplier A | 2023 | Qtr1 | mar | 15/03/2023 | 79.078,91 |
| 2 | Supplier A | 2023 | Qtr1 | mar | 17/03/2023 | 79.692,06 |
| 2 | Supplier A | 2023 | Qtr1 | mar | 22/03/2023 | 101.658,91 |
| 2 | Supplier A | 2023 | Qtr1 | mar | 23/03/2023 | 578,02 |
| 2 | Supplier A | 2023 | Qtr1 | mar | 24/03/2023 | 115.613,78 |
| 2 | Supplier A | 2023 | Qtr1 | mar | 27/03/2023 | 46.635,61 |
| 2 | Supplier A | 2023 | Qtr1 | mar | 29/03/2023 | 140.062,74 |
| 2 | Supplier A | 2023 | Qtr1 | mar | 31/03/2023 | 124.102,15 |
| 2 | Supplier A | 2024 | Qtr1 | ene | 05/01/2024 | - 212,32 |
| 2 | Supplier A | 2024 | Qtr1 | ene | 11/01/2024 | 85,94 |
| 2 | Supplier A | 2024 | Qtr1 | feb | 05/02/2024 | 9.047,53 |
| 2 | Supplier A | 2024 | Qtr1 | feb | 07/02/2024 | 849,90 |
| 2 | Supplier A | 2024 | Qtr1 | feb | 15/02/2024 | 1.606,97 |
| 2 | Supplier A | 2024 | Qtr1 | feb | 19/02/2024 | 1.733,78 |
| 2 | Supplier A | 2024 | Qtr1 | feb | 21/02/2024 | 1.959,94 |
| 2 | Supplier A | 2024 | Qtr1 | feb | 22/02/2024 | 5.930,13 |
| 2 | Supplier A | 2024 | Qtr1 | feb | 27/02/2024 | 5.599,51 |
| 2 | Supplier A | 2024 | Qtr1 | feb | 28/02/2024 | 7.628,05 |
| 2 | Supplier A | 2024 | Qtr1 | mar | 04/03/2024 | 7.490,27 |
| 2 | Supplier A | 2024 | Qtr1 | mar | 07/03/2024 | 16.801,74 |
| 2 | Supplier A | 2024 | Qtr1 | mar | 08/03/2024 | 15.166,82 |
| 2 | Supplier A | 2024 | Qtr1 | mar | 13/03/2024 | 25.212,62 |
| 2 | Supplier A | 2024 | Qtr1 | mar | 18/03/2024 | 16.381,80 |
| 2 | Supplier A | 2024 | Qtr1 | mar | 20/03/2024 | 22.414,28 |
| 2 | Supplier A | 2024 | Qtr1 | mar | 25/03/2024 | 13.862,49 |
Check out the November 2025 Power BI update to learn about new features.
Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!
| User | Count |
|---|---|
| 104 | |
| 82 | |
| 71 | |
| 50 | |
| 46 |