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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
ranjitsingh
Advocate I
Advocate I

Date of Last Visit by Key Value

Folks, I have a table with 2 columns:- Patient# and Date of Last Visit. Unfortunately, Date of Last Visit has multiple dates for each patient#. I want to keep only the most recent date per unique patient#. i.e. For each Patient#, return latest date.

It would be preferable to do this in query editor(M) if not then Data view (DAX). 

 

Thanks!

 

Patient#Date of Last Visit
1001/1/2020
10012/11/2019
2002/15/2018
2007/4/2019
2007/19/2019
3008/8/2020
3003/2/2020
4007/7/2016
4005/26/2016

 

@lbendlin @selimovd @CNENFRNL

1 ACCEPTED SOLUTION
selimovd
Super User
Super User

Hey @ranjitsingh ,

 

if you want to do it in Power Query that's totally possible.

In Power Query copy the original query and group by patient and Max value of the date of last visit. Here is a tutorial how to do that:

How to Group By Maximum Value using Table.Max - Power Query (gorilla.bi)

 

Afterwards join this grouped table to the original one. Then you have the max date per Patient.

 

If you need any help please let me know.
If I answered your question I would be happy if you could mark my post as a solution ✔️ and give it a thumbs up 👍
 
Best regards
Denis
 

View solution in original post

4 REPLIES 4
lbendlin
Super User
Super User

let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("Xc3JDcAgDETRXnxGGtsQDLUg+m+DJShGuT7N6LdGwkyBBAJlZerhI4UslLpRN057lpXLDOk/M0h1i9sKiideilCndJ62jvmiGczH+gA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Patient#" = _t, #"Date of Last Visit" = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Date of Last Visit", type date}}),
#"Sorted Rows" = Table.Sort(#"Changed Type",{{"Patient#", Order.Ascending}, {"Date of Last Visit", Order.Descending}}),
#"Removed Duplicates" = Table.Distinct(#"Sorted Rows", {"Patient#"})
in
#"Removed Duplicates"

Thanks so much! This query is mostly correct (other than Patient# 200). Added grouping to make it work...

 

let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("Xc3JDcAgDETRXnxGGtsQDLUg+m+DJShGuT7N6LdGwkyBBAJlZerhI4UslLpRN057lpXLDOk/M0h1i9sKiideilCndJ62jvmiGczH+gA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Patient#" = _t, #"Date of Last Visit" = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Date of Last Visit", type date}}),
#"Sorted Rows" = Table.Sort(#"Changed Type",{{"Patient#", Order.Ascending}, {"Date of Last Visit", Order.Descending}}),
#"Grouped Rows" = Table.Group(#"Sorted Rows", {"Patient#"}, {{"Real DOFLV", each List.Max([Date of Last Visit]), type nullable date}})
in
#"Grouped Rows"

selimovd
Super User
Super User

Hey @ranjitsingh ,

 

if you want to do it in Power Query that's totally possible.

In Power Query copy the original query and group by patient and Max value of the date of last visit. Here is a tutorial how to do that:

How to Group By Maximum Value using Table.Max - Power Query (gorilla.bi)

 

Afterwards join this grouped table to the original one. Then you have the max date per Patient.

 

If you need any help please let me know.
If I answered your question I would be happy if you could mark my post as a solution ✔️ and give it a thumbs up 👍
 
Best regards
Denis
 

Thanks so much! This worked for me.

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

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.