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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
tylerdv
Frequent Visitor

Dynamic rolling charts

Hey All!

 

Im getting the hang of Power BI, but there is one crucial time saving thing i am missing

 

I used to use excel's 'Offset' function with a name range to create dynamic charts/graphs that would automatically add a weeks worth of data as it got added.

 

I've created a column in my data using the 'Endofweek' funtion, now i need a way for a visulisation to give me -6 weeks of data based on this.

 

Ive seen a lot of answers that go back 42 days, but im focusing on weeks, and this makes it a bit tricky

 

Any help would be much appreciated

 

Thanks

Tyler

1 ACCEPTED SOLUTION

Hi @tylerdv,

 

I assume you have a table with a flag of weeks you want roll back like 'FlagTable'[Flag].

 

Then the formula to create the measure should like below.Smiley Happy

Rolling n Week Sales =
VAR currentWeek =
    MAX ( 'CalendarTable'[WeekNumber] )
VAR currentYear =
    MAX ( 'CalendarTable'[Year] )
VAR selectedFlag =
    MAX ( 'FlagTable'[Flag] )
RETURN
    IF (
        currentWeek - selectedFlag
            < 1,
        CALCULATE (
            [Total Sales],
            FILTER (
                ALL ( CalendarTable ),
                'CalendarTable'[Year]
                    = currentYear - 1
                    && 'CalendarTable'[WeekNumber]
                    = 52 + currentWeek
                    - selectedFlag
            )
        ),
        CALCULATE (
            [Total Sales],
            FILTER (
                ALL ( CalendarTable ),
                'CalendarTable'[Year] = currentYear
                    && 'CalendarTable'[WeekNumber]
                    = currentWeek - selectedFlag
            )
        )
    )

 

Regard

View solution in original post

4 REPLIES 4
Phil_Seamark
Employee
Employee

Please add the following calculated column to your date table

 

Weeks from today = IFERROR(DATEDIFF('Dates'[Date],NOW(),WEEK),-1)

Then you can drag this field into your Report, Page or Visual level filter and set to be between 0 and 6 (if you want to show the last rolling 6 weeks)


To learn more about DAX visit : aka.ms/practicalDAX

Proud to be a Datanaut!

v-ljerr-msft
Employee
Employee

Hi @tylerdv,

 

I assume that you already have the Year and WeekNumber columns in your Calendar table. If not, you can use the formula below to add them first.

Year = YEAR ( 'CalendarTable'[Date] )

WeekNumber = WEEKNUM ( 'CalendarTable'[Date] )

Then you should be able to use the formula below to create a measure to calculate -6 weeks of sales, and show the measure on the chart with your Date column on the report.Smiley Happy

Rolling 6 Week Sales =
VAR currentWeek =
    MAX ( 'CalendarTable'[WeekNumber] )
VAR currentYear =
    MAX ( 'CalendarTable'[Year] )
RETURN
    IF (
        currentWeek - 6
            < 1,
        CALCULATE (
            [Total Sales],
            FILTER (
                ALL ( CalendarTable ),
                'CalendarTable'[Year]
                    = currentYear - 1
                    && 'CalendarTable'[WeekNumber]
                    = 52 + currentWeek
                    - 6
            )
        ),
        CALCULATE (
            [Total Sales],
            FILTER (
                ALL ( CalendarTable ),
                'CalendarTable'[Year] = currentYear
                    && 'CalendarTable'[WeekNumber]
                    = currentWeek - 6
            )
        )
    )

 

Regards

Hi @v-ljerr-msft thanks for your response!

 

How would the bellow code work for it to create a flag that i can then filter on for a visual?

 

Thanks for you help

 

Thanks

T

 

Hi @tylerdv,

 

I assume you have a table with a flag of weeks you want roll back like 'FlagTable'[Flag].

 

Then the formula to create the measure should like below.Smiley Happy

Rolling n Week Sales =
VAR currentWeek =
    MAX ( 'CalendarTable'[WeekNumber] )
VAR currentYear =
    MAX ( 'CalendarTable'[Year] )
VAR selectedFlag =
    MAX ( 'FlagTable'[Flag] )
RETURN
    IF (
        currentWeek - selectedFlag
            < 1,
        CALCULATE (
            [Total Sales],
            FILTER (
                ALL ( CalendarTable ),
                'CalendarTable'[Year]
                    = currentYear - 1
                    && 'CalendarTable'[WeekNumber]
                    = 52 + currentWeek
                    - selectedFlag
            )
        ),
        CALCULATE (
            [Total Sales],
            FILTER (
                ALL ( CalendarTable ),
                'CalendarTable'[Year] = currentYear
                    && 'CalendarTable'[WeekNumber]
                    = currentWeek - selectedFlag
            )
        )
    )

 

Regard

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

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

April Fabric Community Update

Fabric Community Update - April 2024

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

Top Solution Authors