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
harshaltannu
Regular Visitor

DAX flag for Last Completed Week (filter)

Hello DAX gurus

 

I'm trying to create a filter in the DimDate table (my data table), which would put the flag as 1 when the data is in last completed week. This table sits on a SQL server, which I connect via Direct Query in Power BI. A week runs from Thursday to Wednesday. Last completed week would be 1 only when we go into Thursday of this week. See examples below. 

 

  1. When Today is April 23, 2018 (to April 25, 2018) - Last Completed Week is - April 12, 2018 to April 18, 2018 
  2. When Today is April 26, 2018 (to May 02, 2018) - Last Completed Week is - April 19, 2018 to April 25, 2018

 

In my date table, I was able to devise CurrentWeek, LastWeek, LastLastWeek filters. Also, I'm able to devise Last Thursday to This Wednesday. What I'm unable to devise is the Last Completed Week that matches the above logic. 

 

Any help/directions you have for me would be awesome. 

 

Cheers,

Harshal

1 ACCEPTED SOLUTION
v-jiascu-msft
Employee
Employee

Hi Harshal.

 

There are some limitations of applying DAX functions in the DirectQuery mode. Try out the following formula as a calculated column please.

BTW, the dates in your sample aren't continuous. I would suggest you use a complete date table instead.

 

LastWholeWeek =
VAR currentWeek =
    WEEKNUM ( TODAY (), 1 )
VAR weekNum =
    IF ( [Weekday] IN { 5, 6, 7 }, WEEKNUM ( [Date], 1 ) + 1, WEEKNUM ( [Date] ) )
RETURN
    IF ( weekNum = currentWeek - 1, 1, BLANK () )

DAX flag for Last Completed Week.png

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Best Regards,

Dale

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

9 REPLIES 9
v-jiascu-msft
Employee
Employee

Hi Harshal,

 

Did it solve your issue?

 

Best Regards,

Dale

Community Support Team _ Dale
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
v-jiascu-msft
Employee
Employee

Hi Harshal.

 

There are some limitations of applying DAX functions in the DirectQuery mode. Try out the following formula as a calculated column please.

BTW, the dates in your sample aren't continuous. I would suggest you use a complete date table instead.

 

LastWholeWeek =
VAR currentWeek =
    WEEKNUM ( TODAY (), 1 )
VAR weekNum =
    IF ( [Weekday] IN { 5, 6, 7 }, WEEKNUM ( [Date], 1 ) + 1, WEEKNUM ( [Date] ) )
RETURN
    IF ( weekNum = currentWeek - 1, 1, BLANK () )

DAX flag for Last Completed Week.png

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Best Regards,

Dale

Community Support Team _ Dale
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
v-jiascu-msft
Employee
Employee

Hi Harshal,

 

Please try out this measure. It works in my test Direct Query to SQL Server.

LastCustomWeek =
VAR lastWed =
    CALCULATE (
        MAX ( DateDimension[Date] ),
        FILTER (
            ALL ( 'DateDimension' ),
            'DateDimension'[Date] <= TODAY ()
                && 'DateDimension'[Weekday] = 4
        )
    )
RETURN
    IF (
        MIN ( 'DateDimension'[Date] ) <= lastWed
            && MIN ( 'DateDimension'[Date] )
                >= lastWed - 6,
        1,
        BLANK ()
    )

DAX_flag_for_Last_Completed_Week1

 

Best Regards,

Dale

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

Thank you both for your replies, appreciate it. 

 

Dale, the measure you created works like a charm. I had to adjust the criteria to make the switch day as Thursday (instead of Wednesday, as you had). Below is the updated measure. Is there a way that this calculation can be done in a Calculated Column instead? I need to add this new calculation as a page level filter. So, when the users log into the report, they see the week selected automatically, but they also have an option of changing the dates if need be. 

 

LastCustomWeek =
VAR lastWed =
CALCULATE (
MAX ( 'Date'[DATE_] ),
FILTER (
ALL ( 'Date' ),
'Date'[DATE_] <= TODAY ()
&& 'Date'[Date Day] = 5
)
)
RETURN
IF (
MIN ( 'Date'[DATE_] ) < lastWed
&& MIN ( 'Date'[DATE_] )
>= lastWed - 7,
1,
BLANK ()
)

 

Thanks,

Harshal

ChandeepChhabra
Impactful Individual
Impactful Individual

Hi @harshaltannu

 

Can you please share a sample of your data ? I can give it a shot

@ChandeepChhabra: how can I send you the sample data file? 

@harshaltannu Upload it on dropbox / google / onedrive and share the link

@harshaltannu : Can you also share a sample of the calculated column (output) you are expecting ?

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.