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

Find everything you need to get certified on Fabric—skills challenges, live sessions, exam prep, role guidance, and more. Get started

Reply
kault
Helper II
Helper II

DATEDIFF between two tables many-to-many relationship

I need to calculate the difference between two dates between two tables. The problem is the two tables have a many-to-many relationship.

 

Date #1: TaskAnalysis.TaskPlanEndDate (Project X will have multiple "end dates", but they are all the same)

Date #2: ProjectList.ContractEndDate (Project X will have 1 end date)

 

I tried creating this formula: 

datediff('Task Analysis'[TaskPlanEndDate],related('Project List'[ContractEndDate]),day)
 
But get the following error message: "The column 'Project List'[ContractEndDate]' either doesn't exist or doesn't have a relationship to any table available in the current context."
 
The column does exist, and the relationship does exist, but the relationship is many-to-many.
 
What is the workaround so I can calculate the difference between the days?
 
 
1 ACCEPTED SOLUTION

Hi @kault ,

 

Try the following formula:

 

Column = 
DATEDIFF(
    'TaskAnalysis'[TaskPlanEndDate],
    LOOKUPVALUE( ProjectList[ContractEndDate], ProjectList[Project], TaskAnalysis[Project] ),
    DAY
)

 image.pngimage.png

 


If the problem is still not resolved, please provide detailed error information or the expected result you expect. Let me know immediately, looking forward to your reply.

Best Regards,
Winniz

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

6 REPLIES 6
kault
Helper II
Helper II

If anyone else can help, I would appreciate it. I am just looking for how to use DAX to accomplish DATEDIFF between two tables with a many-to-many relationship.

Hi @kault ,

 

Try the following formula:

 

Column = 
DATEDIFF(
    'TaskAnalysis'[TaskPlanEndDate],
    LOOKUPVALUE( ProjectList[ContractEndDate], ProjectList[Project], TaskAnalysis[Project] ),
    DAY
)

 image.pngimage.png

 


If the problem is still not resolved, please provide detailed error information or the expected result you expect. Let me know immediately, looking forward to your reply.

Best Regards,
Winniz

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

I have the same issue as above, different context but same issue between two tables.  I have no way to get around the many-to-many relationship in this case.

 

I tried the solution listed but it did not work for me.  Formula listed below as well as error information. 

 

Column = DATEDIFF('GetRO?username=SIMONS&event=CREATED&start=10-01-2022&end=12-31-2030&token=a997ec'[repair_orders.date_created],LOOKUPVALUE('GetIssues?username=SIMONS&status=OPEN&token=a997ecf6-60a2-41d1-8022-a5c7ca2ab2e4'[issues.date_added],'GetIssues?username=SIMONS&status=OPEN&token=a997ecf6-60a2-41d1-8022-a5c7ca2ab2e4'[issues.unit_number],'GetRO?username=SIMONS&event=CREATED&start=10-01-2022&end=12-31-2030&token=a997ec'[repair_orders.unit_number]),DAY)
 
 
"A table of multiple values was supplied where a single value was expected"
Greg_Deckler
Super User
Super User

@kault You can't use RELATED with many-to-many relationships. You will need to use things like MAXX(FILTER...) or LOOKUPVALUE, etc.


Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

@Greg_Deckler Correct, that's what I had said in my post originally. I am trying to figure out how to "use things like MAXX(FILTER...) or LOOKUPVALUE, etc." instead - I've never used anything like this.

@kault Only way to know for sure is sample data:

Sorry, having trouble following, can you post sample data as text and expected output?
Not really enough information to go on, please first check if your issue is a common issue listed here: https://community.powerbi.com/t5/Community-Blog/Before-You-Post-Read-This/ba-p/1116882

Also, please see this post regarding How to Get Your Question Answered Quickly: https://community.powerbi.com/t5/Community-Blog/How-to-Get-Your-Question-Answered-Quickly/ba-p/38490

The most important parts are:
1. Sample data as text, use the table tool in the editing bar
2. Expected output from sample data
3. Explanation in words of how to get from 1. to 2.


Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

Helpful resources

Announcements
July 2024 Power BI Update

Power BI Monthly Update - July 2024

Check out the July 2024 Power BI update to learn about new features.

July Newsletter

Fabric Community Update - July 2024

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