检查系统事件的开放日期

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

我正在构建多个系统的可靠性报告。我正在使用 power query 来准备数据。

源数据仅显示故障是否在任何给定日期开始或结束,但不显示系统是否在其他任何一天已经关闭(或启动),因此我必须生成可用性数据。

我对齐了开始日期和结束日期,然后将每个间隔内的日期标记为不可用。对于所有其他日期,我将它们标记为可用。

此后我发现一些系统在另一个故障开始之前就缺少一个故障结束。换句话说,他们开始出现故障,几天后又开始出现另一次故障(请检查下面示例中的加热器)。这是不可能的,我需要从数据中删除第一个失败的开始,但我无法找到解决方案。

不成对的失败开始/结束只允许在数据的结尾/开始,不允许在中间。

输入示例:

姓名 日期 失败
发电机 2020年1月22日
发电机 2020年1月23日 结束
发电机 2020年1月24日
发电机 2020年1月25日 开始
发电机 2020年1月26日
发电机 27/01/2020 结束
发电机 2020年1月28日
2020年1月22日
2020年1月23日 开始
2020年1月24日
2020年1月25日 结束
2020年1月26日
27/01/2020 开始
2020年1月28日
冷却器 2020年1月22日
冷却器 2020年1月23日 结束
冷却器 2020年1月24日
冷却器 2020年1月25日
冷却器 2020年1月26日
冷却器 27/01/2020 开始
冷却器 2020年1月28日
加热器 2020年1月22日
加热器 2020年1月23日 开始
加热器 2020年1月24日 结束
加热器 2020年1月25日 开始
加热器 2020年1月26日
加热器 27/01/2020 开始
加热器 2020年1月28日 结束

预期输出:

姓名 日期 状态
发电机 2020年1月22日 向下
发电机 2020年1月23日 向下
发电机 2020年1月24日 向上
发电机 2020年1月25日 向下
发电机 2020年1月26日 向下
发电机 27/01/2020 向下
发电机 2020年1月28日 向上
2020年1月22日 向上
2020年1月23日 向下
2020年1月24日 向下
2020年1月25日 向下
2020年1月26日 向上
27/01/2020 向下
2020年1月28日 向下
冷却器 2020年1月22日 向下
冷却器 2020年1月23日 向下
冷却器 2020年1月24日 向上
冷却器 2020年1月25日 向上
冷却器 2020年1月26日 向上
冷却器 27/01/2020 向下
冷却器 2020年1月28日 向下
加热器 2020年1月22日 向上
加热器 2020年1月23日 向下
加热器 2020年1月24日 向下
加热器 2020年1月25日 向上
加热器 2020年1月26日 向上
加热器 27/01/2020 向下
加热器 2020年1月28日 向下

我尝试过在网上寻找解决方案,但很难准确描述这个问题。

powerquery m
1个回答
0
投票

这是 PowerQuery 中的解决方案。

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("hdIxC8MgEAXg/+IciDWxyV5KOxYyhgxC3dpYxP7/dpEXvBczyud59zjnWd386qNLIapGGdPqU2u00f+DWppCu41e16e80FfL7Uan5GKSV87VB4aj/mNZ/vi+PzRXho7OlFXkyWDJJNlEhgxDtZeY/RLCy/OtgNhKoGJ+kN0nEQDEI8BFiLt3aScEiC8B3pOQUP6t4CIOiMeBj2Xj5Qc=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Name = _t, Date = _t, Failure = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Name", type text}, {"Date", type date}, {"Failure", type text}}),
    #"Added Status" = Table.AddColumn(#"Changed Type", "Status", each
        let
          pName = [Name],
          pDate = [Date],
          pFailure = if [Failure] <> null and [Failure] <> "" then [Failure] else null,
          pSearch = Table.SelectRows(#"Changed Type", each [Name] = pName and [Failure] <> null and [Failure] <> ""),
          prevFailureRow = Table.Max(Table.SelectRows(pSearch, each [Date] < pDate), "Date"),
          prevFailure = if prevFailureRow = null then null else prevFailureRow[Failure],
          nextFailureRow = Table.Min(Table.SelectRows(pSearch, each [Date] > pDate), "Date"),
          nextFailure = if nextFailureRow = null then null else nextFailureRow[Failure],
          result = 
            if      pFailure = null and prevFailure = "End" then "Up"
            else if pFailure = null and nextFailure = "Start" then "Up"
            else if pFailure = "Start" and nextFailure = "Start" then "Up"
            else "Down"
        in
          result
    ,type text)
in
    #"Added Status"

© www.soinside.com 2019 - 2024. All rights reserved.