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

Earn the coveted Fabric Analytics Engineer certification. 100% off your exam for a limited time only!

Reply
Anonymous
Not applicable

Calculated Column based on multiple filter criteria

Hello,

How to create a calculated column flagging whether or not a particular calendar Week is shared between two calendar months or not (end of month scenario), without considering Saturdays and Sundays.

I want to try with approach of distinct count of Month numbers for a Week - If it is 2, then YES, else NO.

Example:
Week 13 is shared between March and April discounting Sat and Sun, thus Yes is needed.
Week 17 is Not shared between April and May discounting Sat and Sun, thus No is needed.
Table Dim_Date

DateWeek NumberMonth NumberWeekdayCalculate_Week shared between months?
04-Apr-21134SunYes
03-Apr-21134SatYes
02-Apr-21134FriYes
01-Apr-21134ThuYes
31-Mar-21133WedYes
30-Mar-21133TueYes
29-Mar-21133MonYes
     
02-May-21175SunNo
01-May-21175SatNo
30-Apr-21174FriNo
29-Apr-21174ThuNo
28-Apr-21174WedNo
27-Apr-21174TueNo
26-Apr-21174MonNo

 

I tried below code but it resulted into "No" for Week 13 above too. It does look fishy to me but unable to fix this. Any help would be great.

 

Calculate_Week shared between months? = 
IF(
       CALCULATE(
           CALCULATE(DISTINCTCOUNT(Dim_Date[Month Number]), 
                     ALLEXCEPT(Dim_Date,Dim_Date[Week Number])),
            FILTER(Dim_Date,
                   OR( Dim_Date[Weekday] <> "Sun", Dim_Date[Weekday] <> "Sat"))
       )     
 = 2, "Yes", "No"
 )

 

Thanks

Nirmit

 

1 ACCEPTED SOLUTION
amitchandak
Super User
Super User

@Anonymous ,


Var _week = [Week]
return
if(calculate(distinctCOUNT(Dim_Date[Month Number]), filter(Dim_Date, Dim_Date[Week] =_week && not( Dim_Date[Weekday] in{"Sun", "Sat"}))) =2, "Yes", "No")

View solution in original post

4 REPLIES 4
Anonymous
Not applicable

Hi @v-eqin-msft 

Many thanks for taking time out to explain this with example. Indeed it should be the inner ALLEXCEPT() removing all other context filters. Though I do need to understand these nuances in more detail!

Regards

Nirmit

v-eqin-msft
Community Support
Community Support

Hi @Anonymous ,

 

Based on my test, if you use the following formula to modify your second method, the result is correct.

 

Column =
VAR _week = [Week Number]

RETURN
    IF (
        CALCULATE (
            CALCULATE (
                DISTINCTCOUNT ( Dim_Date[Month Number] ),
                FILTER ( 'Dim_Date', 'Dim_Date'[Week Number] = _week )
            ),
            FILTER ( Dim_Date, NOT ( Dim_Date[Weekday] IN { "Sun", "Sat" } ) )
        ) = 2,
        "Yes",
        "No"
    )

 

 

Calculated Column based on multiple filter criteria.PNG

 

So I think it could be related to the context with ALLEXCEPT() —— The two groups of expression consider or ignore the filters made on related tables, respectively.As the official document said, ALLEXCEPT() removes all context filters in the table except filters that have been applied to the specified columns.

 

For more details, please refer to thread:

https://community.powerbi.com/t5/Desktop/Filter-and-Allexcept/td-p/76403

https://www.sqlbi.com/articles/order-of-evaluation-in-calculate-parameters/

 

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

Anonymous
Not applicable

Hi @amitchandak 

Thanks much for quick help. Really appreciate. It works exactly like required.

I used the FILTER syntax you provided and sourrounded it with ALLEXCEPT- this works
But ALLEXCEPT sourrounded by FILTER - this does not work 

Would be very helpful if you could explain a bit on this. Thanks in advance.


This works:

 

Week shared between months = 
IF(
       CALCULATE(
           CALCULATE
                    ( 
                    DISTINCTCOUNT(Dim_Date[Month Number]), 
                    FILTER(Dim_Date, NOT (Dim_Date[Weekday] IN {"Sun", "Sat"}))
                     ),
                    ALLEXCEPT(Dim_Date,Dim_Date[Week])
                )
 = 2, "Yes", "No"
 )

 

This does NOT work. (Result of IF is 12 always)

 

IF(
       CALCULATE(
           CALCULATE
                    ( 
                    DISTINCTCOUNT(Dim_Date[Month Number]), 
                    ALLEXCEPT(Dim_Date,Dim_Date[Week])
                     ),
                   FILTER(Dim_Date, NOT (Dim_Date[Weekday] IN {"Sun", "Sat"}))
                )
 = 2, "Yes", "No"
 )

 

Thanks

Nirmit

amitchandak
Super User
Super User

@Anonymous ,


Var _week = [Week]
return
if(calculate(distinctCOUNT(Dim_Date[Month Number]), filter(Dim_Date, Dim_Date[Week] =_week && not( Dim_Date[Weekday] in{"Sun", "Sat"}))) =2, "Yes", "No")

Helpful resources

Announcements
April AMA free

Microsoft Fabric AMA Livestream

Join us Tuesday, April 09, 9:00 – 10:00 AM PST for a live, expert-led Q&A session on all things Microsoft Fabric!

March Fabric Community Update

Fabric Community Update - March 2024

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