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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
VickyDev18
Advocate II
Advocate II

Time Intelligence based on a specific date as reference

Suppose I have a Scenarios table and a disconnected Period Table  as shown below.

Scenarios    
scenario_idscenario_codescenario_typefiscal_yearforecast_start_date
12021 JANForecast20212/1/2020
22021 FEBForecast20213/1/2021
32021 MARForecast20214/1/2022
32021 APRForecast20215/1/2023
42021 MAYForecast20216/1/2024

 

Period 
period_idperiod_code
1MTD
2QTD
3YTD
4FY

 

 

I would like to create 2 measures start_of_period and end_of period that produce the output below if say 2021 MAY was selected. 

Output        
scenario_idscenario_codescenario_typefiscal_yearforecast_start_dateperiod_codecalendar_monthstart_of_periodend_of_period
12021 MAYForecast20216/1/2021MTDMay-215/1/20215/31/2021
12021 MAYForecast20216/1/2021QTDApr-214/1/20214/30/2021
12021 MAYForecast20216/1/2021QTDMay-215/1/20215/31/2021
12021 MAYForecast20216/1/2021YTDJan-211/1/20211/31/2021
12021 MAYForecast20216/1/2021YTDFeb-212/1/20212/2/2021
12021 MAYForecast20216/1/2021YTDMar-213/1/20213/31/2021
12021 MAYForecast20216/1/2021YTDApr-214/1/20214/30/2021
12021 MAYForecast20216/1/2021YTDMay-215/1/20215/31/2021
12021 MAYForecast20216/1/2021FYJan-211/1/20211/31/2021
12021 MAYForecast20216/1/2021FYFeb-212/1/20212/28/2021
12021 MAYForecast20216/1/2021FYMar-213/1/20213/31/2021
12021 MAYForecast20216/1/2021FYApr-214/1/20214/30/2021
12021 MAYForecast20216/1/2021FYMay-215/1/20215/31/2021
12021 MAYForecast20216/1/2021FYJun-216/1/20216/30/2021
12021 MAYForecast20216/1/2021FYJul-217/1/20217/31/2021
12021 MAYForecast20216/1/2021FYAug-218/1/20218/31/2021
12021 MAYForecast20216/1/2021FYSep-219/1/20219/30/2021
12021 MAYForecast20216/1/2021FYOct-2110/1/202110/31/2021
12021 MAYForecast20216/1/2021FYNov-2111/1/202111/30/2021
12021 MAYForecast20216/1/2021FYDec-2112/1/202112/31/2021


I tried something like this but it ends up producing a start and end date for all periods/months even when MTD is selected. Ideally, when I select MTD and 2021 MAY, it should only return values for the month of May'21 i.e. result should be 1 row as shown above.
When QTD is selected it should return 2 rows as shown above and not 12 rows. 

 

 

start_of_period =
VAR __ReferenceDate = SELECTEDVALUE(Scenarios[forecast_start_date])

VAR __Dates = FILTER(VALUES(Calendar[Date]), Calendar[Date] <= __ReferenceDate)

RETURN
SWITCH (
    SELECTEDVALUE(Period[period_code]),
    "MTD", STARTOFMONTH(__Dates), -- First day of the month    
    "QTD", STARTOFQUARTER(__Dates), -- First day of the quarter    
    "YTD", STARTOFYEAR(__Dates), -- First day of the year    
    "FY", STARTOFYEAR(__Dates) -- First day of the year
)

 

 

 

 

 

end_of_period = 
VAR __ReferenceDate = SELECTEDVALUE(Scenarios[forecast_start_date])

VAR __Dates = FILTER(VALUES(Calendar[Date]), Calendar[Date] <= __ReferenceDate)

RETURN
SWITCH (
    SELECTEDVALUE(Period[period_code]),
    "MTD", ENDOFMONTH(__Dates), -- Last day of the month    
    "QTD", ENDOFMONTH(__Dates), -- Last day of the month    
    "YTD", ENDOFMONTH(__Dates), -- Last day of the month    
    "FY", ENDOFYEAR(__Dates) -- Last day of the year
)

 

 




2 REPLIES 2
Bibiano_Geraldo
Super User
Super User

HI, Make sure your Calendar table is connected appropriately and try this:

 

start_of_period = 
VAR __ReferenceDate = SELECTEDVALUE(Scenarios[forecast_start_date])
VAR __SelectedPeriod = SELECTEDVALUE(Period[period_code])
VAR __StartOfMonth = STARTOFMONTH(__ReferenceDate)
VAR __StartOfQuarter = STARTOFQUARTER(__ReferenceDate)
VAR __StartOfYear = STARTOFYEAR(__ReferenceDate)

RETURN 
SWITCH(
    __SelectedPeriod,
    "MTD", __StartOfMonth,  -- Start of the month for MTD
    "QTD", __StartOfQuarter,  -- Start of the quarter for QTD
    "YTD", __StartOfYear,  -- Start of the year for YTD
    "FY", __StartOfYear  -- Start of the fiscal year for FY
)

 

 

 

end_of_period = 
VAR __ReferenceDate = SELECTEDVALUE(Scenarios[forecast_start_date])
VAR __SelectedPeriod = SELECTEDVALUE(Period[period_code])
VAR __EndOfMonth = ENDOFMONTH(__ReferenceDate)
VAR __EndOfQuarter = ENDOFQUARTER(__ReferenceDate)
VAR __EndOfYear = ENDOFYEAR(__ReferenceDate)

RETURN 
SWITCH(
    __SelectedPeriod,
    "MTD", __EndOfMonth,  -- End of the month for MTD
    "QTD", __EndOfQuarter,  -- End of the quarter for QTD
    "YTD", __EndOfMonth,  -- End of the month (could change for YTD but typically aligns with current month)
    "FY", __EndOfYear  -- End of the fiscal year for FY
)

 

 

This gives an error stating that STARTOFMONTH expects a column. I tried adding a new variable  __Dates and passed that instead of  __ReferenceDate but that is producing a blank. 

 

 

 

VAR __Dates = FILTER(VALUES(Calendar[Date]), Calendar[Date] = __ReferenceDate)

 


Here's a link to a -> sample PBIX file 

 

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

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.