如何查找日期最接近另一个日期的值

问题描述 投票:0回答:2

我从事医疗保健工作,需要制作一份报告,以显示患者在各个时间点的实验室值。时间点如下:

移植前:

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,但这也许不是最好的主意?

sql powerbi dax powerquery m
2个回答
0
投票

我能想到的最简单的方法是为每个时间段创建计算列,然后以所需的任何度量直接使用它们。例如,对于1年以下的年份:

1 Year Pre = IF('Table'[Lab Date]>='Table'[Transplant Date]-395 && 'Table'[Lab Date]<='Table'[Transplant Date]-335,'Table'[LabValue],BLANK())

3个月前:

3 Months Pre = IF('Table'[Lab Date]>='Table'[Transplant Date]-104 && 'Table'[Lab Date]<='Table'[Transplant Date]-76,'Table'[LabValue],BLANK())

同样,您也可以为其他时间段创建计算列,并使用它们来获得所需的视觉效果。希望这会有所帮助。


0
投票

您的所有代码都是M,所以我不确定为什么要用SQL标记它。但是这里是[可能不是最优雅的] SQL解决方案:

create table labs (
    OrderID int not null,
    EpisodeID int not null,
    TransplantDate date not null,
    LabDate date not null,
    LabValue int not null)

insert labs
values 
(111, 222, cast('5/2/2018'  as date), cast('1/22/2018'  as date), 23),
(112, 222, cast('5/2/2018'  as date), cast('1/27/2018'  as date), 15),
(113, 222, cast('5/2/2018'  as date), cast('5/3/2018'   as date), 14),
(114, 222, cast('5/2/2018'  as date), cast('10/19/2018' as date), 12),
(115, 223, cast('1/23/2019' as date), cast('1/24/2019'  as date), 20),
(116, 223, cast('1/23/2019' as date), cast('1/25/2019'  as date), 25),
(117, 223, cast('1/23/2019' as date), cast('1/31/2019'  as date), 29),
(118, 223, cast('1/23/2019' as date), cast('4/23/2019'  as date), 30),
(119, 223, cast('1/23/2019' as date), cast('3/1/2019'   as date), 35),
(120, 224, cast('7/19/2019' as date), cast('7/19/2018'  as date),  5),
(121, 224, cast('7/19/2019' as date), cast('7/24/2018'  as date), 13)

create table patient (
    EpisodeID int not null,
    PatientName varchar(128) not null,
    TransplantDate date not null
)

insert patient
values
(222, 'Alphers, Ralph', cast('5/2/2018'  as date)),
(223, 'Bethe, Hans',    cast('1/23/2019' as date)),
(224, 'Gammow, George', cast('7/19/2019' as date))


select q.EpisodeID
, min(q.[1YrPre]  ) as '1YrPre'
, min(q.[3MoPre]  ) as '3MoPre'
, min(q.[1MoPre]  ) as '1MoPre'
, min(q.[1DayPost]) as '1DayPost'
, min(q.[1WkPost] ) as '1WkPost'
, min(q.[1MoPost] ) as '1MoPost'
, min(q.[3MoPost] ) as '3MoPost'
, min(q.[6MoPost] ) as '6MoPost'
, min(q.[1YrPost] ) as '1YrPost'

from (
    select r.OrderID
    , r.EpisodeID
    , case when r.[1YrPreCheck]   = m.[1YrPreCheck]   and m.[1YrPreCheck]   <= 30 then r.LabValue end as '1YrPre'
    , case when r.[3MoPreCheck]   = m.[3MoPreCheck]   and m.[3MoPreCheck]   <= 14 then r.LabValue end as '3MoPre'
    , case when r.[1MoPreCheck]   = m.[1MoPreCheck]   and m.[1MoPreCheck]   <=  7 then r.LabValue end as '1MoPre'
    , case when r.[1DayPostCheck] = m.[1DayPostCheck] and m.[1DayPostCheck] <=  1 then r.LabValue end as '1DayPost'
    , case when r.[1WkPostCheck]  = m.[1WkPostCheck]  and m.[1WkPostCheck]  <=  1 then r.LabValue end as '1WkPost'
    , case when r.[1MoPostCheck]  = m.[1MoPostCheck]  and m.[1MoPostCheck]  <=  7 then r.LabValue end as '1MoPost'
    , case when r.[6MoPostCheck]  = m.[3MoPostCheck]  and m.[3MoPostCheck]  <= 14 then r.LabValue end as '3MoPost'
    , case when r.[6MoPostCheck]  = m.[6MoPostCheck]  and m.[6MoPostCheck]  <= 30 then r.LabValue end as '6MoPost'
    , case when r.[1YrPostCheck]  = m.[1YrPostCheck]  and m.[1YrPostCheck]  <= 30 then r.LabValue end as '1YrPost'

    from (
        select p.EpisodeID
        , min(abs(datediff(day, l.LabDate, dateadd(year,  -1, p.TransplantDate)))) as '1YrPreCheck'
        , min(abs(datediff(day, l.LabDate, dateadd(month, -3, p.TransplantDate)))) as '3MoPreCheck'
        , min(abs(datediff(day, l.LabDate, dateadd(month, -1, p.TransplantDate)))) as '1MoPreCheck'
        , min(abs(datediff(day, l.LabDate, dateadd(day,    1, p.TransplantDate)))) as '1DayPostCheck'
        , min(abs(datediff(day, l.LabDate, dateadd(day,    7, p.TransplantDate)))) as '1WkPostCheck'
        , min(abs(datediff(day, l.LabDate, dateadd(month,  1, p.TransplantDate)))) as '1MoPostCheck'
        , min(abs(datediff(day, l.LabDate, dateadd(month,  3, p.TransplantDate)))) as '3MoPostCheck'
        , min(abs(datediff(day, l.LabDate, dateadd(month,  6, p.TransplantDate)))) as '6MoPostCheck'
        , min(abs(datediff(day, l.LabDate, dateadd(year,   1, p.TransplantDate)))) as '1YrPostCheck'

        from labs l
          inner join patient p on p.EpisodeID = l.EpisodeID

        group by p.EpisodeID
    ) m
      inner join (
        select l.OrderID
        , p.EpisodeID
        , l.LabValue
        , abs(datediff(day, l.LabDate, dateadd(year,  -1, p.TransplantDate))) as '1YrPreCheck'
        , abs(datediff(day, l.LabDate, dateadd(month, -3, p.TransplantDate))) as '3MoPreCheck'
        , abs(datediff(day, l.LabDate, dateadd(month, -1, p.TransplantDate))) as '1MoPreCheck'
        , abs(datediff(day, l.LabDate, dateadd(day,    1, p.TransplantDate))) as '1DayPostCheck'
        , abs(datediff(day, l.LabDate, dateadd(day,    7, p.TransplantDate))) as '1WkPostCheck'
        , abs(datediff(day, l.LabDate, dateadd(month,  1, p.TransplantDate))) as '1MoPostCheck'
        , abs(datediff(day, l.LabDate, dateadd(month,  3, p.TransplantDate))) as '3MoPostCheck'
        , abs(datediff(day, l.LabDate, dateadd(month,  6, p.TransplantDate))) as '6MoPostCheck'
        , abs(datediff(day, l.LabDate, dateadd(year,   1, p.TransplantDate))) as '1YrPostCheck'

        from labs l
      inner join patient p on p.EpisodeID = l.EpisodeID
    ) r on r.EpisodeID = m.EpisodeID
)q 

group by q.EpisodeID
© www.soinside.com 2019 - 2024. All rights reserved.