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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
Anonymous
Not applicable

DAX to retrieve most recent value before a date

Hi there. My sample data is as follows

 

DateTimeIDTimeStampValue
2023-11-01 13:0012023-11-01 12:48100
2023-11-01 13:0012023-11-01 12:52150
2023-11-01 13:0022023-11-01 12:3250
2023-11-01 13:0022023-11-01 12:4075
2023-11-01 14:0012023-11-01 13:1520
2023-11-01 14:0012023-11-01 13:2530
2023-11-01 14:0022023-11-01 13:3540

 

I am trying to retrieve the latest timestamp before each datetime for each ID and then summing them together.

Results table should look like this

 

DateTimeSum
2023-11-01 13:00225 (150 + 75)
2023-11-01 14:0070 (30 + 40)

 

I am using direct query mode

 

Can I have some help with some DAX that would achieve this?

 

Thanks

1 REPLY 1
Jihwan_Kim
Super User
Super User

Hi,

Please check the below picture and the attached pbix file.

 

Jihwan_Kim_0-1718941738982.png

 

Expected result measure: =
VAR _datetime =
    MAX ( Data[DateTime] )
VAR _t =
    FILTER (
        SUMMARIZE ( Data, Data[ID], Data[TimeStamp], Data[Value] ),
        Data[TimeStamp] <= _datetime
    )
VAR _resulttable =
    INDEX ( 1, _t, ORDERBY ( Data[TimeStamp], DESC ),, PARTITIONBY ( Data[ID] ) )
RETURN
    IF ( HASONEVALUE ( Data[DateTime] ), SUMX ( _resulttable, Data[Value] ) )

 

 

INDEX function (DAX) - DAX | Microsoft Learn

 

 


If this post helps, then please consider accepting it as the solution to help other members find it faster, and give a big thumbs up.


Click here to visit my LinkedIn page

Click here to schedule a short Teams meeting to discuss your question.

Helpful resources

Announcements
July 2025 community update carousel

Fabric Community Update - July 2025

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

July PBI25 Carousel

Power BI Monthly Update - July 2025

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