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
Anand24
Super User
Super User

Filter Last 1 or 2 Days based weekday

Hello Experts,

 

I have a scenario wherein i want to filter the data on date column based on current day.

 

Scenario:

If refreshing on Monday, show data in table visualization for last 2 days(Sunday and Saturday) of date column.

If refreshing on any other day(Tuesday to Sunday), load data for last 1 day of date column.

 

Input Data

Record_DateCategory
21-08-2020a
21-08-2020b
21-08-2020c
22-08-2020d
22-08-2020e
22-08-2020f
23-08-2020g
23-08-2020h
23-08-2020j
24-08-2020i
24-08-2020j
24-08-2020k
25-08-2020l
25-08-2020m
25-08-2020n

 

Expected Output:

1. If report refreshes on 24th August(Monday), show data in table visualization for last 2 days, 23rd August(Sunday) and 22nd August(Saturday)

Record_DateCategory
22-08-2020d
22-08-2020e
22-08-2020f
23-08-2020g
23-08-2020h
23-08-2020j

 

2. If report refreshes on any other day, say 25th August(Tuesday), show data in table visualization for last 1 day, 24th August(Monday)

Record_DateCategory
24-08-2020i
24-08-2020j
24-08-2020k

 

Note:

1. Data is fully loaded, i.e., for all dates and not just for last 1 or 2 days.

2. New column or measure using DAX is preferable instead of Power Query changes

 

Thanks is advance !!! 🙂

1 ACCEPTED SOLUTION
mahoneypat
Employee
Employee

Please try this measure expression

 

 

Result =
VAR thisday =
    TODAY ()
VAR days =
    IF ( WEEKDAY ( thisday ) = 2, 2, 1 )
RETURN
    CALCULATE (
        [Your Measure],
        FILTER (
            ALL ( Table[Record_Date] ),
            Table[Record_Date] >= thisday - days
                && Table[Record_Date] < thisday
        )
    )

 

 

If this works for you, please mark it as the solution.  Kudos are appreciated too.  Please let me know if not.

Regards,

Pat





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


View solution in original post

5 REPLIES 5
v-kelly-msft
Community Support
Community Support

Hi @Anand24 ,

 

First create a table as below:

Table 2 = VALUES('Table'[Record_Date])

Then create a measure as below:

_Fiterdate = 
var _weekday=CALCULATE(MAX('Table'[Weekday]),FILTER(ALL('Table'),'Table'[Record_Date]=SELECTEDVALUE('Table 2'[Record_Date])))
var _date1=CALCULATETABLE(VALUES('Table'[Record_Date]),FILTER(ALL('Table'),'Table'[Record_Date]=SELECTEDVALUE('Table 2'[Record_Date])-1||'Table'[Record_Date]=SELECTEDVALUE('Table 2'[Record_Date])-2))
var _date2=CALCULATETABLE(VALUES('Table'[Record_Date]),FILTER(ALL('Table'),'Table'[Record_Date]=SELECTEDVALUE('Table 2'[Record_Date])-1))
Return
IF(_weekday=1 && MAX('Table'[Record_Date]) in _date1,MAX('Table'[Record_Date]),IF(_weekday<>1&&MAX('Table'[Record_Date]) in _date2,MAX('Table'[Record_Date]),BLANK()))

And you will see:

1.png

For the related .pbix file,pls see attached.

 

Best Regards,
Kelly
Did I answer your question? Mark my post as a solution!
mahoneypat
Employee
Employee

Please try this measure expression

 

 

Result =
VAR thisday =
    TODAY ()
VAR days =
    IF ( WEEKDAY ( thisday ) = 2, 2, 1 )
RETURN
    CALCULATE (
        [Your Measure],
        FILTER (
            ALL ( Table[Record_Date] ),
            Table[Record_Date] >= thisday - days
                && Table[Record_Date] < thisday
        )
    )

 

 

If this works for you, please mark it as the solution.  Kudos are appreciated too.  Please let me know if not.

Regards,

Pat





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


Awesome !!!

Thank You @mahoneypat !!! That worked in one go !!!

SteveCampbell
Memorable Member
Memorable Member

You can use a DAX date table, such as this one from https://powerbi.tips/2017/11/creating-a-dax-calendar/

 

Dates 5 =
  GENERATE (
    CALENDAR( DATE( YEAR( TODAY() ) - 2, MONTH( TODAY() ), DAY( TODAY()) ), TODAY()),
    VAR startOfWeek = 1 // Where 1 is Sunday and 7 is Saturday, thus a 3 would be Tuesday    
    VAR currentDay = [Date]
    VAR days = DAY( currentDay )
    VAR months = MONTH ( currentDay )
    VAR years = YEAR ( currentDay )
    VAR nowYear = YEAR( TODAY() )
    VAR nowMonth = MONTH( TODAY() )
    VAR dayIndex = DATEDIFF( currentDay, TODAY(), DAY) * -1
    VAR todayNum = WEEKDAY( TODAY() )
    VAR weekIndex = INT( ROUNDDOWN( ( dayIndex + -1 * IF( todayNum + startOfWeek <= 6, todayNum + startOfWeek, todayNum + startOfWeek - 7 )) / 7, 0 ) )
  RETURN ROW (
    "day", days,
    "month", months,
    "year", years,
    "day index", dayIndex,
    "week index", weekIndex,
    "month index", INT( (years - nowYear ) * 12 + months - nowMonth ),
    "year index", INT( years - nowYear )
  )
)

 

Then you can filter on the day index for -1 and -2



Did I answer your question? Mark my post as a solution! Proud to be a Super User!


Connect with me!
Stay up to date on  
Read my blogs on  



Hi @SteveCampbell ,

Thank you for the response !

I'm afraid this won't work in my case. This table is leaving values for weekends whereas i still need them in my case. Also, I'm not sure how will I conditionally filter my data on weekday using this.

 

My scenario is pretty straight forward:

IF today's Weekday = Monday, THEN Return data for last 2 days(saturday and friday)

Else Return data for last 1 day

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.