Forum Discussion
Help ContextFilter with Time -- YTD
- Anonymous2 years ago
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 _preIf 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.
- Anonymous2 years agoNot applicable
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!- danextian2 years agoSuper User
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.
- Anonymous2 years agoNot applicable
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