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

Get certified in Microsoft Fabric—for free! For a limited time, get a free DP-600 exam voucher to use by the end of 2024. Register now

Reply
Anonymous
Not applicable

Changing date range on the chart based on slicer selection

Hi,

 

I would like to be able to change the date range on a chart based on a slicer.

This would be similar to relative time slicer but I want to be able to define the time periods in the table and then use them in a slicer:

 

image.png

 

So if I select "Last 3 Days" my chart shows data for 3 days only, if I select "Last Month" my chart shows data for last month...

 

What is the best way to achieve that?

1 ACCEPTED SOLUTION
v-yulgu-msft
Microsoft Employee
Microsoft Employee

Hi @Anonymous ,

 

Create an extra table to list all avialble date periods for selection, which is unrelated to source data table.

1.PNG

 

Add below measure into visual level filter.

Measure =
VAR selectedperiod =
    SELECTEDVALUE ( 'Date range'[Period] )
VAR last3days =
    IF (
        MAX ( Test_1[Date] ) <= TODAY ()
            && MAX ( Test_1[Date] )
                > TODAY () - 3,
        1,
        0
    )
VAR last6days =
    IF (
        MAX ( Test_1[Date] ) <= TODAY ()
            && MAX ( Test_1[Date] )
                > TODAY () - 6,
        1,
        0
    )
VAR lastweek =
    IF (
        YEAR ( MAX ( Test_1[Date] ) ) = YEAR ( TODAY () )
            && WEEKNUM ( MAX ( Test_1[Date] ), 2 )
                = WEEKNUM ( TODAY (), 2 ) - 1,
        1,
        0
    )
VAR last12days =
    IF (
        MAX ( Test_1[Date] ) <= TODAY ()
            && MAX ( Test_1[Date] )
                > TODAY () - 12,
        1,
        0
    )
VAR last15days =
    IF (
        MAX ( Test_1[Date] ) <= TODAY ()
            && MAX ( Test_1[Date] )
                > TODAY () - 15,
        1,
        0
    )
VAR lastmonth =
    IF (
        MONTH ( TODAY () ) <> 1,
        IF (
            YEAR ( MAX ( Test_1[Date] ) ) = YEAR ( TODAY () )
                && MONTH ( MAX ( Test_1[Date] ) )
                    = MONTH ( TODAY () ) - 1,
            1,
            0
        ),
        IF (
            YEAR ( MAX ( Test_1[Date] ) )
                = YEAR ( TODAY () ) - 1
                && MONTH ( MAX ( Test_1[Date] ) ) = 12,
            1,
            0
        )
    )
RETURN
    IF (
        selectedperiod = "Last 3 days",
        last3days,
        IF (
            selectedperiod = "Last 6 days",
            last6days,
            IF (
                selectedperiod = "Last Week",
                lastweek,
                IF (
                    selectedperiod = "Last 12 days",
                    last12days,
                    IF (
                        selectedperiod = "Last 15 days",
                        last15days,
                        IF ( selectedperiod = "Last Month", lastmonth, 0 )
                    )
                )
            )
        )
    )

2.PNG3.PNG

Best regards,

Yuliana Gu

Community Support Team _ Yuliana Gu
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

2 REPLIES 2
v-yulgu-msft
Microsoft Employee
Microsoft Employee

Hi @Anonymous ,

 

Create an extra table to list all avialble date periods for selection, which is unrelated to source data table.

1.PNG

 

Add below measure into visual level filter.

Measure =
VAR selectedperiod =
    SELECTEDVALUE ( 'Date range'[Period] )
VAR last3days =
    IF (
        MAX ( Test_1[Date] ) <= TODAY ()
            && MAX ( Test_1[Date] )
                > TODAY () - 3,
        1,
        0
    )
VAR last6days =
    IF (
        MAX ( Test_1[Date] ) <= TODAY ()
            && MAX ( Test_1[Date] )
                > TODAY () - 6,
        1,
        0
    )
VAR lastweek =
    IF (
        YEAR ( MAX ( Test_1[Date] ) ) = YEAR ( TODAY () )
            && WEEKNUM ( MAX ( Test_1[Date] ), 2 )
                = WEEKNUM ( TODAY (), 2 ) - 1,
        1,
        0
    )
VAR last12days =
    IF (
        MAX ( Test_1[Date] ) <= TODAY ()
            && MAX ( Test_1[Date] )
                > TODAY () - 12,
        1,
        0
    )
VAR last15days =
    IF (
        MAX ( Test_1[Date] ) <= TODAY ()
            && MAX ( Test_1[Date] )
                > TODAY () - 15,
        1,
        0
    )
VAR lastmonth =
    IF (
        MONTH ( TODAY () ) <> 1,
        IF (
            YEAR ( MAX ( Test_1[Date] ) ) = YEAR ( TODAY () )
                && MONTH ( MAX ( Test_1[Date] ) )
                    = MONTH ( TODAY () ) - 1,
            1,
            0
        ),
        IF (
            YEAR ( MAX ( Test_1[Date] ) )
                = YEAR ( TODAY () ) - 1
                && MONTH ( MAX ( Test_1[Date] ) ) = 12,
            1,
            0
        )
    )
RETURN
    IF (
        selectedperiod = "Last 3 days",
        last3days,
        IF (
            selectedperiod = "Last 6 days",
            last6days,
            IF (
                selectedperiod = "Last Week",
                lastweek,
                IF (
                    selectedperiod = "Last 12 days",
                    last12days,
                    IF (
                        selectedperiod = "Last 15 days",
                        last15days,
                        IF ( selectedperiod = "Last Month", lastmonth, 0 )
                    )
                )
            )
        )
    )

2.PNG3.PNG

Best regards,

Yuliana Gu

Community Support Team _ Yuliana Gu
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Hi Yuliana,

Can you please explain "Add below measure into visual level filter.".

Can you please review with the current version of Power BI desktop.

 

Regards'

Parvez

Helpful resources

Announcements
November Carousel

Fabric Community Update - November 2024

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

Live Sessions with Fabric DB

Be one of the first to start using Fabric Databases

Starting December 3, join live sessions with database experts and the Fabric product team to learn just how easy it is to get started.

Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Early Bird pricing ends December 9th.

Nov PBI Update Carousel

Power BI Monthly Update - November 2024

Check out the November 2024 Power BI update to learn about new features.