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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
tskumar
Regular Visitor

Same period or days range for last six months

Request  for guidance on how to apply filter on dates from 1st to CurrentDate/Max Date in current month for last six months and sum the values
For example:
Current Date is : 21 July 2022
so the filter should be : 1 Jun 2022 to 21 Jun 2022, 1 May 2022 to 21 May 2022, 1 Apr 2022 to 21 Apr 2022 so on...

Thank you

2 ACCEPTED SOLUTIONS

Hi,

Thank you for your message.

I think there are many ways to restrict to show until the data is available.

Please check the below measure and the attached file. I amended a little to show up to available date.

 

Same days range last six months: =
VAR _availablelastdate =
    CALCULATE (
        MAXX (
            FILTER ( VALUES ( 'Calendar'[Date] ), [Quantity measure:] <> BLANK () ),
            'Calendar'[Date]
        ),
        ALL ( 'Calendar' )
    )
VAR _currentdaynumber =
    DAY ( MAX ( 'Calendar'[Date] ) )
VAR _endofdatepreviousmonth =
    CALCULATE (
        MAX ( 'Calendar'[End of Month] ),
        DATEADD ( 'Calendar'[Date], -1, MONTH )
    )
VAR _endofdatesixmonthsbefore =
    CALCULATE (
        MAX ( 'Calendar'[End of Month] ),
        DATEADD ( 'Calendar'[Date], -6, MONTH )
    )
VAR _filtercalendar =
    FILTER (
        ALL ( 'Calendar' ),
        'Calendar'[End of Month] >= _endofdatesixmonthsbefore
            && 'Calendar'[End of Month] <= _endofdatepreviousmonth
            && DAY ( 'Calendar'[Date] ) <= _currentdaynumber
    )
RETURN
    IF (
        MAX ( 'Calendar'[Date] ) <= _availablelastdate,
        IF (
            _endofdatepreviousmonth <> BLANK ()
                && _endofdatesixmonthsbefore <> BLANK (),
            CALCULATE ( [Quantity measure:], _filtercalendar )
        )
    )

If this post helps, then please consider accepting it as the solution to help other members find it faster, and give a big thumbs up.


Click here to visit my LinkedIn page

Click here to schedule a short Teams meeting to discuss your question.

View solution in original post

tamerj1
Super User
Super User

Hi @tskumar 

Here is a sample file with the solution https://we.tl/t-KPniHplng5

You need to have a previous date table which is just a shadow copy of the date table. The relationships are shown below. 

1.png

This is the code of the measure. The sample file contains old data so I used fixed date in the code but in your case you need to replace this date with TODAY ( )

2.png

Sales Previous 6 Months Same Period = 
VAR RefDate = DATE (2009, 7, 15 ) -- Use 'TODAY ( )'
VAR PreviousDates =
    DATESINPERIOD ('Previous Dates'[Date], RefDate, - 6, MONTH )
VAR FilteredPerviousDates =
    FILTER ( PreviousDates, DAY ( 'Previous Dates'[Date] ) <= DAY ( RefDate ) )
VAR Result =
    CALCULATE ( 
        [Sales Amount],
        REMOVEFILTERS ( 'Date' ),
        KEEPFILTERS ( FilteredPerviousDates ),
        USERELATIONSHIP ( 'Date'[Date], 'Previous Dates'[Date] )
    )
RETURN
    Result

 

View solution in original post

4 REPLIES 4
tamerj1
Super User
Super User

Hi @tskumar 

Here is a sample file with the solution https://we.tl/t-KPniHplng5

You need to have a previous date table which is just a shadow copy of the date table. The relationships are shown below. 

1.png

This is the code of the measure. The sample file contains old data so I used fixed date in the code but in your case you need to replace this date with TODAY ( )

2.png

Sales Previous 6 Months Same Period = 
VAR RefDate = DATE (2009, 7, 15 ) -- Use 'TODAY ( )'
VAR PreviousDates =
    DATESINPERIOD ('Previous Dates'[Date], RefDate, - 6, MONTH )
VAR FilteredPerviousDates =
    FILTER ( PreviousDates, DAY ( 'Previous Dates'[Date] ) <= DAY ( RefDate ) )
VAR Result =
    CALCULATE ( 
        [Sales Amount],
        REMOVEFILTERS ( 'Date' ),
        KEEPFILTERS ( FilteredPerviousDates ),
        USERELATIONSHIP ( 'Date'[Date], 'Previous Dates'[Date] )
    )
RETURN
    Result

 

tskumar
Regular Visitor

Thank you Jihwan_Kim for the quick response and solution. It will definitely help me,  one small concern is I have to select the date to show the MTD value, it would be great if it could detect the last available date. 
For example if the data is available till December 15th only, then it should show till that date instead of rest of the dates as well  as shown in the image below   

tskumar_0-1659262360336.png

 



Hi,

Thank you for your message.

I think there are many ways to restrict to show until the data is available.

Please check the below measure and the attached file. I amended a little to show up to available date.

 

Same days range last six months: =
VAR _availablelastdate =
    CALCULATE (
        MAXX (
            FILTER ( VALUES ( 'Calendar'[Date] ), [Quantity measure:] <> BLANK () ),
            'Calendar'[Date]
        ),
        ALL ( 'Calendar' )
    )
VAR _currentdaynumber =
    DAY ( MAX ( 'Calendar'[Date] ) )
VAR _endofdatepreviousmonth =
    CALCULATE (
        MAX ( 'Calendar'[End of Month] ),
        DATEADD ( 'Calendar'[Date], -1, MONTH )
    )
VAR _endofdatesixmonthsbefore =
    CALCULATE (
        MAX ( 'Calendar'[End of Month] ),
        DATEADD ( 'Calendar'[Date], -6, MONTH )
    )
VAR _filtercalendar =
    FILTER (
        ALL ( 'Calendar' ),
        'Calendar'[End of Month] >= _endofdatesixmonthsbefore
            && 'Calendar'[End of Month] <= _endofdatepreviousmonth
            && DAY ( 'Calendar'[Date] ) <= _currentdaynumber
    )
RETURN
    IF (
        MAX ( 'Calendar'[Date] ) <= _availablelastdate,
        IF (
            _endofdatepreviousmonth <> BLANK ()
                && _endofdatesixmonthsbefore <> BLANK (),
            CALCULATE ( [Quantity measure:], _filtercalendar )
        )
    )

If this post helps, then please consider accepting it as the solution to help other members find it faster, and give a big thumbs up.


Click here to visit my LinkedIn page

Click here to schedule a short Teams meeting to discuss your question.
Jihwan_Kim
Super User
Super User

Hi,

I tried to create a sample pbix file like below.

I suggest having a calednar dimension table, and then writing a measure something like below.

I hope the below can provide some ideas on how to create a solution for your dataset.

 

Untitled.png

 

Same days range last six months: =
VAR _currentdaynumber =
    DAY ( MAX ( 'Calendar'[Date] ) )
VAR _endofdatepreviousmonth =
    CALCULATE (
        MAX ( 'Calendar'[End of Month] ),
        DATEADD ( 'Calendar'[Date], -1, MONTH )
    )
VAR _endofdatesixmonthsbefore =
    CALCULATE (
        MAX ( 'Calendar'[End of Month] ),
        DATEADD ( 'Calendar'[Date], -6, MONTH )
    )
VAR _filtercalendar =
    FILTER (
        ALL ( 'Calendar' ),
        'Calendar'[End of Month] >= _endofdatesixmonthsbefore
            && 'Calendar'[End of Month] <= _endofdatepreviousmonth
            && DAY ( 'Calendar'[Date] ) <= _currentdaynumber
    )
RETURN
    IF (
        _endofdatepreviousmonth <> BLANK ()
            && _endofdatesixmonthsbefore <> BLANK (),
        CALCULATE ( [Quantity measure:], _filtercalendar )
    )

If this post helps, then please consider accepting it as the solution to help other members find it faster, and give a big thumbs up.


Click here to visit my LinkedIn page

Click here to schedule a short Teams meeting to discuss your question.

Helpful resources

Announcements
July 2025 community update carousel

Fabric Community Update - July 2025

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

July PBI25 Carousel

Power BI Monthly Update - July 2025

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