如何从表格中获取最小和最大日期?

时间:2018-10-05 11:49:54

标签: powerbi dax powerbi-desktop

我有一张桌子,如下。我想在power bi中有另一个表,该表可以在特定日期告诉我什么是Min(StartTime)和Max(endTime)。

LogTime   Job_Name   StartTime       endTime
9/18/2018   Job1    9/18/2018 10:27 9/18/2018 10:28
9/18/2018   Job2    9/18/2018 9:47  9/18/2018 9:51
9/18/2018   Job3    9/18/2018 9:41  9/18/2018 9:50
9/18/2018   Job4    9/18/2018 9:46  9/18/2018 9:48
9/17/2018   Job5    9/17/2018 11:23 9/17/2018 11:25
9/17/2018   Job6    9/17/2018 11:17 9/17/2018 11:24
9/16/2018   Job7    9/16/2018 17:20 9/16/2018 17:23
9/16/2018   Job8    9/16/2018 17:18 9/16/2018 17:21
9/16/2018   Job9    9/16/2018 17:17 9/16/2018 17:21
9/14/2018   Job10   9/14/2018 17:19 9/14/2018 17:21
9/14/2018   Job11   9/14/2018 17:19 9/14/2018 17:20
9/14/2018   Job12   9/14/2018 17:17 9/14/2018 17:20
9/14/2018   Job13   9/14/2018 17:16 9/14/2018 17:20

必需表:

LogTime      StartTime       endTime        endTime-StartTime
9/18/2018   9/18/2018 9:46  9/18/2018 10:28 hh:mm
9/17/2018   9/17/2018 11:17 9/17/2018 11:25 
9/16/2018   9/16/2018 17:17 9/16/2018 17:23 
9/14/2018   9/14/2018 17:16 9/14/2018 17:21 

1 个答案:

答案 0 :(得分:0)

您可以使用Power Query Editor来实现。我命名了起点(您的数据)baseTable,并将其用作targetTable(您的必需表)的源。

enter image description here

这些步骤是:

  1. 在“转换”选项卡上使用[分组依据](高级)。按“ LogTime”分组。进行两个聚合(minTime和maxTime“)。操作:所有行。
  2. 添加两个“自定义列”,以提取StartTime和EndTime记录。 (请参见脚本)
  3. 在两列中展开记录
  4. 创建一个自定义列(从EndTime中减去StartTime)。将数据类型更改为持续时间
  5. 删除列[MinTime]和[MaxTime]

脚本如下:

let
Source = #"baseTable",
#"Grouped Rows" = Table.Group(Source, {"LogTime"}, {{"minTime", each _, type table}, {"maxTime", each _, type table}}),
#"Added Custom" = Table.AddColumn(#"Grouped Rows", "minStart", each Table.Min([minTime],"StartTime")),
#"Added Custom1" = Table.AddColumn(#"Added Custom", "maxEnd", each Table.Max([maxTime],"endTime")),
#"Expanded minStart" = Table.ExpandRecordColumn(#"Added Custom1", "minStart", {"StartTime"}, {"StartTime"}),
#"Expanded maxEnd" = Table.ExpandRecordColumn(#"Expanded minStart", "maxEnd", {"endTime"}, {"endTime"}),
#"Added Custom2" = Table.AddColumn(#"Expanded maxEnd", "endTime-StartTime", each [endTime]-[StartTime]),
#"Changed Type" = Table.TransformColumnTypes(#"Added Custom2",{{"StartTime", type time}, {"endTime", type time}, {"endTime-StartTime", type duration}}),
#"Removed Columns" = Table.RemoveColumns(#"Changed Type",{"minTime", "maxTime"})
in
#"Removed Columns"

将此脚本复制到“高级编辑器”时,使用空白查询新来源,您应该能够查看“应用步骤”。