DAX Calculate sum of values - sum

I have a table admissiontbl that records patients admissions.
Every patient has an AdmittedDate, DischargedDAte and LengthOfStay recorded.
PatientID
AdmittedDate
DischargedDate
AdYear
Admonth
LengthOfStay
001
02/01/2010
24/01/2010
2010
01
22
002
12/12/2009
18/12/2009
2009
12
7
003
12/12/2009
19/01/2010
2009
12
38
The LengthOfStay is calculated as DischargedDAte - AdmittedDate
Problem:
I will like to create a measure that sums up the LengthOfStay every month. This will include all patients discharged that month (i.e. including those admitted in the previous month or year).
For Example:
For Jan/2010, we want to calculate the SUM of LengthOfStay for ALL patients having their DischargedDate in Jan/2010. This must include patients having their AdmittedDate =< Jan/2010.
Scenario:
PatientID 003 was admitted on 12/12/2009 and discharged on 19/01/2010 will have his/her LengthOfStay = 38 days when you calculate for Jan/2010.
PatientID 002 was admitted on 12/12/2009 and discharged on 18/12/2009 will have his/her LengthOfStay = 7 days when you calculate for Jan/2010.
PatientID 001 was admitted on 02/01/2010 and discharged on 24/01/2010 will have his/her LengthOfStay = 22 days when you calculate for Jan/2010.
Result:
The SUM of LengthOfStay = 38 + 7 + 22 = 67 days
Note: The measure should take into account the month or year of the admission when calculating the SUM LengthOfStay.
I have tried the below DAX but it's not working
CM LOS Days =
Var CY = MAX(Admissions[Year])
Var CM = MAX(Admissions[Month])
Return
CALCULATE(SUM(Admissions[LengthOfStay]),Admissions[AdYear]= CY && Admissions[AdMonth]<=CM)
Grateful if anyone could help.

Related

How Do I retrieve most Recent record in different years With Date date in different table

I'm working with a database that isn't structured that well and need to retrieve the row with the latest month used in specific years. The main data is stored is stored in the member table and lists one row per member month. The Date for the member month is not specifically stored here but connected by a foreign Date_Key and linked to a Date table. This is where the column for the Year and Month can be derived based on the Date_Key specified in each table. Each row in the Date table represents 1 new month for a year and each of these rows has a unique sequential date_key.
I am using Microsoft SQL Server Studio as the environment
Member Table
MemberKey
Membe_ID
Date_Key
100
1234
89
101
1234
96
102
1234
97
103
1236
96
104
1236
97
Date Table
Date_Key
Year
Month
89
2020
10
90
2020
11
91
2020
12
92
2021
1
93
2021
2
94
2021
3
95
2021
4
96
2021
5
97
2021
6
Looking for the following Results
Member_ID
Year
Month
1234
2020
10
1234
2021
6
1236
2021
6
2020/11 is NOT a date. It is a year/month pair. But it seems like a simple aggregate - select year, max(month) group by year. You join and include member ID so you include that column in the GROUP BY clause to get one row per member per year.
select mbr.Member_ID, dts.Year, max(dts.Month) as Month
from dbo.Members as mbr
inner join dbo.Dates as dts on mbr.Date_Key = dts.Date_Key
group by mbr.Member_ID, dts.Year
order by mbr.Member_ID, dts.Year
;

SQL last 6 months visits

Purpose of the report: Identify patients who did not have dental cleanings in the last 6 months
What would be the best approach to write a sql script?
Patients table
patient_id
patient_name
11
Jason Strong
22
Ryan Smith
33
Casey Hammer
Visits table
v_id
patient_id
reason_visit
date_of_visit
1
11
medical
01/01/2021
2
22
dental cleaning
11/10/2020
3
22
annual
01/01/2021
4
11
dental cleaning
5/10/2021
5
11
annual
5/1/2021
Expected
patient_id
patient_name
22
Ryan Smith
33
Casey Hammer
Casey is on the list because she is not in the visits table meaning she never received a cleaning from our office.
Ryan Smith is on the list because it is time for his cleaning.
I was also thinking what if the patient did not have an appointment in the last 6 months but had an future appointment for dental cleaning. I would want to exclude that.
in postgresql:
select * from Patients p
where not exists (
select 1 from Visits v
where v.patient_id = p.patient_id
and reason_visit = 'dental cleaning'
and date_of_visit < now() - interval '6 month'
)
in sql server replace now() - interval '6 month' with dateadd(month, -6,getdate())
in mysql date_add(now(), interval -6 month)

Readmission of patient through 30 days after first discharge (total 31 days)

I have below sample data in one of my table and I want to find "If the discharge is followed by a readmission through 30 days after first discharge (total 31 days), use the admit date from the first admission and the discharge date from the last discharge".
PatientId ClaimId Admit Date Discharge Date
A001 110001 12/20/2019 1/17/2020
A001 110002 4/30/2020 4/30/2020
A001 110003 4/18/2020 4/30/2020
A001 110004 5/1/2020 5/5/2020
A001 110005 5/8/2020 5/27/2020
A001 110006 8/22/2020 9/20/2020
A001 110007 9/2/2020 9/5/2020
A001 110008 9/21/2020 10/20/2020
A001 110009 10/21/2020 11/19/2020
A001 110010 9/2/2020 9/5/2020
I tried this way but I can get only min of admit date. Not sure how to find Max of discharge date through 30 days after first discharge. Appreciate help.
SELECT A.PatientId,
A.Discharge_Date,
Min(B.Admit_Date) AS MinOfadmitDate,
DATEDIFF(dd,A.Discharge_Date,Min(B.Admit_Date)) AS Day_span
FROM Table1 A
INNER JOIN Table1 AS B ON A.PatientId = B.PatientId
WHERE B.Admit_Date > A.Discharge_Date
GROUP BY A.PatientId, A.Discharge_Date
HAVING DATEDIFF(dd,A.Discharge_Date, Min(B.Admit_Date))<=30
Can any one help to find min of admit date and max of discharge date
through 30 days after first discharge?
I think you can use the analytical function to find the first discharge date and then use aggregate function to find min and max from 30 days as follows:
select t.patientid,
min(admit_date) as min_Admit_date,
max(discharge_date) as max_discharge_date
from (select t.*,
min(discharge_date) over (partition by patientid) as min_d_date
from your_table t) t
where dateadd(d,30,min_d_date) > admit_date

Finding average value of three weekdays, broken down on hours

I have an origin-destination table like this in Bigquery with weekday, date, UTC time/hour and count of trips:
Origin Destination Day Date Time Count
NY Station Downtown Mon 02.09.2019 15 12
NY Station Downtown Mon 02.09.2019 16 10
City libry Eastside Mon 02.09.2019 17 10
NY Station Downtown Tue 03.09.2019 15 8
NY Station Downtown Tue 03.09.2019 16 5
City libry Eastside Tue 03.09.2019 17 5
NY Station Downtown Wed 04.09.2019 15 8
NY Station Downtown Wed 04.09.2019 16 10
City libry Eastside Wed 04.09.2019 17 11
I wish to get the average Count for
each origin-destination pair (NY Station-Downtown and City libry-Eastside)
the average of Monday-Wednesday at each given time
The output should then be something like
Origin Destination Avg_Day Period Time Avg_Count
NY Station Downtown Mon-Wed Week1 (02.09.19-04.09.19) 15 9,33
NY Station Downtown Mon-Wed Week1 (02.09.19-04.09.19) 16 8,33
City libry Eastside Mon-Wed Week1 (02.09.19-04.09.19) 17 8,67
Ignore the Avg_day and Period columns as its just for help/showing for which days and dates i wish to achieve the average for. In other words the aim is to have an idea of the average counts for each origin-destination pair on a normal weekday (in this case defined as mon-wed) on certain hours of the day. The average count of for example the time 15 for NY Station-Downtown pair is 9,33, calculated by taking the average of the count for 15 o'clock at Monday, at Tuesday and at Wednesday (that is the average of 12, 8 and 8).
I have tried variants of CASE and WHERE SQL queries, but not even close to grasping the logic on how to make the query for this so no point in posting any query. Possibly have to create a temporary table also. Can anyone help me? it is HUGELY appreciated
Below is for BigQuery Standard SQL
#standardSQL
select
Origin,
Destination,
'Mon-Wed' AS Avg_Day,
FORMAT('Week%i (%s-%s)', week, min_date, max_date) AS Period,
Time,
Avg_Count
from (
SELECT
Origin,
Destination,
'Mon-Wed' AS Avg_Day,
EXTRACT(WEEK FROM PARSE_DATE('%d.%m.%Y', date)) week,
MIN(date) AS min_date,
MAX(date) AS max_date,
Time,
ROUND(AVG(count), 2) AS Avg_Count
FROM `project.dataset.table`
WHERE day IN ('Mon', 'Tue', 'Wed')
GROUP BY Origin, Destination, Time, week
)
if to apply to sample data from your question - output is
Row Origin Destination Avg_Day Period Time Avg_Count
1 NY Station Downtown Mon-Wed Week35 (02.09.2019-04.09.2019) 15 9.33
2 NY Station Downtown Mon-Wed Week35 (02.09.2019-04.09.2019) 16 8.33
3 City libry Eastside Mon-Wed Week35 (02.09.2019-04.09.2019) 17 8.67

Showing Null Value Based on Three Tables

I’m working on showing employees that have not entered in any hours for a previous week. I’m currently working with three tables. One table is a calendar that has the first date of each week. The week format is Sunday to Saturday. The second table is the list of hours entered. The Time table contains the date the time was entered and the employees name. The third table is the list of all the employees. I can’t seem to get the joins to work how I would like them to. The end result I would like to see that Bob entered time in week 7 and 8, but week 9 is null. Thank you for your help. Its greatly appreciated.
Current Code
SELECT
d.Resource
,SUM(p.Hours) AS Hours
,m.[WeeksSundayToSaturday]
,DatePart(wk, m.[WeeksSundayToSaturday]) AS WeekNumber
FROM CalendarWeeks m
LEFT JOIN [TimeTracking] p ON
(m.[WeeksSundayToSaturday] BETWEEN p.Date AND p.Date + 7)
RIGHT JOIN [DepartmentMembers] d ON
d.Resource = p.CreatedBy
GROUP BY
d.Resource
,m.WeeksSundayToSaturday
Data Tables
Department Members
Name Department
Bob Engineer
Sue HR
John Operations
Time Tracking
Resource Hours Date
Bob 13 2/9/2014
Sue 12 2/10/2014
John 2 2/11/2014
Bob 6 2/12/2014
Bob 8 2/13/2014
John 8 2/14/2014
John 8 2/15/2014
Bob 8 2/16/2014
Bob 1 2/17/2014
Bob 2 2/18/2014
Bob 1 2/19/2014
Bob 8 2/20/2014
Bob 9 2/21/2014
Bob 6 2/22/2014
Sue 8 2/23/2014
John 2 2/24/2014
Calendar
WeeksSundayToSaturday
1/5/2014
1/12/2014
1/19/2014
1/26/2014
2/2/2014
2/9/2014
2/16/2014
2/23/2014
3/2/2014
3/9/2014
3/16/2014
3/23/2014
3/30/2014
Desired Result
Bob
Week 7 = 27
Week 8 = 35
Week 9 = NULL
Your above query is giving compilation error, please try below query i think it will help you
SELECT
d.Resource
,SUM(p.Hours) AS Hours
,m.[WeeksSundayToSaturday]
,DatePart(wk, m.[WeeksSundayToSaturday]) AS WeekNumber
FROM CalendarWeeks m
LEFT JOIN [TimeTracking] p ON (p.Date BETWEEN
m.[WeeksSundayToSaturday] AND Dateadd(d,6, m.[WeeksSundayToSaturday])
RIGHT JOIN [DepartmentMembers] d ON d.Resource = p.CreatedBy
GROUP BY d.Resource ,m.WeeksSundayToSaturday