【发布时间】:2020-02-04 20:39:35
【问题描述】:
我从事医疗保健工作,需要制作一份报告,显示不同时间点的患者实验室值。时间点如下:
移植前:
1 年 = 365 天 +/- 30 天
3 个月 = 90 天 +/- 14 天
1 个月 = 30 天 +/- 7 天
移植后:
1 天 = 24 小时 +/- 12 小时
1 周 = 7 天 +/- 1 天
1 个月 = 30 天 +/- 7 天
3 个月 = 90 天 +/- 14 天
6 个月 = 180 天 +/- 30 天
1 年 = 365 天 +/- 30 天
我的数据模型有很多表(来自 SQL Server 查询的结果),但主实验室表如下所示:
+-----------------------+-----------------+------------+-----------+
| Order ID | Episode ID | Transplant Date | Lab Date | Lab Value |
+----------+------------+-----------------+------------+-----------+
| 111 | 222 | 5/2/2018 | 1/22/2018 | 23 |
| 112 | 222 | 5/2/2018 | 1/27/2018 | 15 |
| 113 | 222 | 5/2/2018 | 5/3/2018 | 14 |
| 114 | 222 | 5/2/2018 | 10/19/2018 | 12 |
| 115 | 223 | 1/23/2019 | 1/24/2019 | 20 |
| 116 | 223 | 1/23/2019 | 1/25/2019 | 25 |
| 117 | 223 | 1/23/2019 | 1/31/2019 | 29 |
| 118 | 223 | 1/23/2019 | 4/23/2019 | 30 |
| 119 | 223 | 1/23/2019 | 3/1/2019 | 35 |
| 120 | 224 | 7/19/2019 | 7/19/2018 | 5 |
| 121 | 224 | 7/19/2019 | 7/24/2018 | 13 |
+-----------------------+-----------------+------------+-----------+
Order ID 是实验室的唯一标识符,Episode ID 是患者的唯一标识符,我们正在寻找与 Transplant Date 相关的实验室。
还有一个患者数据表,如下所示:
+------------+----------------+-----------------+
| Episode ID | Patient Name | Transplant Date |
+------------+----------------+-----------------+
| 222 | Alphers, Ralph | 5/2/2018 |
| 223 | Bethe, Hans | 1/23/2019 |
| 224 | Gammow, George | 7/19/2019 |
+------------+----------------+-----------------+
生成的数据应如下所示:
+------------+------------+--------------+-------------+------------+-------------+--------------+---------------+-------------+
| Episode ID | 1 year pre | 3 months pre | 1 month pre | 1 day post | 1 week post | 1 month post | 6 months post | 1 year post |
+------------+------------+--------------+-------------+------------+-------------+--------------+---------------+-------------+
| 222 | | 15 | | 14 | | | 12 | |
| 223 | | | | 20 | 29 | 35 | | |
| 224 | 5 | | | | | | | |
+------------+------------+--------------+-------------+------------+-------------+--------------+---------------+-------------+
考虑到处理时间(用户体验)和开发复杂性,有没有最好的方法?
现在,我就是这样做的。
首先,我使用 Power Query (M) 创建时间点(例如 Table.AddColumn(#"Changed Type", "Minutes to One Year Before Transplant", each Number.Abs(Duration.TotalMinutes(([Lab Date] - DateTime.From(Date.AddYears([Transplant Date], -1)))))))。
然后,我使用 DAX 查找最接近正确目标日期的记录的天数:
Labs shortest minutes to one year before transplant =
VAR EpisodeID = Patients[Episode ID]
VAR TargetDate = DATEADD(Patients[Transplant Date], 1, MONTH)
VAR WindowDays = 30
RETURN
CALCULATE(
MIN(Labs[Minutes to One Month After Transplant]),
FILTER(Labs, Labs[Episode ID] = EpisodeID),
FILTER(Labs, Labs[Lab Date] >= DATEADD(TargetDate, -WindowDays, DAY)),
FILTER(Labs, Labs[Lab Date] <= DATEADD(TargetDate, WindowDays, DAY))
)
然后,我使用该分钟数作为标识符来获取Order ID
Lab Order ID closest to one year before transplant =
VAR EpisodeID = Patients[Episode ID]
VAR TargetDate = DATEADD(Patients[Transplant Date], 1, MONTH)
VAR WindowDays = 30
VAR DaysFrom = Patients[Labs shortest minutes to one year before transplant]
RETURN
CALCULATE(
MIN(Labs[Order ID]),
FILTER(Labs, Labs[Episode ID] = EpisodeID),
FILTER(Labs, Labs[Lab Date] >= DATEADD(TargetDate, -WindowDays, DAY)),
FILTER(Labs, Labs[Lab Date] <= DATEADD(TargetDate, WindowDays, DAY))
)
最后,我可以使用 Order ID 从该实验室获取我想要的任何内容,例如值:
Lab Value closest to one year before transplant =
VAR EpisodeID = Patients[Episode ID]
VAR OrderID = Patients[Lab Order ID closest to one year before transplant]
RETURN
CALCULATE(
MIN(Labs[Value]),
FILTER(Labs, Labs[Episode ID] = EpisodeID),
FILTER(Labs, Labs[Order ID] = OrderID)
)
而且,我需要为 3 个不同的实验室执行此操作,这意味着将这个过程重复 30 次。而且,生成的报告需要一段时间来进行计算。我可以将一堆工作推回 SQL Server,但也许这不是最好的主意?
【问题讨论】:
-
当有多个匹配结果时,你想要哪个?
-
@GordonLinoff
Lab Date和Value的组合对于每个Episode ID应该是唯一的,但如果不是,则只取第一个。
标签: sql powerbi dax powerquery m