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

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
Mtelf24
Helper I
Helper I

Date value based off MAX date of a different column

I have a table of values that measure date change events linked to a unique id, e.g.

 

IDRecord CreatedChangedFromChangedTo
120/05/202030/06/202013/07/2020
121/05/202013/07/202020/07/2020
128/05/202020/07/202028/06/2020
214/04/202016/05/202015/09/2020
218/05/202015/09/202023/09/2020
201/06/202023/09/202012/09/2020

 

I need to measure the earliest movement based off ID, and the latest movement based off ID too.

Before I was using a formula like:

 

 

LatestMovement = 
CALCULATE (
    MAX ( Table[ChangedTo] ),
    ALLEXCEPT ( Table, Table[ID] )
)

 

 

And that works fine to a certain extent - but in my above table isn't quite what I'm after. It would return 20/07/2020 for all three rows for ID 1 (it being in all three rows suits me fine) but I don't want that returning because the latest movement is actually 28/06/2020 based off the created date.

My question is, how do I return the max date of ChangedTo in the results in the style of the above formula, but using the MAX of the created on date? I.E. the formula should be able to check for the MAX of the createdon date based off the ID and then return the value in the column for the ID, e.g:

IDRecord CreatedChangedFromChangedToLatestMovement
120/05/202030/06/202013/07/202028/06/2020
121/05/202013/07/202020/07/202028/06/2020
128/05/202020/07/202028/06/202028/06/2020
214/04/202016/05/202015/09/202012/09/2020
218/05/202015/09/202023/09/202012/09/2020
201/06/202023/09/202012/09/202012/09/2020

 

I've seen a number of other threads on this sort of topic but can't quite land on what I'm looking for.

1 ACCEPTED SOLUTION

Hi @Mtelf24 ,

 

I have looked at the data you send out and you should use the following measure:

LatestMovement_ = 
VAR TEMP_TABLE =
    FILTER (
        SUMMARIZE (
            ALL ( Sheet1[ID]; Sheet1[CreatedOn]; Sheet1[ChangedTo] );
            Sheet1[ID];
            Sheet1[CreatedOn];
            Sheet1[ChangedTo]
        );
        Sheet1[ID] = SELECTEDVALUE ( Sheet1[ID] )
    )
VAR DATE_SELECT =
    MAXX ( TEMP_TABLE; Sheet1[CreatedOn] )
VAR DATE_CHANGED_TO =
    MAXX (
        FILTER ( TEMP_TABLE; Sheet1[CreatedOn] = DATE_SELECT );
        Sheet1[ChangedTo]
    )
RETURN
    IF ( DATE_CHANGED_TO = BLANK (); ""; DATE_CHANGED_TO )

 

 


Regards

Miguel Félix


Did I answer your question? Mark my post as a solution!

Proud to be a Super User!

Check out my blog: Power BI em Português



View solution in original post

6 REPLIES 6
MFelix
Super User
Super User

Hi @Mtelf24 ,

 

Looking at the final result you present you want the minimum value not the MAX so just use:

 

LatestMovement = 
CALCULATE (
    MIN ( Table[ChangedTo] ),
    ALLEXCEPT ( Table, Table[ID] )
)

 

But I assume this is only a coincidence of having the lowest date at the highest creation date.

 

Try the following measure:

LatestMovement =
)
LOOKUPVALUE (
    'Table'[Changed To],
    'Table'[Record Created], CALCULATE (
        MAX ( 'Table'[Record Created] ),
        ALLEXCEPT ( 'Table', 'Table'[ID] )
    ),
    'Table'[ID], SELECTEDVALUE ( 'Table'[ID] )
)

 

This  should give expected result. I have made a change to the date of the record 1 and got the date for the last line, so assuming that is the result you need.


Regards

Miguel Félix


Did I answer your question? Mark my post as a solution!

Proud to be a Super User!

Check out my blog: Power BI em Português



Hi @MFelix, thanks for your help on this.

(apologies if my post below isn't clear - the board automation cleared out my original post so I had to rewrite it)

 

I've tried the secondary formula you've provided, although unfortunately without success - no error returned, but all rows in the column are blank (which searching, seems to be a common issue for PowerBI users unfamiliar with the formula) which I think is suggesting its not getting a match? For what its worth, all field references (other than the ID) for changedto and created are of the data type 'date' so I don't think its a data type thing - although I did try changing the createddate to a date/time type since I noticed the output of just using the CALCULATE part of your formula (which works fine on its own) within the LOOKUPVALUE returned a date/time format in the output, e.g:

 

 

 

LatestMovementLookup = CALCULATE ( MAX ( Table[Record Created] ), ALLEXCEPT ( Table, Table[ID] ) )

 

 


To me it looks like your formula should work (i.e. it will return whatever is 'Changed To' in the row where 'Record Created' matches the output of the CALCULATE formula (which is returning the latest (max) 'Record Created' value by ID), but I must be missing something in regards to something along the lines of data type formatting or duplicate references?

 

ID

'Table'[Record Created]

// below is output after changing format from Date to Date Type using example of 5 different rows with the same ID

Changed ToCALCULATE ( MAX ( 'Table'[Record Created] ), ALLEXCEPT ( 'Table', 'Table'[ID] ) ) //output of above formula below
119/03/2020 00:00:0004 May 202027/05/2020 00:00:00
127/05/2020 00:00:0026 May 202027/05/2020 00:00:00
119/03/2020 00:00:00 27/05/2020 00:00:00
116/04/2020 00:00:0025 April 202027/05/2020 00:00:00
127/05/2020 00:00:0026 May 202027/05/2020 00:00:00


I'm wondering if its having an issue with the fact that the original record created field doesn't record the time, in which case its getting confused by the two 27/05/2020 00:00:00 references when doing LOOKUPVALUE(), I think I do have a method for getting the exact time rather than 00:00:00 so could try that.

 

(Changed to can be blank by the way as the above is basically like an audit record, and a user can remove a date as much as change it to a new one)

 

I'm just having abit of a look on the official microsoft reference on LOOKUPVALUE() and this does appear to be along the right track of what I'm looking to achieve. I can't quite work out what the

,Table[ID], SELECTEDVALUE ( Table[ID] ) bit you added is for though?

I get a blank result if I remove the above as well without an error.

Hi @Mtelf24 ,

 

I have looked at the data you send out and you should use the following measure:

LatestMovement_ = 
VAR TEMP_TABLE =
    FILTER (
        SUMMARIZE (
            ALL ( Sheet1[ID]; Sheet1[CreatedOn]; Sheet1[ChangedTo] );
            Sheet1[ID];
            Sheet1[CreatedOn];
            Sheet1[ChangedTo]
        );
        Sheet1[ID] = SELECTEDVALUE ( Sheet1[ID] )
    )
VAR DATE_SELECT =
    MAXX ( TEMP_TABLE; Sheet1[CreatedOn] )
VAR DATE_CHANGED_TO =
    MAXX (
        FILTER ( TEMP_TABLE; Sheet1[CreatedOn] = DATE_SELECT );
        Sheet1[ChangedTo]
    )
RETURN
    IF ( DATE_CHANGED_TO = BLANK (); ""; DATE_CHANGED_TO )

 

 


Regards

Miguel Félix


Did I answer your question? Mark my post as a solution!

Proud to be a Super User!

Check out my blog: Power BI em Português



Apologies, just to add to MFelix's v.helpful solution (which requires use of a measure instead of calculated column), I also wanted to add that if I changed the last line to avoid a data type conflict:

IF ( DATE_CHANGED_TO = BLANK (); ""; DATE_CHANGED_TO )

to

IF ( DATE_CHANGED_TO = BLANK (); BLANK(); DATE_CHANGED_TO )

 I could then make use of it as a calculated column with the following (though I haven't fully explored any 'gotchas' with it yet that may cause issues as it seems almost too easy), but its looking good for what I wanted to use it in so far (matrix) - as I found that when I added the measure to a table the table would not filter 100% correctly when selecting a column/value in my matrix:

LatestMoveCalcColumn = 
CALCULATE(LDOSMovementsCOVID[LatestMovement_])

 

Hi @Mtelf24 ,

 

Just to clarify why I have placed "" instead of BLANK in the measure, is that when you are calculating measure blank values get automatically filter out from the visualization so if you had a certain item that the value of the maximum date was returning BLANK you would not had any result in the visualization.

 

But great catch.


Regards

Miguel Félix


Did I answer your question? Mark my post as a solution!

Proud to be a Super User!

Check out my blog: Power BI em Português



Hi @Mtelf24 ,

 

In the test file I have made this worked correctly with date fields, no issue so not really sure if it's a date format issue.

 

Can you share a mockup file please? If there is sensitive information share trough private message.

 

The SELECTEDVALUE part is to force the filter to pickup the ID that is on the current row, as you can we are making a Lookupvalue on the full table so with the selected value we only focus on the specific ID.


Regards

Miguel Félix


Did I answer your question? Mark my post as a solution!

Proud to be a Super User!

Check out my blog: Power BI em Português



Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

Check out the June 2025 Power BI update to learn about new features.

June 2025 community update carousel

Fabric Community Update - June 2025

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