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
Anonymous
Not applicable

Subtracting Dates within Two Different Tables

Hello,

 

 

I have a report I'm working on using DirectQuery. I'd like to subtract the difference in two dates that reside in two tables but based off a variable in one of the tables.

 

So, we have a table for Equipment and a table for SCalls. I need to find the difference in the Equipment Create Date and the SCall Date for when the SCall Type is a certain value.

 

Can I do this in DirectQuery?

 

Thanks,

Michelle

3 ACCEPTED SOLUTIONS

@Anonymous,

Create similar measures as below. If the DAX don't help, please share dummy data of your tables.

Measure = CALCULATE(max(SCCalls[Create Date]),FILTER(SCCalls,SCCalls[Scall Type]="test2"))
Difference = DATEDIFF(MAX(SCEquipments[Create Date]),SCEquipments[Measure],DAY)

1.PNG

Regards,
Lydia

Community Support Team _ Lydia Zhang
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

@Anonymous,

What if the type of CallTypeID? If it is numerical type, change your DAX to the following:

Measure= CALCULATE(MAX(SCCalls[CreateDate]), FILTER(SCCalls, SCCalls[CallTypeID]=55))

Regards,
Lydia

Community Support Team _ Lydia Zhang
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

@Anonymous,

Right-click on your table , then select "New Measure" and apply your DAX.

Regards,
Lydia

Community Support Team _ Lydia Zhang
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

11 REPLIES 11
v-yuezhe-msft
Employee
Employee

@Anonymous,

Is there any relationship between the Equipment table and SCalls table?

You should be able to use DAX or Power BI query code to achieve the above requirement, please share dummy data of the two tables and post expected result so that we can provide you appropriate DAX formula or Power Query code.

Regards,
Lydia

Community Support Team _ Lydia Zhang
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

I have 2 tables 
AA tables has                                                 BB table has
ArrivalDateTime                                             EventDateTime
10/15/2022 07:46:36 PM                               10/15/2022 07:46:36 PM 
10/15/2022  11:49:23 AM                               10/15/2022  11:52:23 PM

10/15/2022 10:43:04 PM                               10/15/2022 10:54:04 PM

10/15/2022 11:38:45 PM                               10/16/2022 00:01:45 PM

10/15/2022 10:49:45 PM                               10/15/2022 11:01:45 PM
10/15/2022 11:51:45 PM                               10/16/2022 00:10:45 PM

can we do time difference calculations like this ( if we have Common column which has Many to Many Relation) ( whatif we have Many to One Reation ).

I have one more tables which has EndDateTime
can we lookup these columns into main tables?

Anonymous
Not applicable

@v-yuezhe-msft,

 

Yes, they are related by EquipmentID field. The formula I'm using is:

 

VALUE(DATEDIFF(SCEquipments[CreateDate],SCCalls[CreateDate],DAY))

 

The error it's producing is: A single value for column "CreateDate" in table "SCCalls" cannot be determined. This can happen when a measure formula refers to a column that contains many values without specifying an aggregation such as min, max, count, or sum to get a single result.

 

The issue is that each equipment can have multiple service calls. I'd like to subtract the dates from every service call and then filter the calls down to what I'm looking for but it seems I have to filter within the formula instead.

@Anonymous,

Create similar measures as below. If the DAX don't help, please share dummy data of your tables.

Measure = CALCULATE(max(SCCalls[Create Date]),FILTER(SCCalls,SCCalls[Scall Type]="test2"))
Difference = DATEDIFF(MAX(SCEquipments[Create Date]),SCEquipments[Measure],DAY)

1.PNG

Regards,
Lydia

Community Support Team _ Lydia Zhang
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Anonymous
Not applicable

I'm still getting the same error. The two tables are quite large but here is some of the important information:

 

 

Table FieldSample Data
SCCalls.EquipmentID12345
SCCalls.CallNumber201123
SCCalls.CallTypeIDP
SCCalls.CustomerID

123

SCCalls.CreateDate1/1/18 2:00pm
SCCalls.CloseDate1/5/18 5:00pm
  
  
Table FieldSample Data
SCEquipments.EquipmentID12345
SCEquipments.CustomerID

123

SCEquipments.CreateDate1/1/18 2:00pm
SCEquipments.InstallDate1/2/16 3:00pm

 

Let me know if I need to give you more information. 

@Anonymous,

Right click your table and choose new measure, then apply my DAX. If you still get issues, please post expected result based on the sample data you share and post a screenshot about your current scenario.

Regards,
Lydia

Community Support Team _ Lydia Zhang
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Anonymous
Not applicable

I was wrong about the CallTypeID above. Apparently it's one of the few ID's that have numerical values instead of letters.

 

I put the measure in and got the below error.

 

Error.jpg

 

I tried adding the Value function right before the (SCCAlls[CallTypeID]="55")) but I got this:

 

Error2.jpg

 

Thanks,

@Anonymous,

What if the type of CallTypeID? If it is numerical type, change your DAX to the following:

Measure= CALCULATE(MAX(SCCalls[CreateDate]), FILTER(SCCalls, SCCalls[CallTypeID]=55))

Regards,
Lydia

Community Support Team _ Lydia Zhang
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Anonymous
Not applicable

@v-yuezhe-msft,

 

Okay, this got the Measure to work correctly but now I have an error with the column. Can you take a look? You have been so helpful!!

 

2018-05-02_16-14-46.jpg

@Anonymous,

Right-click on your table , then select "New Measure" and apply your DAX.

Regards,
Lydia

Community Support Team _ Lydia Zhang
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Anonymous
Not applicable

Great! Thanks!

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.