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

Power BI is turning 10! Let’s celebrate together with dataviz contests, interactive sessions, and giveaways. Register now.

Reply
rpiboy_1
Helper V
Helper V

DATESINPERIOD - filter context handling with Page Date slicer, do I need a CALCULATETABLE?

  • I have a well formed Date Table (thanks Bravo).
  • I have a report page with a slicer on it that allows the user to define a date range (BETWEEN)
  • I'm calculating rolling average as per SQLBI

 

Logins R6M = 
VAR _noMonths = 6
VAR _lastSelectedDate = MAX('Date'[Date])
VAR _period = 
    DATESINPERIOD( 'Date'[Date], _lastSelectedDate, -_noMonths, MONTH)
VAR _result =
    CALCULATE(
        AVERAGEX(
            VALUES('Date'[Fiscal Month Number]),
            [Logins (sum)]
        ),
        _period
    )
VAR _lastDateWithLogin = MAX( 'Monthly User Activity by Project'[Date])
VAR _firstVisibleDate = MIN('Date'[Date])
RETURN
    IF( _firstVisibleDate <= _lastDateWithLogin, _result)​

 

  • I know that DATESINPERIOD is 'sugar syntax' around DAX that I could write myself.
    • what I don't recall, does DATESINPERIOD apply a CALCULATETABLE to the 'Date' table?
  • That is, if the period on the page (thanks to the slicer) started at Jan 1, 2022, will the calculation for 1/1/2022 account for the six months of 2021 that are not included in the slicer range?
  • I'm asking because I was thinking that the context from the slicer on page would be strictly applied, in which case the first period of averages would be in-accurate because it would be missing data.
  • I wrote a version of the above including a CALCULATETABLE, but both measures report the same data.

 

Logins R6M (filter) = 
VAR _noMonths = 6
VAR _lastSelectedDate = MAX('Date'[Date])

VAR _period = 
    CALCULATETABLE(
        DATESINPERIOD(
            'Date'[Date],
            _lastSelectedDate,
            -_noMonths,
            MONTH
        ),
        ALL('Date'[Date])
    )

VAR _result =
    CALCULATE(
        AVERAGEX(
            VALUES('Date'[Fiscal Month Number]),
            [Logins (sum)]
        ),
        _period
    )

VAR _lastDateWithLogin = MAX( 'Monthly User Activity by Project'[Date])
VAR _firstVisibleDate = MIN('Date'[Date])
RETURN
    IF( _firstVisibleDate <= _lastDateWithLogin, _result)​

 

2 REPLIES 2
rpiboy_1
Helper V
Helper V

 If my logic is correct, that is I do need to extend the period outside the scope of the Page Slicer, then why do both measures return the exact same values for the first dates shown. There should be at least some deviation between the two measures for the first 'period', eventually coming into alignment the further away you get from the 'start'.

ahadkarimi
Solution Specialist
Solution Specialist

Hi @rpiboy_1,

Yes, DATESINPERIOD respects the slicer context, so it won't include dates outside the slicer range, which might cause inaccuracies. Your version with CALCULATETABLE and ALL('Date'[Date]) is better because it ensures all needed dates are considered for accurate rolling averages.

Did I answer your question?  If so, please mark my post as the solution!✔️
Your Kudos are much appreciated!  Proud to be a Responsive Resident!

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

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

June 2025 community update carousel

Fabric Community Update - June 2025

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