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

Don't miss out! 2025 Microsoft Fabric Community Conference, March 31 - April 2, Las Vegas, Nevada. Use code MSCUST for a $150 discount. Prices go up February 11th. Register now.

Reply
DominicBrien
Frequent Visitor

Cross table Datediff

Hello,
Lets say I have a person table where I can find date of birth for every person in the system.
I also have an event table where I can find the date an event occured at and another field that holds the ID of the person table to establish to whom this event is related.
I want to have a measure or columns on events that would hold the age the person was when the event occured.
This
Age = DATEDIFF(Event[Date],Contacts[DateOfBirth],YEAR)
Tells me that
A single value for column 'DateOfBirth' in table 'Contacts' 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.
I also tried including "RELATED" in there without any success.
I find it weird as it should absolutly be able to find a single record...
 
Thanks

1 ACCEPTED SOLUTION
v-haibl-msft
Microsoft Employee
Microsoft Employee

@DominicBrien

 

What’s the warning message when you try including “RELATED” in the calculate column expression?

If Event and Contacts table has relationship with the PersonID key, you should be able to get the desired result with following column formula.

 

Age = 
IF (
    ISERROR ( DATEDIFF ( Event[Date], RELATED ( Contacts[DateOfBirth] ), YEAR ) ),
    DATEDIFF ( RELATED ( Contacts[DateOfBirth] ), Event[Date], YEAR )
)

Cross table Datediff_1.jpg

 

Best Regards,

Herbert

View solution in original post

6 REPLIES 6
v-haibl-msft
Microsoft Employee
Microsoft Employee

@DominicBrien

 

What’s the warning message when you try including “RELATED” in the calculate column expression?

If Event and Contacts table has relationship with the PersonID key, you should be able to get the desired result with following column formula.

 

Age = 
IF (
    ISERROR ( DATEDIFF ( Event[Date], RELATED ( Contacts[DateOfBirth] ), YEAR ) ),
    DATEDIFF ( RELATED ( Contacts[DateOfBirth] ), Event[Date], YEAR )
)

Cross table Datediff_1.jpg

 

Best Regards,

Herbert

What happens if the two tables only have an indirect relationship between them? like -------- (dotted) lines? 

What about if there is no relationship at all.  For example, how do I do a datediff between two completely independent tables?  Let's say I want to make sure the tables refreshed on the same day.  I would put a date in each table (sql) then I want to compare the dates from each table.  

 

I am open to ideas, please?

 

Hello thanks for the help,

In fact I assumed it was not working because the "RELATED" function and the field appeared with both underlined with red squedlies.

 

Buth when I use the column it does hold the right value.

 

What are the squedlies supposed to tell me?

I didnot mention but I am in direct query so I aparently cannot use ISERROR...

@DominicBrien

 

If you use it as measure, it cannot determin a signle value for a column. So you’ll see red underlines. You can take a look at this article which explains the difference between calculated column and measure.

To make ISERROR to be supported in DirectQuery mode, you can try to enable following option in File - Options.

Cross table Datediff_1.jpg

 

Or you can try to update the column expression as below.

Age = 
IF (
    Event[Date] <= RELATED ( Contacts[DateOfBirth] ),
    DATEDIFF ( Event[Date], RELATED ( Contacts[DateOfBirth] ), YEAR ),
    DATEDIFF ( RELATED ( Contacts[DateOfBirth] ), Event[Date], YEAR )
)

 

Best Regards,

Herbert

Greg_Deckler
Super User
Super User

With this:

 

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45Wci9KTVfSUTLWNzTSN7Q0N1KK1YlWcsrMyQEKgoQMgKKmBmDRoPzk7NQSoLiZvpm+kYGBmVJsLAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Person = _t, Column1 = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Person", type text}, {"Column1", type date}})
in
    #"Changed Type"

 

 

and this, related on Person (many-to-one Events to People)

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45Wci1LzSsxVNJRMjTQNzTVNzIwNABy3ItS05VidXBJB+UnZ6eWIBQYISswwdCPIU1Qv1NmTg5C2hhZ2gwuHQsA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Event = _t, Date = _t, Person = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Event", type text}, {"Date", type date}, {"Person", type text}})
in
    #"Changed Type"

 

I did this in a column and it worked

 

 

Age = DATEDIFF(RELATED(People[Column1].[Date]),Events[Date].[Date],YEAR)

 



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!:
Power BI Cookbook Third Edition (Color)

DAX is easy, CALCULATE makes DAX hard...

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

Jan25PBI_Carousel

Power BI Monthly Update - January 2025

Check out the January 2025 Power BI update to learn about new features in Reporting, Modeling, and Data Connectivity.

Jan NL Carousel

Fabric Community Update - January 2025

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