Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
4 years ago
Solved

Return values for the last date available

Hi everyone,

 

I am making a report with the following tables:

 

ProjectList

Project IDProject nameProject Owner

1

Project XJohn Doe
Project YJane Doe

 

And

 

StatusUpdates

Project IDDateUpdate
120/11/2021Fixed issue 1
222/11/2021Fixed issue 2
121/11/2021Fixed issue 3
224/11/2021Fixed issue 4

 

"ProjectList" filters "StatusUpdates" based on the column "ProjectID". I essentially imported the first table as a table in my report, and I would like to achieve that by clicking on one of the values in that table, it returns the string value in the column "Update" of the table "StatusUpdates" of the latest date. So essentially, when you would click on project 1 in the table, it would return the value "Fixed issue 3" on a card, which is the third record in the second table.

 

I already tried with SELECTEDVALUE() but I'm not able to fix the issue. Can someone help me?

8 Replies

  • smpa01's avatar
    smpa01
    Community Champion

    Anonymous  you can use a measure like this

    Measure =
    CALCULATE (
        MAX ( StatusUpdates[Update] ),
        FILTER (
            VALUES ( StatusUpdates ),
            StatusUpdates[Date]
                = CALCULATE (
                    MAX ( StatusUpdates[Date] ),
                    ALLEXCEPT ( ProjectList, ProjectList[Project ID] )
                )
        )
    )
    

     

     

    • Anonymous's avatar
      Anonymous
      Not applicable

      Hi, unfortunately it does not work for meand it even seems to return the first registered values, not the last ones...

    • AlexisOlson's avatar
      AlexisOlson
      Super User

      This appears to work but I'd recommend defining a variable for MaxDate for several reasons:

      1. Performance. You don't have to recalculate for each row of the table you're filtering.
      2. Robustness. Avoid potential trouble arising from a context transition inside FILTER.
      3. Readability. Broken up a bit, the code is often easier for humans to understand.
  • smpa01's avatar
    smpa01
    Community Champion

    Anonymous  returned and attached

    Measure =
    VAR _0 =
        MAX ( 'Project Status'[Created] )
    RETURN
        CALCULATE (
            MAX ( 'Project Status'[Status Budget] ),
            FILTER (
                VALUES ( 'Project Status' ),
                'Project Status'[Created]
                    = CALCULATE ( _0, ALLEXCEPT ( 'Project Status', 'Project Status'[ProjectId] ) )
            )
        )
    

     

     

    AlexisOlson  thanks !!!

     

    • AlexisOlson's avatar
      AlexisOlson
      Super User

      Not quite what I meant. Once a variable is defined, adjusting its filter context with CALCULATE doesn't do anything, so your DAX is equivalent to 

      Measure =
      VAR _0 = MAX ( 'Project Status'[Created] )
      RETURN
          CALCULATE (
              MAX ( 'Project Status'[Status Budget] ),
              FILTER ( VALUES ( 'Project Status' ), 'Project Status'[Created] = _0 )
          )

       

      I was suggesting something like this:

      Measure =
      VAR ProjectLastCreated =
          CALCULATE (
              MAX ( 'Project Status'[Created] ),
              ALLEXCEPT ( 'Project Status', 'Project Status'[ProjectId] )
          )
      RETURN
          CALCULATE (
              MAX ( 'Project Status'[Status Budget] ),
              'Project Status'[Created] = ProjectLastCreated
          )