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

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
MyWeeLola
Helper II
Helper II

Using a filter for custom date periods, how I display 2 separate graphs with 2 different time period

Good morning

 

I have a code that utilizes 'addcolumns' to create custom time periods in an easily accessible way and is accessed using a slicer.

 

An example of the code is displayed at the end bottom of the post.

 

Currently, I have a line graph that displays data with the x axis being a date range which is filtered using a slicer.

 

This works in a fairly standard way and works well.

 

If a user selects, "last 7 days" then the last 7 days of data is displayed.

This is replicated for selections like 'last 14 days', mtd, qtd, last qtr, ytd, last year.

 

What I now need is if a user selects 'last 7 days' the graph continues to display the last 7 days, then a second graph displays the previous 7 days before that. That methodology is selected for the other time ranges too. Given the nature of the data being displayed, this would provide an easier way to compare performance.

 

How would I adapt my code to accommodate this?

 

here is the code of the 'date periods' selection I use. 

 

I look forward to any assistance

 

Date Periods =
var _sd = min('Date'[Date])
var _ed = max('Date'[Date])

    VAR __Date = TODAY()
    VAR __NextMonth = EOMONTH(__Date,-1)
    VAR __Month = MONTH(__NextMonth)
    VAR __Year = YEAR(__NextMonth)
    VAR __Quarter = QUARTER(__Date)
    VAR __QYear = SWITCH(__Quarter,1,YEAR(__Date) - 1,YEAR(__Date))
    VAR __MonthStart = SWITCH(__Quarter,1,10,2,1,3,4,7)
    VAR __MonthEnd = SWITCH(__Quarter,1,12,2,3,3,6,9)
    VAR __Day = SWITCH(__Quarter,1,31,2,31,3,30,30)
    VAR __PYear = YEAR(__Date) - 1

RETURN

UNION(
    ADDCOLUMNS(
        DATESMTD('Date'[Date]),
        "Type","MTD",
        "Order",5
    ),
    ADDCOLUMNS(
        DATESQTD('Date'[Date]),
        "Type","QTD",
        "Order",7
    ),
    ADDCOLUMNS(
        CALENDAR(_ed-6,_ed),
        "Type","Last 7 Days",
        "Order",1
    ),
    ADDCOLUMNS(
        CALENDAR(_ed-13,_ed),
        "Type","Last 14 Days",
        "Order",3
    ),
    ADDCOLUMNS(
        CALENDAR(DATE(__Year,__MonthStart,1),DATE(__Year,__MonthEnd,__Day)),
        "Type","Last Qtr",
        "Order",8
    ),
    ADDCOLUMNS(
        CALENDAR(DATE(__PYear,1,1),DATE(__PYear,12,31)),
        "Type","Last Year",
        "Order",10
    ),
    ADDCOLUMNS(
        datesytd('Date'[Date]),
        "Type","This Year",
        "Order",9
    ),
    ADDCOLUMNS(
        CALENDAR(_sd,_ed),
        "Type","All time",
        "Order",11
    ),
    ADDCOLUMNS(
        CALENDAR(_sd,_ed),
        "Type","Custom",
        "Order",12
    ),
    ADDCOLUMNS(
        CALENDAR(DATE(__Year,__Month,1),__NextMonth),
        "Type","Last Month",
        "Order",6
    ),
     ADDCOLUMNS(
        CALENDAR(_ed-27,_ed-14),
        "Type","Previous 14 Days",
        "Order",4
    ),
     ADDCOLUMNS(
        CALENDAR(_ed-13,_ed-7),
        "Type","Previous 7 Days",
        "Order",2
    )
)
1 ACCEPTED SOLUTION
Anonymous
Not applicable

HI @MyWeeLola,

In fact, you not need to create a new table. The switch function are used to confirm the period duration which will be used in the calculate expressions. 

For example:

The MTD the period duration is month, the YTD is year, QTD is quarter.

For above scenario, you can add a variable with switch function to convert them to days, then you can use this as condition in expressions.

Regards,

Xiaoxin Sheng

View solution in original post

4 REPLIES 4
Anonymous
Not applicable

HI @MyWeeLola ,

You may need to create a measure formula with switch function to check the current date period type and redirect to the specific formula to show the previous period values.(each period has its own previous calculation expressions)

Write Conditional Statement Using SWITCH in DAX and Power BI - RADACAD

Regards,

Xiaoxin Sheng

@Anonymous 

 

Thank you for the response.

 

Would the following work in principle?

 

On the result of the switch statement, I could set a min and max date. I would then either create or filter a second date table based on those values, and then apply at date range to the second graph.

 

What would bite me using that methodology?

 

Lola

Anonymous
Not applicable

HI @MyWeeLola,

In fact, you not need to create a new table. The switch function are used to confirm the period duration which will be used in the calculate expressions. 

For example:

The MTD the period duration is month, the YTD is year, QTD is quarter.

For above scenario, you can add a variable with switch function to convert them to days, then you can use this as condition in expressions.

Regards,

Xiaoxin Sheng

@Anonymous 

 

Thank you for the response. I got it working with the second table. That you for your time. It is appreciated.

 

Lola

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.