Forum Discussion

JoanMok's avatar
JoanMok
Frequent Visitor
1 year ago
Solved

using RELATED to calculate date difference

Hallo,  I have a fact table call OTD, which has two decimal number columns but with date data, OriginalShipmentDate and PlannedShipmentDate.  I have two date dimension tables with managed r...
  • Anonymous's avatar
    Anonymous
    1 year ago

    Hi JoanMok 

     

    The RELATED() function is to return a related value from another table.

    Also the RELATED function needs a row context; therefore, it can only be used in calculated column expression, where the current row context is unambiguous, or as a nested function in an expression that uses a table scanning function. A table scanning function, such as SUMX, gets the value of the current row value and then scans another table for instances of that value. The Measure is the Filter Context, so you get the error.

    Here are some links for your reference:

    Context in DAX Formulas - Microsoft Support

    Understanding Context Transition - SQLBI

     

    If you want to calculate the datediff between 2 dates which in one table, you can try this:

    Here I create a set of sample:

    You can add a calculated column:

    Column = DATEDIFF('Table'[Date1],'Table'[Date2],DAY)

    Or a Measure:

    Measure = DATEDIFF(MAX('Table'[Date1]),MAX('Table'[Date2]),DAY) //The Max() function is to get the current row's date

     

    Best Regards

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