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

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
win_anthony
Resolver III
Resolver III

Circular Dependency With Date Diff Fail

The goal is to create a calculated column which will count the number of days between the last transaction and today. I currently have calculated column which will flag the most recent transaction for each store. From this calculated column, I tried to create another calculated column which will count the number of days between the last transaction flag and today using DATEDIFF. This did not work because "Circular Dependency was detected". Below are references: 1.) The flag which will tell me the latest transaction per store 2.) The failed calculated column which I tried to create 3.) Sample data with current output 4.) Sample data with expected output

 

Any advice on how I can achieve the goal of creating a calculated column which will count the number of days between the last transaction and today? Your advice is greatly appreciated.

 

1.) The flag which will tell me the latest transaction per store

 

latest_welcome_transaction_flag = 
IF( 
    CALCULATE(
        MAX( table[date] ),
        FILTER(
            VALUES( table[date] ),
            table[date] = CALCULATE( 
                                            MAX( table[date] ), 
                                            ALLEXCEPT( table, table[store_id] ) , 
                                            LEFT( table[status], 7 ) = "Welcome" 
                                            )
        )
    ) <> BLANK(),
    1
)

 

2.) The failed calculated column which I tried to create resulting in "Circular Dependency was detected"

 

# of Days Since Last Transaction.WelComeOpp = 
IF( table[latest_welcome_transaction_flag] = 1,
    DATEDIFF(
        table[date], TODAY(), DAY 
    )
)

 

3.) Sample data with current output

store_idtransaction_idstatusdatelatest_welcome_transaction_flag
1100Welcome1-Jan-21 
1101In Process3-Jan-21 
1102Canceled4-Jan-21 
1103Welcome10-Jan-211
2104Welcome2-Jan-211
3105Welcome7-Jan-21 
3106In Process11-Jan-21 
3107Delivery12-Jan-21 
3108Returned13-Jan-21 
3109Welcome14-Jan-211
4110Welcome1-Feb-21 
4111Welcome5-Feb-21 
4112Welcome25-Feb-211

4.) Sample Data With Expected Output

store_idtransaction_iddatelatest_welcome_transaction_flag#_days_since_last_transaction
110310-Jan-21161
21042-Jan-21169
310914-Jan-21157
411225-Feb-21115
1 ACCEPTED SOLUTION
OwenAuger
Super User
Super User

Hi @win_anthony 

 

Try rewriting your first calculated column like this:

latest_welcome_transaction_flag = 
VAR LatestWelcomeDatePerStore = 
    CALCULATE ( 
        MAX ( 'table'[date] ),
        ALLEXCEPT ( 'table', 'table'[store_id] ),
        LEFT ( 'table'[status], 7 ) = "Welcome"
    )
RETURN
    IF ( 'table'[date] = LatestWelcomeDatePerStore, 1 )

I have simplified the code a little, and ensured that the CALCULATE function, which is called within the row context of the table, includes an ALLEXCEPT which removes filters on all columns except store_id.

 

Originally, the outermost call to CALCULATE added filters corresponding to all columns including # of Days Since Last Transaction.WelComeOpp, which created a circular reference.

 

Whenever CALCULATE is called in a calculated column, "context transition" causes the values of all columns in the current row are converted to an equivalent filter, unless those columns are excluded from the resulting filter context using ALLEXCEPT/REMOVEFILTERS. 

 

There is a good article on this general topic here at SQLBI.

 

Regards,

Owen


Owen Auger
Did I answer your question? Mark my post as a solution!
Blog
Twitter
LinkedIn

View solution in original post

1 REPLY 1
OwenAuger
Super User
Super User

Hi @win_anthony 

 

Try rewriting your first calculated column like this:

latest_welcome_transaction_flag = 
VAR LatestWelcomeDatePerStore = 
    CALCULATE ( 
        MAX ( 'table'[date] ),
        ALLEXCEPT ( 'table', 'table'[store_id] ),
        LEFT ( 'table'[status], 7 ) = "Welcome"
    )
RETURN
    IF ( 'table'[date] = LatestWelcomeDatePerStore, 1 )

I have simplified the code a little, and ensured that the CALCULATE function, which is called within the row context of the table, includes an ALLEXCEPT which removes filters on all columns except store_id.

 

Originally, the outermost call to CALCULATE added filters corresponding to all columns including # of Days Since Last Transaction.WelComeOpp, which created a circular reference.

 

Whenever CALCULATE is called in a calculated column, "context transition" causes the values of all columns in the current row are converted to an equivalent filter, unless those columns are excluded from the resulting filter context using ALLEXCEPT/REMOVEFILTERS. 

 

There is a good article on this general topic here at SQLBI.

 

Regards,

Owen


Owen Auger
Did I answer your question? Mark my post as a solution!
Blog
Twitter
LinkedIn

Helpful resources

Announcements
RTI Forums Carousel3

New forum boards available in Real-Time Intelligence.

Ask questions in Eventhouse and KQL, Eventstream, and Reflex.

LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.