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

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply
pmdci
Advocate V
Advocate V

DAX: relating a field from another table in a function

Hi,
 
I created the following DAX calculated column, which I am using across different tables.
 
=(YEAR(Today())-YEAR(DATEFIELD]))*12+MONTH(Today())-MONTH([DATEFIELD])
 
This field calculates how many months have elapsed from the date found in DATEFIELD until the time the model is processed. It works quite well, except for one little caveat: The DATE FIELD must exist within the table itself.
 
But what if I want to relate to a date found at a date dimension table? Say that I only have a datekey in the table I want to hold my calculated field, and I want to refer to the date field found in the date dimension table. How would I go about it? I tried playing with the RELATED() function but I only got errors.
 
Regards,
P.

1 ACCEPTED SOLUTION

@Neuro81 and @ankitpatira

 

Sorry for the trouble, but I managed to fix it in the end

 

=(YEAR(Today())-YEAR(RELATED('Availability Calendar'[Date])))*12+MONTH(Today())-MONTH(RELATED('Availability Calendar'[Date]))

 

The problem is that I got lost with the number of parenthesis (how lame!) So I deconstructed the formula and use a replace function to make it work.

 

Wish I could delete this post altogether. Again sorry for wasting your time! Smiley SadSmiley Mad

View solution in original post

8 REPLIES 8
Neuro81
Helper I
Helper I

What errors did you get?
also did you create a relationship between the two tables in the diagram view?

-- ------------------------------ --
Please mark solutions as complete when your question has been answered
-- ------------------------------ --

@Neuro81 and @ankitpatira

 

Sorry for the trouble, but I managed to fix it in the end

 

=(YEAR(Today())-YEAR(RELATED('Availability Calendar'[Date])))*12+MONTH(Today())-MONTH(RELATED('Availability Calendar'[Date]))

 

The problem is that I got lost with the number of parenthesis (how lame!) So I deconstructed the formula and use a replace function to make it work.

 

Wish I could delete this post altogether. Again sorry for wasting your time! Smiley SadSmiley Mad

@pmdci
Sometimes its best to talk it out.

Whenever I get stuck I usually take a walk and try not to think of the problem and a lot of the time the solution just comes.

Its good that you take the time to try and fix the problem yourself instead of just waiting till someone comes to help you!!

-- ------------------------------ --
Please mark solutions as complete when your question has been answered
-- ------------------------------ --

@Neuro81 There is a relationship, yes. Between the fact table in which the calculated column is located, and a Date Dimension table. The linked fields are called DATEKEY in both tables.

 

Here is what I tried:

 

=(YEAR(Today())-YEAR(RELATED('Availability Calendar'[Date]))*12+MONTH(Today())-MONTH(RELATED('Availability Calendar'[Date]))

 

But Visual Studio gives me a syntax error message.

ankitpatira
Community Champion
Community Champion

@pmdci You can make use of RELATED function. It will work since you have relation between two tables.

@ankitpatira as I mentioned in my original post, I tried it but I only got errors.

@pmdci what is the error you getting when using RELATED.

@ankitpatira I get a syntax error. See above.

 

Regards,

P.

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!

Dec Fabric Community Survey

We want your feedback!

Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.