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

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
Jabz
New Member

Get latest record by ID

Hi, i am trying to get the last record by ID but it has to work with date filters, for example, i have the following table:

 

ID       Date      

A        1 Jan

A        10 Jan

B        29 Jan 

B        16 Feb

A        1 Feb

B         20 Feb

 

So, if i filter the table by January it will show me only the A record with date 10 Jan and B record with date 29 Jan, and if i filter by February it will show A record with Date 1 Feb and B record with Date 20 Feb.

 

Is there a way to do this in a measure? because i searched for solutions but the majority of them just use a calculate column to get the max date but that doesn´t work in my case since i want to filter "dynamically".

 

Basically i need that whatever date filter is, show me the last record by ID in that date.

2 ACCEPTED SOLUTIONS
AlexisOlson
Super User
Super User

There are several ways to do this. See for example my post here from 2018:
https://stackoverflow.com/questions/52525377/return-top-value-ordered-by-another-column

 

I'd recommend using the INDEX function.

CALCULATE ( 
    [Your Measure Here],
    INDEX ( 1, ORDERBY ( Table1[Date] ), DESC ),
    PARTITIONBY ( Table1[ID] )
)

View solution in original post

v-xuxinyi-msft
Community Support
Community Support

Hi @Jabz 

 

Here is my testing.

 

Date table:

vxuxinyimsft_0-1708566280219.png

 

Mesure:

 

Measure = 
VAR _MaxDate = CALCULATE(MAX([Date]), FILTER(ALLEXCEPT('Table','Table'[ID]), MONTH([Date]) = MONTH(SELECTEDVALUE('Date'[Date]))))
RETURN
IF(MAX([Date]) = _MaxDate, 1, 0)

 

 

Put the measure into the filter so that the visual only shows data where the measure is equal to 1.

vxuxinyimsft_2-1708567387281.png

 

Filter with Date table

vxuxinyimsft_3-1708567414721.png

 

vxuxinyimsft_4-1708567573686.png

Is this the result you expect?

 

Best Regards,
Yulia Xu

 

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

View solution in original post

2 REPLIES 2
v-xuxinyi-msft
Community Support
Community Support

Hi @Jabz 

 

Here is my testing.

 

Date table:

vxuxinyimsft_0-1708566280219.png

 

Mesure:

 

Measure = 
VAR _MaxDate = CALCULATE(MAX([Date]), FILTER(ALLEXCEPT('Table','Table'[ID]), MONTH([Date]) = MONTH(SELECTEDVALUE('Date'[Date]))))
RETURN
IF(MAX([Date]) = _MaxDate, 1, 0)

 

 

Put the measure into the filter so that the visual only shows data where the measure is equal to 1.

vxuxinyimsft_2-1708567387281.png

 

Filter with Date table

vxuxinyimsft_3-1708567414721.png

 

vxuxinyimsft_4-1708567573686.png

Is this the result you expect?

 

Best Regards,
Yulia Xu

 

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

AlexisOlson
Super User
Super User

There are several ways to do this. See for example my post here from 2018:
https://stackoverflow.com/questions/52525377/return-top-value-ordered-by-another-column

 

I'd recommend using the INDEX function.

CALCULATE ( 
    [Your Measure Here],
    INDEX ( 1, ORDERBY ( Table1[Date] ), DESC ),
    PARTITIONBY ( Table1[ID] )
)

Helpful resources

Announcements
LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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