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
abd8
Regular Visitor

Dynamic Dates for 13-Month Trend

Hi -

 

I am attempting to get a dynamic 13-month trend that will update based on the date I select in a drop-down.

 

For example, when I select February 2025, I want the trend to show February 2024-February 2025 or when I select January 2025, I want to see January 2024 - January 2025, etc. 

 

I can't use a date range filter due to other visuals on the page that are only for the specific reporting month. Also I don't want to allow free range filter, only 13 months back from the month selected.

 

Based on some googling / copilot help I completed the following steps, but the range is still not coming through correctly. Any help greatly appreciated!

 

Step 1: Create SelectedMonth Measure on 'Calendar' Table

SelectedMonth = SELECTEDVALUE('Calendar'[Year Month])

Step 2: Create SelectMonthColumn

SelectedMonthColumn = [SelectedMonth]
Step 3: Create StartDate Measure on 'Calendar' Table
StartDate =
DATE(YEAR([SelectedMonth]) - 1, MONTH([SelectedMonth]), 1)
Step 4: Create StartDateColumn
StartDateColumn = [StartDate]
Step 5: Create '13_Month_Trend' table with start date and end date dynamic based on measures / columns created in steps 1-4. This is where things appear to be going wrong, with this table returning dates Jan 2024 - Dec 2025.
13_Month_Trend =
VAR EndDate = MAX('Calendar'[SelectedMonthColumn])
VAR StartDate = EDATE(EndDate, -12)
RETURN
    ADDCOLUMNS (
        CALENDAR (StartDate, EndDate),
        "Year", YEAR([Date]),
        "Month Number", MONTH([Date]),
        "Year Month", FORMAT([Date], "YYYY-MM"),
        "Day", DAY([Date]),
        "Day of Week", WEEKDAY([Date]),
        "Quarter", QUARTER([Date]),
        "Quarter Year", YEAR([Date]) & " Q" & QUARTER([Date])
    )
 
 
 
Here is a screenshot of the results I'm getting. When filtered for February 2025, I would expect the table to start at 2/2024 and end with 2/2025. However, it just showed Jan 2024 - Dec 2025, no matter what i select in the dropdown.
abd8_0-1744314432885.png

 

 

 

 

 

1 ACCEPTED SOLUTION
Ashish_Mathur
Super User
Super User

Hi,

I have solved a similar question in the attached PBI file.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

View solution in original post

8 REPLIES 8
Ashish_Mathur
Super User
Super User

Hi,

I have solved a similar question in the attached PBI file.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/
abd8
Regular Visitor

I also noticed that the date range is different depending on whether I display as just the date or the date hierarchy. Does anyone know why this may be?

Hi @abd8,
I wanted to check if you had the opportunity to review the information provided by @Ashish_Mathur. Please feel free to contact us if you have any further questions. If the response has addressed your query, please accept it as a solution and give a 'Kudos' so other members can easily find it.
Thank you.

DataInsights
Super User
Super User

@abd8,

 

The calculated table you created in step 5 is unable to recognize user-specified filters in Report view (e.g., slicers). See link below for a dynamic solution:

 

https://www.sqlbi.com/articles/show-previous-6-months-of-data-from-single-slicer-selection/ 





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

Proud to be a Super User!




This is very helpful. However, when I try to implement that code, I'm getting this error:

USERELATIONSHIP function can only use the two columns references participating in relationship.

 

I did tweak it slightly for my use case, so that may be what's causing it:

13_Month_Trend =
 
VAR NumOfMonths = -13
VAR ReferenceDate = 'Calendar'[SelectedMonth]
VAR PreviousDates =
    DATESINPERIOD (
        'Previous Date'[Date],
        ReferenceDate,
        NumOfMonths,
        MONTH
    )
VAR Result =
    CALCULATE (
        SELECTEDMEASURE (),
        REMOVEFILTERS ( 'Calendar' ),
        KEEPFILTERS ( PreviousDates ),
        USERELATIONSHIP ( 'Previous Date'[Date], 'Calendar'[SelectedMonthColumn] )
    )
RETURN Result

@abd8,

 

Verify the columns in the relationship between 'Previous Date' and 'Calendar'. The columns in the relationship should be used in the USERELATIONSHIP function.





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

Proud to be a Super User!




Ok, I set up the relationship and that formala is now working. How do I now set up my slicer and x-axis to ensure a proper 13-month lookback on the x-axis based on the slicer selection?

@abd8,

 

The slicer would be based on a 'Calendar' field such as Year Month (you should be able to control all visuals with this slicer). The x-axis would use a 'Previous Date' field.





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

Proud to be a Super User!




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.