Forum Discussion

emarc1's avatar
emarc1
Icon for Advocate II rankAdvocate II
9 years ago
Solved

Circular dependencies / calendar tables

I've always struggled to get multiple date relationships to work with calendar tables. In my data model, the Invoices table contains order dates, despatch dates and invoice dates (amongst many others but these are the important ones right now). I've made a relationship for Invoices[DateOrdered]<>Calendar[Date] and inactive relationships for DateDespatched and DateInvoiced.

 

 

I'm trying to calculate the length of time between when orders are placed and despatched using some of the work of Alberto Ferrari: http://sqlblog.com/blogs/alberto_ferrari/archive/2011/01/19/working-days-computation-in-powerpivot.aspx

 

One column is made in Calendar that has a 1 for every working day and a 0 for every weekend day and holidays. Another column sums all of the dates before the current date:


01/01/2017, Sun, 0, 0 (Weekend)
02/01/2017, Mon, 0, 0 (Bank Holiday)

03/01/2017, Tue, 1, 1

04/01/2017, Wed, 1, 2

05/01/2017, Thu, 1, 3

06/01/2017, Fri, 1, 4

07/01/2017, Sat, 0, 4 (Weekend)

08/01/2017, Sun, 0, 4 (Weekend)

09/01/2017, Mon, 1, 5

...

 

You subtract the final WorkingDayNumber value for the order date from the value for the despatch date and you get the number of working days between them.

 

I then put two calculated columns into Invoices. Here's the one for DateOrdered:

CALCULATE(
VALUES( 'Calendar'[WorkingDayNumber] ),
'Calendar'[Date] = EARLIER( Invoices[DateOrdered] )
)

 

This creates a circular dependancy... So I tried using LOOKUPVALUE but I get the same problem! I thought LOOKUPVALUE was meant to get around relationship issues. If I deactivate the relationship between Calendar[Date] and Invoices[DateOrdered] (so now there are just three inactive relationships there) then either of these ways actually works perfectly... But it breaks everything else in my report that relies on that relationship. 

 

Any thoughts on how to get everything working here?

 

I can't share the pbix but I can take screen shots of things if it helps.

  • Anonymous's avatar
    Anonymous
    9 years ago

    The problem is all the bi-directional crossfiltering. It's often better to leave the direction as Single on relationships and use the CROSSFILTER() formula only in the measures that absolutely require the Both directionality.

8 Replies

    • emarc1's avatar
      emarc1
      Icon for Advocate II rankAdvocate II

      Thanks for your reply v-chuncz-msft.

      I think I've also tried that method before too.

       

      With all of the date relationships inactive still, and the two calculated columns set out like this (the WorkingDayOrder column just uses Invoices[DateOrdered] <> Calendar[Date] instead):

       

       

      That WorkingDayOrder works correctly but you can see here I get a circular dependancy with the WorkingDayDespatched column.

       

      Here's the Calendar table for reference:

       

       

      Now if I'm thinking correctly... I can make the 'Calendar'[Date]<>'Invoices'[DateOrdered] relationship active and replace the WorkingDayOrdered column with:

      WorkingDayOrdered = RELATED( 'Calendar'[WorkingDayNumber] )

      And now there's no circular dependency but something's not quite right...

       

      Why is WorkingDayDespatch showing the same WorkingDayNumber as WorkingDayOrder when the dates that relate them are different? It's as if the USERELATIONSHIP is being ignored and the active relationship is taking priority.

      • emarc1's avatar
        emarc1
        Icon for Advocate II rankAdvocate II

        Here's a screenshot of the relationships as they are set up for the second half of that last post: