UPDATE: This has been solved. I created a "calculated variable" in tableau called [Lab Volume] and put this code into the calculation:
If [Average Results Per Day] <= 25 THEN 'Low'
ELSEIF [Average Results Per Day] > 25 and [Average Results Per Day] < 75 THEN 'Medium'
ELSEIF [Average Results Per Day] >= 75 THEN 'High'
END
Thank you for your help!
ORIGINAL POST: I am having trouble figuring out how to create new categories of data, sum these categories, and then find how many days since the lab last reported. Basically I have a list of unique [patientIDs], [Lab_Name], [result_reported_date], which I believe are the only variables I need for this project. SQL code will go into Tableau for data viz.
Here is my dilemma:
(1) I need to average the number of reported lab results for each laboratory [lab_names] and then categorize them into three volume categories (low volume labs, medium volume labs, high volume labs) based upon how many samples submitted each day in the last 30-day window: low is <25 samples/day; medium is 25-50 samples/day; high is 75+ samples/day.
Input looks something like this, with mulitple labs over multiple days with a sum of the number of samples submitted. The file has 750 different labs, with dates going back 30+ days, and a sum of the samples submitted on each day.
Lab_Name | Date | Samples_Submitted |
---|---|---|
LabA | 1/01/22 | 80 |
LabB | 1/01/22 | 22 |
LabA | 1/02/22 | 101 |
LabB | 1/01/22 | 12 |
LabA | 1/03/22 | 95 |
LabB | 1/03/22 | 80 |
Output would be something like this:
Lab_Name | Vol_category | Average_sample_per30day |
---|---|---|
LabA | low | 22 |
LabB | medium | 35 |
LabC | high | 85 |
LabD | low | 15 |
LabE | medium | 32 |
This is the code I have to count the number of samples submitted by Lab_Name. I don't know how to average the number of samples submitted over the last 30-days and then create low, medium, high categories.
select
Lab_Name,
count(Lab_Name) as Lab_count
from dbo.Lab_tests
where
result_reported_date > '2022-07-01' and
Test_LOINC in ('41458-1', '94306-8', '94309-2', '94500-6', '94502-2', '94531-1', '94533-7',
'94534-5', '94559-2', '94565-9', '94568-9', '94640-0', '94756-4', '94757-2',
'94759-8', '94760-6', '94845-5', '95406-5', '95409-9', '95423-0', '95425-5',
'95608-6', '96094-8', '96123-5', '96448-6', '96986-5', '99999-9', '94558-4',
'95209-3', '96119-3', '97097-0')
group by Lab_Name
order by Lab_count desc ;