Как создать столбец, который ссылается на значения из предыдущей и текущей строки в других столбцах - PullRequest
1 голос
/ 30 апреля 2020

Я пытаюсь создать вычисляемый столбец «StartTime» из четырех предоставленных столбцов в соответствии со следующими правилами:

**If**   [fldEquipmentID]{current row} = [fldEquipmentID]{previous row}
**and**  [ShiftStartTime]{current row} = [ShiftStartTime]{previous row}
**and**  [ActivityStartTime]{current row} **is** *null*
**then** [ActivityEndTime]{previous row}
**else** [ActivityStartTime]{current row}.

Я включил целевой вывод из рабочей таблицы Excel на прикрепленное изображение.

привет,

let
Source = #table({"fldEquipmentID",  "ShiftStartTime",   "ActivityStartTime", "ActivityEndTime", "IndexCol"},
                {{2,    43886.25,   43886.25,   43886.75,0},
                 {2,    43895.25,   43895.54,   43895.59,1},
                 {2,    43896.25,   43896.54,   43896.60,2},
                 {2,    43899.25,   43899.53,   43899.62,3},
                 {2,    43899.25,   null,       43899.64,4},
                 {2,    43899.25,   null,       43899.65,5},
                 {3,    43886.25,   43886.25,   43886.75,6},
                 {3,    43895.25,   43895.54,   43895.59,7},
                 {3,    43896.25,   43896.54,   43896.60,8},
                 {3,    43899.25,   43899.53,   43899.62,9},
                 {3,    43899.25,   null,       43899.64,10},
                 {3,    43899.25,   null,       43899.65,11},
                 {4,    43886.25,   43886.25,   43886.75,12},
                 {4,    43895.25,   43895.54,   43895.59,13},
                 {4,    43896.25,   43896.54,   43896.60,14},
                 {4,    43899.25,   43899.53,   43899.62,15},
                 {4,    43899.25,   null,       43899.64,16},
                 {4,    43899.25,   null,       43899.65,17}}
                ),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"ShiftStartTime", type datetime}, {"ActivityStartTime", type datetime}, {"ActivityEndTime", type datetime}}),
StartTime = List.Accumulate(List.Skip(#"Changed Type"[IndexCol]),
                                        {#"Changed Type"[ActivityStartTime]{0}},
                                        (result,current) => result & if [fldEquipmentID]{current}=[fldEquipmetID]{current-1} and [ShiftStartTime]{current}= [ShiftStartTime]{current-1} and [ActivityStartTime]{current}=null 
                                                            then [ActivityEndTime]{current-1} 
                                                            else [ActivityStartTime]{current})

в StartTime

Вывод в Excel с вычисленным столбцом

enter image description here

1 Ответ

1 голос
/ 30 апреля 2020

попробуйте заменить нижний ряд на

#"Added Custom" = Table.AddColumn(#"Changed Type", "StartTime", each if [fldEquipmentID]=#"Changed Type"{[IndexCol]-1}[fldEquipmentID] and [ShiftStartTime]=#"Changed Type"{[IndexCol]-1}[ShiftStartTime] and [ActivityStartTime]=null then #"Changed Type"{[IndexCol]-1}[ActivityEndTime] else  [ActivityStartTime])

, который вы также можете изменить, чтобы учитывать, когда IndexCol = 0, например

#"Added Custom" = Table.AddColumn(#"Changed Type", "StartTime", each if [IndexCol] = 0 then "something" else if [fldEquipmentID]=#"Changed Type"{[IndexCol]-1}[fldEquipmentID] and [ShiftStartTime]=#"Changed Type"{[IndexCol]-1}[ShiftStartTime] and [ActivityStartTime]=null then #"Changed Type"{[IndexCol]-1}[ActivityEndTime] else  [ActivityStartTime])

Примечание. Я не думаю, что ваш пример вывода была правильная база по вашему описанному методу

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...