2

请帮忙!

理想情况下,我真的很想只使用公式来解决这个问题——而不是 VBA 或任何我认为“花哨”的东西。

我为一个奖励持续参与奖金的计划工作。我们有三个(有时更多)参与时间段,这些时间段可能重叠和/或可能有不参与的空间。神奇的数字是连续订婚84天。我们一直在手动检查每条线路(数百条线路),以查看这些时间段加起来是否达到 84 天的连续参与,没有不活动的时间段。

在链接中有一张我们使用的摘要的图片。例如,第 3 行在 3 个时间段中的任何一个中都没有 84 天,但前 2 个时间段加起来包括连续 120 天。日期不会按日期顺序显示 - 例如,早期参与可能会列在第 3 期。

在此处输入图像描述

真的很期待你的建议。

安妮

4

2 回答 2

3

@TomSharpe 向您展示了一种使用公式解决此问题的方法。如果您有超过三个时间段,则必须对其进行修改。

不确定您是否会认为 Power Query 解决方案“太花哨”,但它确实允许无限数量的时间段,如您在示例中所示。

使用 PQ,我们

  • 为每对开始/结束构建所有连续日期的列表
  • 合并每一行的列表,删除重复项
  • 将间隙和孤岛技术应用于每行的结果日期列表
  • 计算每个“岛”的条目数并返回最大值

请注意:我计算了开始日期和结束日期。在您的天数列中,您没有(除了一个实例)。如果您想同时计算两者,请保持代码不变;如果你不这样做,我们可以做一个小的修改

使用 Power Query

  • 创建一个排除第一行合并单元格的表格
  • 以我在屏幕截图中显示的格式重命名表格列,因为表格中的每个列标题必须具有不同的名称。
  • 选择该数据表中的某个单元格
  • Data => Get&Transform => from Table/Range
  • 当 PQ 编辑器打开时:Home => Advanced Editor
  • 记下第 2 行中的表
  • 粘贴下面的 M 代码代替您看到的内容
  • 将第 2 行中的表名称更改回最初生成的名称。
  • 阅读评论并探索Applied Steps以更好地理解算法

编辑M 代码
代码以对日期列表进行排序以处理某些情况

let
    Source = Excel.CurrentWorkbook(){[Name="Table2"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Start P1", type datetime}, {"Comment1", type text}, {"End P1", type datetime}, {"Days 1", Int64.Type}, {"Start P2", type datetime}, {"Comment2", type text}, {"End P2", type datetime}, {"Days 2", Int64.Type}, {"Start P3", type datetime}, {"Comment3", type text}, {"End P3", type datetime}, {"Days 3", Int64.Type}}),

//set data types for columns 1/5/9... and 3/7/11/... as date
dtTypes = List.Transform(List.Alternate(Table.ColumnNames(#"Changed Type"),1,1,1), each {_,Date.Type}),
typed = Table.TransformColumnTypes(#"Changed Type",dtTypes),

//add Index column to define row numbers
rowNums = Table.AddIndexColumn(typed,"rowNum",0,1),

//Unpivot except for rowNum column
    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(rowNums, {"rowNum"}, "Attribute", "Value"),

//split the attribute column to filter on Start/End => just the dates
//then filter and remove the attributes columns
    #"Split Column by Delimiter" = Table.SplitColumn(#"Unpivoted Other Columns", "Attribute", Splitter.SplitTextByEachDelimiter({" "}, QuoteStyle.Csv, false), {"Attribute.1", "Attribute.2"}),
    #"Changed Type1" = Table.TransformColumnTypes(#"Split Column by Delimiter",{{"Attribute.1", type text}, {"Attribute.2", type text}}),
    #"Removed Columns" = Table.RemoveColumns(#"Changed Type1",{"Attribute.2"}),
    #"Filtered Rows" = Table.SelectRows(#"Removed Columns", each ([Attribute.1] = "End" or [Attribute.1] = "Start")),
    #"Removed Columns1" = Table.RemoveColumns(#"Filtered Rows",{"Attribute.1"}),
    #"Changed Type2" = Table.TransformColumnTypes(#"Removed Columns1",{{"Value", type date}, {"rowNum", Int64.Type}}),

//group by row number
//generate date list from each pair of dates
//combine into a single list of dates with no overlapped date ranges for each row
    #"Grouped Rows" = Table.Group(#"Changed Type2", {"rowNum"}, {
        {"dateList", (t)=> List.Sort(
            List.Distinct(
                List.Combine(
                    List.Generate(
                        ()=>[dtList=List.Dates(
                                t[Value]{0},
                                Duration.TotalDays(t[Value]{1}-t[Value]{0})+1 ,
                                #duration(1,0,0,0)),idx=0],
                        each [idx] < Table.RowCount(t),
                        each [dtList=List.Dates(
                                    t[Value]{[idx]+2},
                                    Duration.TotalDays(t[Value]{[idx]+3}-t[Value]{[idx]+2})+1,
                                    #duration(1,0,0,0)),
                                idx=[idx]+2],
                        each [dtList]))))}
            }),

//determine Islands and Gaps
    #"Expanded dateList" = Table.ExpandListColumn(#"Grouped Rows", "dateList"),

//Duplicate the date column and turn it into integers
    #"Duplicated Column" = Table.DuplicateColumn(#"Expanded dateList", "dateList", "dateList - Copy"),
    #"Changed Type3" = Table.TransformColumnTypes(#"Duplicated Column",{{"dateList - Copy", Int64.Type}}),

//add an Index column
//Then subtract the index from the integer date
// if the dates are consecutive the resultant ID column will => the same value, else it will jump
    #"Added Index" = Table.AddIndexColumn(#"Changed Type3", "Index", 0, 1, Int64.Type),
    #"Added Custom" = Table.AddColumn(#"Added Index", "ID", each [#"dateList - Copy"]-[Index]),
    #"Removed Columns2" = Table.RemoveColumns(#"Added Custom",{"dateList - Copy", "Index"}),

//Group by the date ID column and a Count will => the consecutive days
    #"Grouped Rows1" = Table.Group(#"Removed Columns2", {"rowNum", "ID"}, {{"Count", each Table.RowCount(_), Int64.Type}}),
    #"Removed Columns3" = Table.RemoveColumns(#"Grouped Rows1",{"ID"}),

//Group by the Row number and return the Maximum Consecutive days
    #"Grouped Rows2" = Table.Group(#"Removed Columns3", {"rowNum"}, {{"Max Consecutive Days", each List.Max([Count]), type number}}),

//combine the Consecutive Days column with original table
    result = Table.Join(rowNums,"rowNum",#"Grouped Rows2","rowNum"),
    #"Removed Columns4" = Table.RemoveColumns(result,{"rowNum"})
in
    #"Removed Columns4"

在此处输入图像描述

于 2021-07-26T13:39:56.663 回答
3

不幸的是,Gap and Island 似乎不是一个初学者,因为我认为如果没有 VBA 或大量帮助列,您就无法使用它,而且开始日期需要按顺序排列。很遗憾,因为最长的连续任务时间(AKA 最大的岛屿)很容易从 VBA 版本中退出,并且可以说它比下面的数组公式版本更容易理解

继续选项 2,如果您有 Excel 365,则可以使用序列生成特定范围内的日期列表,然后检查每个日期是否属于这样的参与期之一:

=LET(array,SEQUENCE(Z$2-Z$1+1,1,Z$1),
period1,(array>=A3)*(array<=C3),
period2,(array>=E3)*(array<=G3),
period3,(array>=I3)*(array<=K3),
SUM(--(period1+period2+period3>0)))

假设 Z1 和 Z2 包含您感兴趣的日期范围的开始和结束(我使用了 1/1/21 和 31/7/21)。

如果您没有 Excel 365,则可以使用 Row 函数来生成日期列表。我建议使用名称管理器来创建命名范围日期:

=INDEX(Sheet1!$A:$A,Sheet1!$Z$1):INDEX(Sheet1!$A:$A,Sheet1!$Z$2)

在此处输入图像描述

那么公式是:

= SUM(--(((ROW(Dates)>=A3) * (ROW(Dates)<=C3)  +( ROW(Dates)>=E3) * (ROW(Dates)<=G3) + (ROW(Dates)>=I3) * (ROW(Dates)<=K3))>0))

您可能必须使用CtrlShiftEnter或使用 Sumproduct 而不是 Sum 来输入它。

在此处输入图像描述

编辑

正如@Qualia 敏锐地指出的那样,您需要最长的持续参与时间。这可以通过将频率应用于第一个公式来找到:

=LET(array,SEQUENCE(Z$2-Z$1+1,1,Z$1),
period1,(array>=A3)*(array<=C3),
period2,(array>=E3)*(array<=G3),
period3,(array>=I3)*(array<=K3),
onDays,period1+period2+period3>0,
MAX(FREQUENCY(IF(onDays,array),IF(NOT(onDays),array)))
)

non_365 版本变为

=MAX(FREQUENCY(IF((ROW(Dates)>=A3)*(ROW(Dates)<=C3)+(ROW(Dates)>=E3)*(ROW(Dates)<=G3)+(ROW(Dates)>=I3)*(ROW(Dates)<=K3),ROW(Dates)),
IF( NOT(  (ROW(Dates)>=A3)*(ROW(Dates)<=C3)+(ROW(Dates)>=E3)*(ROW(Dates)<=G3)+(ROW(Dates)>=I3)*(ROW(Dates)<=K3) ),ROW(Dates))))

在此处输入图像描述

于 2021-07-26T08:58:44.800 回答