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
Alex_LevelUp
Frequent Visitor

List of dates for previous week in time using intelligent time slicer

Hello -
I'm trying to make a table with values in a column called "Selection" for MTD, YTD, PYTD, PMTD, and WEEKLY. I'm using the concept for intelligent time slicers linked here: (https://youtu.be/AdLDYohLeJc). I'm currently hung up on trying to generate the "Weekly" Selection. I can generate the dates properly for WTD using:

VAR WeekStart =
     CALCULATE(
        TodayDate-WEEKDAY(TodayDate,3),
        FILTER(
            DISTINCT('Dates'[Date]),
            YEAR('Dates'[date])
            =YEAR(TodayDate)
     )
)
 
and then:

    ADDCOLUMNS(
        CALENDAR(WeekStart, TodayDate),
        "Selection", "Weekly",
        "Sort", 6))


However, the data I would like to filter on this specific request is trailing by 3 weeks. How would I obtain a list of dates using the calendar function for a week of dates that is 3 weeks back? Ex. if the date is 8/22/22, I would like the list of days for the week of 8/1/22-through 8/7/22. 

 

I have the extended date table available with week offsets, but I can't figure out how to use dax to get the dates I would like to display.

 

Alex_LevelUp_0-1661186862168.png

 

Thanks!

 

1 ACCEPTED SOLUTION
v-yangliu-msft
Community Support
Community Support

Hi @Alex_LevelUp ,

According to your description, According to your DAX, I can see that you want to get the date three weeks before TODAY().Right?

(1)You can use the following DAX to replace the italicized part of your error :

VAR WeekStart =

     CALCULATE(

        TodayDate-WEEKDAY(TodayDate,3)

,FILTER(

            DISTINCT('Dates'[Date]),

            YEAR('Dates'[date])

            =YEAR(TodayDate)

     )

)

VAR _pre_three_week_start =

    CALCULATE(

        DATEADD('Dates'[Date], -21, DAY),

        FILTER(

            'Dates','Dates'[Date]=WeekStart

    )

)

VAR _pre_three_week_end =

    CALCULATE(

        DATEADD('Dates'[Date], -15, DAY),

        FILTER(

           'Dates','Dates'[Date]=WeekStart

    )

)

(2)And the [Result] is :

ADDCOLUMNS(
CALENDAR(_pre_three_week_start, _pre_three_week_end),
"Selection", "WeeklyTEST",
"Sort", 7
)

(3)The Result as follows:

vyangliumsft_0-1661844080049.png

If this method cant  meet your needs, can you provide your Date table for us  so that we can help you better.

 

Best Regards,

Liu Yang

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

5 REPLIES 5
v-yangliu-msft
Community Support
Community Support

Hi @Alex_LevelUp ,

According to your description, According to your DAX, I can see that you want to get the date three weeks before TODAY().Right?

(1)You can use the following DAX to replace the italicized part of your error :

VAR WeekStart =

     CALCULATE(

        TodayDate-WEEKDAY(TodayDate,3)

,FILTER(

            DISTINCT('Dates'[Date]),

            YEAR('Dates'[date])

            =YEAR(TodayDate)

     )

)

VAR _pre_three_week_start =

    CALCULATE(

        DATEADD('Dates'[Date], -21, DAY),

        FILTER(

            'Dates','Dates'[Date]=WeekStart

    )

)

VAR _pre_three_week_end =

    CALCULATE(

        DATEADD('Dates'[Date], -15, DAY),

        FILTER(

           'Dates','Dates'[Date]=WeekStart

    )

)

(2)And the [Result] is :

ADDCOLUMNS(
CALENDAR(_pre_three_week_start, _pre_three_week_end),
"Selection", "WeeklyTEST",
"Sort", 7
)

(3)The Result as follows:

vyangliumsft_0-1661844080049.png

If this method cant  meet your needs, can you provide your Date table for us  so that we can help you better.

 

Best Regards,

Liu Yang

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Perfect! Thank you for your help!

v-yangliu-msft
Community Support
Community Support

Hi @Alex_LevelUp ,

According to your description, you want to display the date of the third week before by a certain date. Right?

Here are the steps you can follow:

(1)We can create a date table using CALENDAR() based on test table. The two tables do not need to be connected.

Date  =  CALENDAR(FIRSTDATE('test'[Date]),LASTDATE('test'[Date]))

vyangliumsft_0-1661409692979.png

(2)We put the ‘Date’[Date]  in the slice and put the ‘test’[Date] in the Table or Matrix.

vyangliumsft_1-1661409692989.png

(3)Create Measure  “isDisplay”:

isDisplay =
var _week_day= WEEKDAY(SELECTEDVALUE('Date '[Date]),3)
var _week_start= DATEADD( VALUES('Date '[Date]), -_week_day,DAY)
var _pre_three_week_start= DATEADD(_week_start,-21,DAY)
var _pre_three_week_end=DATEADD(_week_start,-15,DAY)
return
  IF( HASONEFILTER('Date '[Date]), IF( MAX('test'[Date]) >=_pre_three_week_start && MAX('test'[Date]) <= _pre_three_week_end ,1,0) ,0)

(4)We can put the “isDisplay” measure in the Table or Matrix visual filter. After it ,we configure the filter and click “Apply filter”.

vyangliumsft_2-1661409693001.png

(5)Result: When you select a date in the slice ,we can see the date of the third week before the date we selected.

vyangliumsft_3-1661409693003.png

If this method can't meet your requirement, can you share sample data and sample output in table format? Or a sample pbix after removing sensitive data. We can better understand the problem and help you.

If you need pbix, please click here.

case_02.pbix

 

Best Regards,

Liu Yang

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly

This seems really close, particulary to define the previous three week start and end date by a variable.

Below is how I'm structuring my custom table. It is simply using fields from a date table.
Once my table is built, I would use a slicer where I filter on the "selection" column in my custom table. I tried to use your variables as they were set up, but I was getting a ciruclar dependency error, so I used the FILTER & DISTINCT solution that worked on my measures. Now I'm getting a "DATEADD" must specify a column error.

The measures in question that you provided are in italics. How do I adjust those to work with the current format? The other measures work.

 

Weekly/MTD/YTD/PYTD Selection =
VAR TodayDate = TODAY()
VAR YearStart =
    CALCULATE
        (STARTOFYEAR('Dates'[Date]),
        FILTER(DISTINCT('Dates'[Date]),
        YEAR(Dates[Date]) = YEAR(TodayDate)
    )
)
VAR MonthStart =
    CALCULATE
    (STARTOFMONTH(Dates[Date]),
    FILTER(
        DISTINCT('Dates'[Date]),
        YEAR('Dates'[Date]) = YEAR (TodayDate)
        &&MONTH ('Dates'[Date]) = MONTH(TodayDate)
    )
)

VAR PYTodayDate =ble in question are in italics

    DATE(YEAR(TodayDate)-1, MONTH(TodayDate), DAY(TodayDate))
VAR PYMonthStart =
    CALCULATE(
        DATEADD(STARTOFMONTH(Dates[Date]), -1, YEAR),
        FILTER(
            DISTINCT('Dates'[Date]),
            YEAR('Dates'[Date]) -1
            = YEAR(PYTodayDate)
            &&MONTH('Dates'[Date]) = MONTH(TodayDate)
    )
 )
VAR PYstart =
    CALCULATE(
        DATEADD(STARTOFYEAR(Dates[Date]), -1, YEAR),
        FILTER(
            DISTINCT('Dates'[Date]),
            YEAR('Dates'[Date]) -1
            =YEAR(PYTodayDate)
     )
)
VAR WeekStart =
     CALCULATE(
        TodayDate-WEEKDAY(TodayDate,3),
        FILTER(
            DISTINCT('Dates'[Date]),
            YEAR('Dates'[date])
            =YEAR(TodayDate)
     )
)
var _week_day=
    CALCULATE(
        WEEKDAY(SELECTEDVALUE('Dates'[Date]),3),
        FILTER(
            DISTINCT(Dates[Date]),
            YEAR('Dates'[Date])
            =YEAR(TodayDate)
    )
)

var _week_start=
    CALCULATE(
        DATEADD( VALUES('Dates'[Date]), -_week_day,DAY),
        FILTER(
            DISTINCT(Dates[Date]),
            YEAR('Dates'[Date])
            = YEAR(TodayDate)
    )
)

VAR _pre_three_week_start =
    CALCULATE(
        DATEADD(_week_start, -21, DAY),
        FILTER(
            DISTINCT(Dates[Date]),
            DAY(Dates[Date])
    )
)

VAR _pre_three_week_end =
    CALCULATE(
        DATEADD(_week_start, -15, DAY),
        FILTER(
            DISTINCT(Dates[Date]),
            DAY(Dates[Date])
    )
)

VAR Result =
UNION (
    ADDCOLUMNS(
        CALENDAR(MIN(Dates[Date]), MAX(Dates[Date]) ),
        "Selection", "All",
        "Sort ", 1
     ),
    ADDCOLUMNS(
        CALENDAR(YearStart, TodayDate),
        "Selection", "YTD",
        "Sort", 4
    ),
    ADDCOLUMNS(
        CALENDAR(MonthStart,TodayDate),
        "Selection", "MTD",
        "Sort", 2
    ),
    ADDCOLUMNS(
        CALENDAR(PYStart, PyTodayDate),
        "Selection", "PYTD",
        "Sort", 5
    ),
    ADDCOLUMNS(
        CALENDAR(PYMonthStart, PYTodayDate),
        "Selection", "PYMTD",
        "Sort", 3
    ),
    ADDCOLUMNS(
        CALENDAR(WeekStart, TodayDate),
        "Selection", "Weekly",
        "Sort", 6
    ),
    ADDCOLUMNS(
        CALENDAR(_pre_three_week_start,_pre_three_week_end),
        "Selection", "WeeklyTEST",
        "Sort", 7
    )

RETURN
Result
Greg_Deckler
Super User
Super User

@Alex_LevelUp Sequential makes things like that easy: https://community.powerbi.com/t5/Quick-Measures-Gallery/Sequential/m-p/380231?search-action-id=25265...

 


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

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