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.

v-kongfanf-msft

Dynamic rollback of the previous N weeks of data

Scenario:

In this article, we'd like to create a report to display some date-related data and let users switch which previous weeks of data they want to see.
The detailed requirements are as follows:
1. Dynamically roll back to display the previous N weeks of data. Use a slicer to pick a week number for N.
2. Summarize the sum of the previous N weeks of data into a Monday display.
3. Dynamically select holidays in different countries and exclude the values of weekends and holidays from the summary.

 

Expected Result:

We have three slicers in the report: country, week number and Date. If country selects China, number selects 4, and Date selects 1/30/2023, it means to roll back 4 weeks of data from 1/30/2023 and aggregate the weekly data into each Monday. And the summary should exclude data generated by Saturdays, Sundays, and holidays within the current date range. For example, the portion of the red box is the eligible data, of which 1/2/ 2023 is the Chinese New Year holiday.

 

Raw Data:

vkongfanfmsft_0-1713338063888.png

 

Expected:

vkongfanfmsft_1-1713334201269.png

 

Sample Data:

In the sample data, there is a product table(All data for 2023).

vkongfanfmsft_1-1713336369695.png

vkongfanfmsft_2-1713336377679.png

 

How:

Steps:

1. For data preparation, get the Chinese and US 2023 holidays from the web and get the dates of WeekDays and prepare the data table for slicer generation. Prepare the following tables:

 

 

Date = VALUES('Product'[Date])  // Creating a virtual Date table to generate slicers

 

 

vkongfanfmsft_8-1713334586036.png

 

Country table:

vkongfanfmsft_9-1713334623199.png

 

PreviousWeek table:

vkongfanfmsft_10-1713334638253.png

 

AmericaHoliday table:

vkongfanfmsft_11-1713334655440.png

 

ChinaHoliday table:

vkongfanfmsft_12-1713334678403.png

 

2. Determine if the date in the date column is a holiday or is weekend based on the country selected.

 

 

WeekDay = WEEKDAY ( 'Product'[Date], 2 )

 

 

If the Date is Saturday or Sunday, return to 6 or 7:

vkongfanfmsft_13-1713334724077.png

 

If the Date is holiday, return to 1:

 

 

Is Holiday =
VAR _chooseCountry =
    SELECTEDVALUE ( Country[country] )
VAR __cur_date =
    SELECTEDVALUE ( 'Product'[Date] )
VAR __isWeekend =
    WEEKDAY ( __cur_date, 2 )
VAR _matchingCountries =
    SWITCH (
        _chooseCountry,
        "China",
            IF (
                COUNTROWS ( FILTER ( 'ChinaHoliday', 'ChinaHoliday'[Date] = __cur_date ) ) > 0,
                1,
                BLANK ()
            ),
        "America",
            IF (
                COUNTROWS ( FILTER ( 'AmericaHoliday', 'AmericaHoliday'[Date] = __cur_date ) ) > 0,
                1,
                BLANK ()
            )
    )
RETURN
    _matchingCountries

 

 

 

vkongfanfmsft_14-1713334793943.png

 

3. Determines if the current date is Monday, if so, dynamically rolls back the previous n weeks of data and aggregates the weekly data to Monday.

Create three measures as below:

 

 

Table_DateMeasure = CALCULATE ( MAX ( 'Product'[Date] ) )
IsMonday_Table =
IF ( WEEKDAY ( 'Product'[Table_DateMeasure], 2 ) = 1, 1, 0 )

 

 

 

 

Previous weeks =
IF (
    SELECTEDVALUE ( 'Product'[Date] )
        >= SELECTEDVALUE ( 'Date'[Date] )
            - SELECTEDVALUE ( PreviousWeek[number] ) * 7
        && SELECTEDVALUE ( 'Product'[Date] ) <= SELECTEDVALUE ( 'Date'[Date] ),
    1,
    IF ( NOT ISFILTERED ( 'Date'[Date] ), SELECTEDVALUE ( 'Product'[Date] ) )
)

 

 

 

Put the two measures IsMonday_Table and Previous weeks into the filter pane of the matrix and set both to 1:

vkongfanfmsft_15-1713334952995.png

 

4. Filter the dates and calculate the final sum of Absence and Accidents. Create two measures:

 

 

Sum Absence =
VAR __cur_date =
    SELECTEDVALUE ( 'Product'[Date] )
VAR __ismonday =
    WEEKDAY ( __cur_date, 2 )
VAR __start_date =
    IF ( __ismonday = 1, __cur_date )
VAR __end_date = __start_date + 6
VAR __isHoliday =
    IF ( [Is Holiday], __cur_date )
VAR __result =
    CALCULATE (
        SUM ( 'Product'[Absence] ),
        'Product'[Date] >= __start_date
            && 'Product'[Date] <= __end_date
            && 'Product'[WeekDay] <= 5
            && 'Product'[Date] <> __isHoliday
    )
RETURN
    __result

 

 

 

 

Sum Accidents =
VAR __cur_date =
    SELECTEDVALUE ( 'Product'[Date] )
VAR __ismonday =
    WEEKDAY ( __cur_date, 2 )
VAR __start_date =
    IF ( __ismonday = 1, __cur_date )
VAR __end_date = __start_date + 6
VAR __isHoliday =
    IF ( [Is Holiday], __cur_date )
VAR __result =
    CALCULATE (
        SUM ( 'Product'[Accidents] ),
        'Product'[Date] >= __start_date
            && 'Product'[Date] <= __end_date
            && 'Product'[WeekDay] <= 5
            && 'Product'[Date] <> __isHoliday
    )
RETURN
    __result

 

 

 

Put Sum Absence and Sum Accidents in the Values box of the matrix:

vkongfanfmsft_16-1713335052838.png

 

Select the matrix, and turn on the Switch values to row option in the Formatting pane:

vkongfanfmsft_17-1713335064344.png

 

Additional:

With the help of measure to determine whether the date is a holiday/weekend or not, we can avoid data redundancy by creating too many New Columns. Otherwise if we use calculated columns to mark each date, we will have these columns.

vkongfanfmsft_18-1713335088249.png

 

Author: Yaning Y.

Reviewer: Ula and Kerry