Forum Discussion

hchava5292's avatar
hchava5292
Frequent Visitor
3 months ago
Solved

Based on Dynamic period selection previous period value is incorrect

Hi All,
I have a requirement of period selection last month, last 3 months, current month, next month, next 3 months, custom period where user will select there own start and end dates. My model is direct lake semantic model. So, i have created a calculated groups for this periodsin semantic model. Below are the logic I used for period implementation. My use case is if user select last month he have to see May 2026 value in KPI.In reference label it has to show variance value compared with previous month April 2026. If last 3 months selected then Current Period value should be from Mar-May 2026 it should compare aganist Feb 2026 value for variance. This is scenario for next month and next 3 months as well. I tried using below logics but for some reason previous period value is not correct. I appreciate help here.

If you look at the screenshot i have attached it clearly shows current month selection which means June 2026 that particular month previous period should be May 2026 but it is giving me some other value.Same with all other selections if i select last 3 months which means from march-may 2026 previous period value should be Feb 2026 but some other value it is showing.


Logics:

_anchorDate =
CALCULATE (
MAX ( PBI_DateDim[DateValue] ),
REMOVEFILTERS ( PBI_DateDim ),
PBI_DateDim[DateValue] <= TODAY ()
)

Calculated Groups

Last Month = VAR _endDate = EOMONTH([_anchorDate], -1)
VAR _startDate = EOMONTH(_endDate, -1) + 1
RETURN
CALCULATE(
SELECTEDMEASURE(),
KEEPFILTERS(DATESBETWEEN(PBI_DateDim[DateValue], _startDate, _endDate))
)

Last 3 Months = VAR _endDate = EOMONTH([_anchorDate], -1)
VAR _startDate = EOMONTH(_endDate, -3) + 1
RETURN
CALCULATE(
SELECTEDMEASURE(),
KEEPFILTERS(DATESBETWEEN(PBI_DateDim[DateValue], _startDate, _endDate))
)

Current Month =
VAR _startDate = EOMONTH([_anchorDate], -1) + 1
VAR _endDate = EOMONTH([_anchorDate], 0)
RETURN
CALCULATE(
SELECTEDMEASURE(),
KEEPFILTERS(
DATESBETWEEN(PBI_DateDim[DateValue], _startDate, _endDate)
)
)

Next Month =
VAR _startDate = EOMONTH([_anchorDate], 0) + 1
VAR _endDate = EOMONTH([_anchorDate], 1)
RETURN
CALCULATE(
SELECTEDMEASURE(),
KEEPFILTERS(
DATESBETWEEN(PBI_DateDim[DateValue], _startDate, _endDate)
)
)

Next 3 Months =
VAR _startDate = EOMONTH([_anchorDate], 0) + 1
VAR _endDate = EOMONTH([_anchorDate], 3)
RETURN
CALCULATE(
SELECTEDMEASURE(),
KEEPFILTERS(
DATESBETWEEN(PBI_DateDim[DateValue], _startDate, _endDate)
)
)

Custom Period = SELECTEDMEASURE()


Base Measures:

Allocated Hours =
SUMX (
SUMMARIZE (
Fact,
Fact[RoomKey],
Fact[DateValue],
Fact[Hour]
),
MIN ( CALCULATE ( SUM ( Fact[AllocatedHours] ) ), 1 )
)

Total Room Hours = SUM ( Fact[TotalRoomHours] )
Room Time Allocated = DIVIDE ( [Allocated Hours], [Total Room Hours] )

There is a active relationship between the Fact and date table using Datevalue field.
Time Allocated Current Period= calculate([Time Allocated], 'Period'[Period] = SELECTEDVALUE('Period'[Period], "Last Month"))
Room Time Allocated Previous Period =
VAR _period =
SELECTEDVALUE('Period'[Period], "Last Month")

VAR _anchorDate =
[_anchorDate]

VAR _alignedEnd =
EOMONTH(_anchorDate, -1)

VAR _currentMonthEnd =
EOMONTH(_anchorDate, 0)

VAR _customStart =
CALCULATE(
MIN(PBI_DateDim[DateValue]),
ALLSELECTED(PBI_DateDim[DateValue])
)

VAR _currentStartDate =
SWITCH(
_period,
"Last Month", EOMONTH(_anchorDate, -2) + 1,
"Last 3 Months", EOMONTH(_alignedEnd, -3) + 1,
"Current Month", EOMONTH(_anchorDate, -1) + 1,
"Next Month", _currentMonthEnd + 1,
"Next 3 Months", _currentMonthEnd + 1,
"Custom Period", _customStart,
EOMONTH(_anchorDate, -2) + 1
)

VAR _previousStartDate =
EOMONTH(_currentStartDate, -2) + 1

VAR _previousEndDate =
EOMONTH(_currentStartDate, -1)

RETURN
DIVIDE(
CALCULATE(
[Allocated Hours],
REMOVEFILTERS(PBI_DateDim),
DATESBETWEEN(
PBI_DateDim[DateValue],
_previousStartDate,
_previousEndDate
)
),
CALCULATE(
[Total Room Hours],
REMOVEFILTERS(PBI_DateDim),
DATESBETWEEN(
PBI_DateDim[DateValue],
_previousStartDate,
_previousEndDate
)
)
)



  • The bug is in these two lines:

    VAR _previousStartDate = EOMONTH(_currentStartDate, -2) + 1
    VAR _previousEndDate   = EOMONTH(_currentStartDate, -1)


    You're going back two months from the current start instead of one, so everything is off by a month. For Current Month, _currentStartDate is June 1; your formula lands on April instead of May.


    Replace those two lines with:

    VAR _previousEnd   = EOMONTH(_currentStart, -1)
    VAR _previousStart = EOMONTH(_previousEnd, -1) + 1

     

     

     

     

3 Replies

  • The bug is in these two lines:

    VAR _previousStartDate = EOMONTH(_currentStartDate, -2) + 1
    VAR _previousEndDate   = EOMONTH(_currentStartDate, -1)


    You're going back two months from the current start instead of one, so everything is off by a month. For Current Month, _currentStartDate is June 1; your formula lands on April instead of May.


    Replace those two lines with:

    VAR _previousEnd   = EOMONTH(_currentStart, -1)
    VAR _previousStart = EOMONTH(_previousEnd, -1) + 1

     

     

     

     

  • Hi  hchava5292 


    Thank you for reaching out to Microsoft Fabric Community and thanks to andrewsommer  for providing meaningful insights.


    Just wanted to check if you had the opportunity to review the information provided. Please feel free to contact us if you have any further questions. 

     

     Best Regards,

    Abdul Rafi.

  • Hi hchava5292  ,

    Could you please confirm if the issue has been resolved? If not, feel free to reach out if you have any further questions.

    Your update would be helpful for other members who may face a similar issue.

    Best Regards,
    Abdul Rafi