Tableau Calculated Field Help

Tableau newbie here would really appreciate some help with a calculated field.

Let's say I have some data:

Patient ID Appointment Date
10001 1/1/2019
10001 4/5/2018
10001 2/3/2021
10002 3/2/2019
10002 3/5/2018
10003 1/1/2019
10003 1/3/2021

I want to get the previous appointment date in a separate column, based on the sorted ascending appointment date per patient.

I'd like my output to be:

Patient ID Appointment Date Previous Appointment Date
10001 1/1/2019 4/5/2018
10001 4/5/2018 NULL
10001 2/3/2021 1/1/2019
10002 3/2/2019 3/5/2018
10002 3/5/2018 NULL
10003 1/1/2019 NUll
10003 1/3/2021 1/1/2019

Eventually, I want to get the average time between each appointment.

I've attempted using LOD functions with window functions, but I can't figure out how to partition based on Patient ID (but I could be doing it wrong). Would anyone have any ideas on how I can achieve the output I want? Thanks in advance!

Topic tableau

Category Data Science

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.