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
Anonymous
Not applicable

Help ContextFilter with Time -- YTD

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:

User1314_PowerB_0-1716376735275.png

 

 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

1 ACCEPTED SOLUTION
Anonymous
Not applicable

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

vkaiyuemsft_0-1716881400020.png

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.

 

View solution in original post

5 REPLIES 5
Anonymous
Not applicable

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

vkaiyuemsft_0-1716881400020.png

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.

 

danextian
Super User
Super User

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.

 





Dane Belarmino | Microsoft MVP | Proud to be a Super User!

Did I answer your question? Mark my post as a solution!


"Tell me and I’ll forget; show me and I may remember; involve me and I’ll understand."
Need Power BI consultation, get in touch with me on LinkedIn or hire me on UpWork.
Learn with me on YouTube @DAXJutsu or follow my page on Facebook @DAXJutsuPBI.
Anonymous
Not 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:

User1314_PowerB_0-1716384949374.png

 

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.

User1314_PowerB_1-1716385207389.png



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.





Dane Belarmino | Microsoft MVP | Proud to be a Super User!

Did I answer your question? Mark my post as a solution!


"Tell me and I’ll forget; show me and I may remember; involve me and I’ll understand."
Need Power BI consultation, get in touch with me on LinkedIn or hire me on UpWork.
Learn with me on YouTube @DAXJutsu or follow my page on Facebook @DAXJutsuPBI.
Anonymous
Not applicable

Sure! Here is a sample Dataset:

SeasonSupplierYearQuarterMonthDateSales
1Supplier A2023Qtr1ene11/01/2023           368,30  
1Supplier A2023Qtr1ene13/01/2023-         153,97  
1Supplier A2023Qtr1ene25/01/2023       1.188,24  
1Supplier A2023Qtr1feb03/02/2023       3.257,29  
1Supplier A2023Qtr1feb08/02/2023     10.798,85  
1Supplier A2023Qtr1feb10/02/2023       4.435,98  
1Supplier A2023Qtr1feb20/02/2023       9.726,95  
1Supplier A2023Qtr1mar09/03/2023       2.653,02  
2Supplier A2023Qtr1ene04/01/2023-            80,81  
2Supplier A2023Qtr1ene12/01/2023-         106,15  
2Supplier A2023Qtr1ene20/01/2023           764,82  
2Supplier A2023Qtr1feb01/02/2023       4.302,45  
2Supplier A2023Qtr1feb03/02/2023       9.459,34  
2Supplier A2023Qtr1feb08/02/2023       9.745,48  
2Supplier A2023Qtr1feb10/02/2023       9.497,13  
2Supplier A2023Qtr1feb20/02/2023     15.771,80  
2Supplier A2023Qtr1feb21/02/2023     16.371,64  
2Supplier A2023Qtr1feb22/02/2023     35.541,47  
2Supplier A2023Qtr1feb24/02/2023     61.303,83  
2Supplier A2023Qtr1mar01/03/2023     65.506,64  
2Supplier A2023Qtr1mar03/03/2023     93.857,79  
2Supplier A2023Qtr1mar09/03/2023     66.897,74  
2Supplier A2023Qtr1mar10/03/2023     51.143,15  
2Supplier A2023Qtr1mar15/03/2023     79.078,91  
2Supplier A2023Qtr1mar17/03/2023     79.692,06  
2Supplier A2023Qtr1mar22/03/2023   101.658,91  
2Supplier A2023Qtr1mar23/03/2023           578,02  
2Supplier A2023Qtr1mar24/03/2023   115.613,78  
2Supplier A2023Qtr1mar27/03/2023     46.635,61  
2Supplier A2023Qtr1mar29/03/2023   140.062,74  
2Supplier A2023Qtr1mar31/03/2023   124.102,15  
2Supplier A2024Qtr1ene05/01/2024-         212,32  
2Supplier A2024Qtr1ene11/01/2024             85,94  
2Supplier A2024Qtr1feb05/02/2024       9.047,53  
2Supplier A2024Qtr1feb07/02/2024           849,90  
2Supplier A2024Qtr1feb15/02/2024       1.606,97  
2Supplier A2024Qtr1feb19/02/2024       1.733,78  
2Supplier A2024Qtr1feb21/02/2024       1.959,94  
2Supplier A2024Qtr1feb22/02/2024       5.930,13  
2Supplier A2024Qtr1feb27/02/2024       5.599,51  
2Supplier A2024Qtr1feb28/02/2024       7.628,05  
2Supplier A2024Qtr1mar04/03/2024       7.490,27  
2Supplier A2024Qtr1mar07/03/2024     16.801,74  
2Supplier A2024Qtr1mar08/03/2024     15.166,82  
2Supplier A2024Qtr1mar13/03/2024     25.212,62  
2Supplier A2024Qtr1mar18/03/2024     16.381,80  
2Supplier A2024Qtr1mar20/03/2024     22.414,28  
2Supplier A2024Qtr1mar25/03/2024     13.862,49  

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.

Top Solution Authors