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
rax99
Helper V
Helper V

Add condition to calculated column based on filter applied

Consider the following table:

 

Table1

CustomerDateNDayNo.CallIDAgentHandledFlag
A23/09/201801549870
A24/09/201811549880
A24/09/201811549891
B01/09/201801549900
B27/09/2018261549911
C14/09/201801549920
C26/09/2018121549931
C29/09/2018151549941
D29/09/201801549950
D30/09/201811549961

 

NDayNo. = Normalised day

So 0 = first time they contacted in the month.

eg Customer A first called on 23rd (=0) then again on the 4th(NdayNo or difference from 0 =1)

     Customer B first called on 1st (=0), then again on the 27th (NdayNo or difference from 0=26 )

     Customer C first called on 14th (=0), then again on the 26th (NdayNo or difference from 0 =12), then again on 29th (NdayNo or difference from 0 =15)

 

Now, I have added a filter that filters on AgentHandleFlag column (0, and/or 1)

 

Upon filtering, the resulting table appears like this:

 

Table2

CustomerDateNDayNo.CallIDAgentHandledFlag**DesiredNDayNo.**
A24/09/2018115498910
B27/09/20182615499110
C26/09/20181215499310
C29/09/20181515499413
D30/09/2018115499610

 

Now that agenthandledflag =0 rows have been removed, I need the NDayNo to re-adjust based on the new result set. So I need a new calc column that adjusts the NDayNo. so that its now based on the filtered results. (See the DesiredNDayNo. column)

 

eg. Customer A Calls on the 24th for the first time (NDayNo. now = 0), etc.

 

Is there anyway we can add conditions to calculated columns based on filters applied?

 

So from Table 1, the condition would be, if AgentHandleFlag = 0 and I change the AgentHandleFlag_filter to 1, then adjust the NdayNo. to be based on the new dataset (which is DesiredNDayNo. in Table 2).

 

I hope I have made this clear, but let me know if further clarification is required. Thanks

2 REPLIES 2
v-yulgu-msft
Employee
Employee

Hi @rax99,

 

Please try this measure:

previous date =
IF (
    SELECTEDVALUE ( Table2[Date] )
        = CALCULATE (
            MIN ( Table2[Date] ),
            FILTER (
                ALLSELECTED ( Table2 ),
                Table2[Customer] = SELECTEDVALUE ( Table2[Customer] )
                    && Table2[Date].[MonthNo] = MONTH ( SELECTEDVALUE ( Table2[Date] ) )
            )
        ),
    0,
    DATEDIFF (
        CALCULATE (
            MIN ( Table2[Date] ),
            FILTER (
                ALLSELECTED ( Table2 ),
                Table2[Customer] = SELECTEDVALUE ( Table2[Customer] )
                    && Table2[Year-Month] = SELECTEDVALUE ( Table2[Year-Month] )
                    && Table2[Date] < MAX ( Table2[Date] )
            )
        ),
        MAX ( Table2[Date] ),
        DAY
    )
)

1.PNG2.PNG

 

Best regards,

Yuliana Gu

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

If you wish to do it via calculated column then try like this:

 

 

NDayNo Flag Based =
MAX (
    0,
    CALCULATE (
        MIN ( 'Table'[Date] ),
        ALLEXCEPT (
            'Table',
            'Table'[Customer],
            'Table'[AgentHandledFlag]
        ),
        'Table'[Date] > EARLIER ( 'Table'[Date] )
    ) - 'Table'[Date]
)

 


 


Did I answer your question correctly? Mark my answer as a solution!


Proud to be a Datanaut!  

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.