cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
M_Wavemaker Frequent Visitor
Frequent Visitor

Dynamic comparison of time periods

Hello,

 

I am trying to show the data for the same week of the previous year (which I have managed), however I would like to be able filter this by Country. Currently, if I select a country using a slicer, it does not dynamically change my 'Sessions (LY)'. My formula for this is:

 

Sessions (LY) = 
    VAR CurrentWeek = SELECTEDVALUE(LandingPagedata[WeekNo])
    VAR CurrentYear = SELECTEDVALUE(LandingPagedata[Year])

    RETURN
    CALCULATE( SUM(LandingPagedata[Sessions]),
        FILTER( ALL(LandingPagedata),
            LandingPagedata[WeekNo] = CurrentWeek &&
            LandingPagedata[Year] = CurrentYear -1 ))

Can someone please advise how I make this dynamic for Country? Sorry - I've tried everything I know, I'm very limited in DAX knowledge.

 

Thanks

M

1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
Community Support Team
Community Support Team

Re: Dynamic comparison of time periods

HI, @M_Wavemaker 

Sessions (LY) = 
    VAR CurrentWeek = SELECTEDVALUE(LandingPagedata[WeekNo])
    VAR CurrentYear = SELECTEDVALUE(LandingPagedata[Year])

    RETURN
    CALCULATE( SUM(LandingPagedata[Sessions]),
        FILTER( ALL(LandingPagedata),
            LandingPagedata[WeekNo] = CurrentWeek &&
            LandingPagedata[Year] = CurrentYear -1 ))

For ALL(LandingPagedata), this returns all the rows in a whole table,  ignoring any filters that might have been applied

So just adjust it as below:

New Sessions (LY) 1 = 
    VAR CurrentWeek = SELECTEDVALUE(LandingPagedata[WeekNo])
    VAR CurrentYear = SELECTEDVALUE(LandingPagedata[Year])

    RETURN
    CALCULATE( SUM(LandingPagedata[Sessions]),
        FILTER( ALL(LandingPagedata[WeekNo],LandingPagedata[Year]),
            LandingPagedata[WeekNo] = CurrentWeek &&
            LandingPagedata[Year] = CurrentYear -1 ))
New Sessions (LY) 2 = 
    VAR CurrentWeek = SELECTEDVALUE(LandingPagedata[WeekNo])
    VAR CurrentYear = SELECTEDVALUE(LandingPagedata[Year])

    RETURN
    CALCULATE( SUM(LandingPagedata[Sessions]),
        FILTER( ALLEXCEPT(LandingPagedata,LandingPagedata[Country]),
            LandingPagedata[WeekNo] = CurrentWeek &&
            LandingPagedata[Year] = CurrentYear -1 ))

Then it will work well.

 

Best Regards,

Lin

Community Support Team _ Lin
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
amitchandak Super Contributor
Super Contributor

Re: Dynamic comparison of time periods

Change this

ALL(LandingPagedata)

Try to restrict it to week and year as per need

Sessions (LY) = 
    VAR CurrentWeek = SELECTEDVALUE(LandingPagedata[WeekNo])
    VAR CurrentYear = SELECTEDVALUE(LandingPagedata[Year])

    RETURN
    CALCULATE( SUM(LandingPagedata[Sessions]),
        FILTER( ALL(LandingPagedata[WeekNo]),
            LandingPagedata[WeekNo] = CurrentWeek &&
            LandingPagedata[Year] = CurrentYear -1 ))

Or

Sessions (LY) = 
    VAR CurrentWeek = SELECTEDVALUE(LandingPagedata[WeekNo])
    VAR CurrentYear = SELECTEDVALUE(LandingPagedata[Year])

    RETURN
    CALCULATE( SUM(LandingPagedata[Sessions]),all(LandingPagedata[WeekNo]),all(LandingPagedata[Year]),
        FILTER( LandingPagedata,
            LandingPagedata[WeekNo] = CurrentWeek &&
            LandingPagedata[Year] = CurrentYear -1 ))
Highlighted
Community Support Team
Community Support Team

Re: Dynamic comparison of time periods

HI, @M_Wavemaker 

Sessions (LY) = 
    VAR CurrentWeek = SELECTEDVALUE(LandingPagedata[WeekNo])
    VAR CurrentYear = SELECTEDVALUE(LandingPagedata[Year])

    RETURN
    CALCULATE( SUM(LandingPagedata[Sessions]),
        FILTER( ALL(LandingPagedata),
            LandingPagedata[WeekNo] = CurrentWeek &&
            LandingPagedata[Year] = CurrentYear -1 ))

For ALL(LandingPagedata), this returns all the rows in a whole table,  ignoring any filters that might have been applied

So just adjust it as below:

New Sessions (LY) 1 = 
    VAR CurrentWeek = SELECTEDVALUE(LandingPagedata[WeekNo])
    VAR CurrentYear = SELECTEDVALUE(LandingPagedata[Year])

    RETURN
    CALCULATE( SUM(LandingPagedata[Sessions]),
        FILTER( ALL(LandingPagedata[WeekNo],LandingPagedata[Year]),
            LandingPagedata[WeekNo] = CurrentWeek &&
            LandingPagedata[Year] = CurrentYear -1 ))
New Sessions (LY) 2 = 
    VAR CurrentWeek = SELECTEDVALUE(LandingPagedata[WeekNo])
    VAR CurrentYear = SELECTEDVALUE(LandingPagedata[Year])

    RETURN
    CALCULATE( SUM(LandingPagedata[Sessions]),
        FILTER( ALLEXCEPT(LandingPagedata,LandingPagedata[Country]),
            LandingPagedata[WeekNo] = CurrentWeek &&
            LandingPagedata[Year] = CurrentYear -1 ))

Then it will work well.

 

Best Regards,

Lin

Community Support Team _ Lin
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

Helpful resources

Announcements
October 2019 Community Highlights

October 2019 Community Highlights

October was a busy month in the community. Read the recap article to learn about some of the events and content.

New Solution Badges

New Solution Badges

Two waves of brand new solution badges are coming! Read the article for more information on our new community badges.

Ask Amir Anything

Exclusive LIVE Community Event No. 2 – Ask Amir Anything

Next in our Triple A series: Ask Amir Netz questions about the latest updates, features and future.

Analytics in Azure virtual event

Analytics in Azure virtual event

Experience a limitless analytics service built to ingest, prep, manage, and serve data for immediate use in Power BI.

Users Online
Currently online: 145 members 1,646 guests
Please welcome our newest community members: