Calculating a difference for lab results for only those with more than one result

%3CLINGO-SUB%20id%3D%22%5C%26quot%3Blingo-sub-3139739%5C%26quot%3B%22%20slang%3D%22%5C%26quot%3Ben-US%5C%26quot%3B%22%3ECalculating%20a%20difference%20for%20lab%20results%20for%20only%20those%20with%20more%20than%20one%20result%26lt%3B%5C%2Flingo-sub%26gt%3B%3CLINGO-BODY%20id%3D%22%5C%26quot%3Blingo-body-3139739%5C%26quot%3B%22%20slang%3D%22%5C%26quot%3Ben-US%5C%26quot%3B%22%3E%3CP%3EHello%2C%26lt%3B%5C%2FP%26gt%3B%3C%2FP%3E%3CP%3EIs%20there%20a%20way%20to%20calculate%20the%20difference%20in%20values%20for%20an%20individual%20only%20if%20there%20is%20more%20than%20one%20value%20for%20a%20particular%20referred%20value%3F%26lt%3B%5C%2FP%26gt%3B%3C%2FP%3E%3CP%3EFor%20example%20I%20have%20the%20test%20results%20for%20over%201000%20patients%20listed%20by%20patient%20ID%2C%20date%20and%20lab%20result.%26nbsp%3B%20I%20only%20want%20to%20calculate%20the%20difference%20in%20the%20first%20lab%20result%20versus%20the%20last%20lab%20result%20but%20some%20patients%20only%20have%20one%20lab%20value%20for%20that%20time%20period%2C%20therefore%20there%20is%20no%20difference%20to%20calculate.%26nbsp%3B%20Below%2C%20patientID%20A%20has%20two%20values%20so%20I%20would%20want%20the%20difference%20between%20the%20two%20dates%2C%20Patient%20B%20has%20multiple%20values%20so%20would%20want%20the%20difference%20between%20the%20first%20and%20last%20date%2C%20and%20Patient%20C%20only%20has%20one%20value%20so%20I%20can't%20calculate%20a%20value.%20Is%20there%20a%20way%20to%20account%20for%20this%3F%26lt%3B%5C%2FP%26gt%3B%3C%2FP%3E%3CP%3EThank%20you.%26lt%3B%5C%2FP%26gt%3B%3C%2FP%3E%3CP%3Epatientid%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3Blabdate%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%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3B%3C%2FTD%3E%3CTD%3E9%2F3%2F2021%26lt%3B%5C%2FTD%26gt%3B%3C%2FTD%3E%3CTD%3E12.7%25%26lt%3B%5C%2FTD%26gt%3B%26lt%3B%5C%2FTR%26gt%3B%3C%2FTD%3E%3C%2FTR%3E%3CTR%3E%3CTD%3EC%26lt%3B%5C%2FTD%26gt%3B%3C%2FTD%3E%3CTD%3E8%2F13%2F2020%26lt%3B%5C%2FTD%26gt%3B%3C%2FTD%3E%3CTD%3E5.2%25%26lt%3B%5C%2FTD%26gt%3B%26lt%3B%5C%2FTR%26gt%3B%26lt%3B%5C%2FTBODY%26gt%3B%26lt%3B%5C%2FTABLE%26gt%3B%26lt%3B%5C%2Flingo-body%26gt%3B%3CLINGO-LABS%20id%3D%22%5C%26quot%3Blingo-labs-3139739%5C%26quot%3B%22%20slang%3D%22%5C%26quot%3Ben-US%5C%26quot%3B%22%3E%3CLINGO-LABEL%3EFormulas%20and%20Functions%26lt%3B%5C%2Flingo-label%26gt%3B%26lt%3B%5C%2Flingo-labs%26gt%3B%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3C%2FTD%3E%3C%2FTR%3E%3C%2FTBODY%3E%3C%2FTABLE%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3C%2FLINGO-SUB%3E
New Contributor

Hello,

Is there a way to calculate the difference in values for an individual only if there is more than one value for a particular referred value?

For example I have the test results for over 1000 patients listed by patient ID, date and lab result.  I only want to calculate the difference in the first lab result versus the last lab result but some patients only have one lab value for that time period, therefore there is no difference to calculate.  Below, patientID A has two values so I would want the difference between the two dates, Patient B has multiple values so would want the difference between the first and last date, and Patient C only has one value so I can't calculate a value. Is there a way to account for this?

Thank you.

patientid       labdate           labvalue

A2/27/20205.6%
A9/10/20215.7%
B6/8/20209.1%
B9/26/202010.5%
B3/27/202111.2%
B9/3/202112.7%
C8/13/20205.2%
10 Replies

@cklingel 

Alternatively, in D2:

 

=IF(AND(B2=MAXIFS($B$2:$B$7000,$A$2:$A$7000,A2),COUNTIF($A$2:$A$7000,A2)>1),INDEX($C$2:$C$7000,MATCH(MAXIFS($B$2:$B$7000,$A$2:$A$7000,A2),$B$2:$B$7000,0))-INDEX($C$2:$C$7000,MATCH(MINIFS($B$2:$B$7000,$A$2:$A$7000,A2),$B$2:$B$7000,0)),"")

 

Adjust the ranges if you have more than 7000 rows, then fill down

@cklingel 

=XLOOKUP(D2,$A$2:$A$8,$B$2:$B$8,,,-1)-XLOOKUP(D2,$A$2:$A$8,$B$2:$B$8,,,)

This formula works as shown in my spreadsheet.

In D2 i entered the formula:

=UNIQUE(A2:A8)

 

Thank you for your quick help. When I run the D2 formula I received #SPILL errors so after correcting received values not consistent with what they should be. For instance, Patient A had two results so his result difference should have been 0.1% but I am getting 561?
patientid labdate labvalue Column1
A 2/27/2020 0.056 37647 561.00
A 9/10/2021 0.057

@Quadruple_Pawn 

Here's the my data table using your data if that's what you want then let me know i'll send over the formulas

tana96799_1-1644360471592.png

 

@cklingel 

fyi im using xl v.2013. here's what the data and the rpt looks like. you'll have to do minor calc on the side but it's really easy...not sure if you wanted to subtract the 1st lab test from the last or vice versa, in my rpt your subtracting the 1st lab result from the latest

tana96799_0-1644361863513.png

 

Hello,
Yes, this is exactly what I am looking for. Thank you!

@cklingel 

sorry im new to this platform, but how do I attach the excel worksheet plse?

@tana96799  In the message field you can expand the toolbar and attach files with the paper clip icon

cklingel_0-1644422046065.png

 

@cklingel Hi, i just sent it to your email address plse let me know

 

Thanks