根据条件增加日期栏

时间:2018-12-17 10:54:25

标签: powerbi

我在处理查询方面遇到一些问题,因此将不胜感激。

我有一个需要根据其他两个列值增加的日期列。

例如报告日期栏-2018年12月17日

如果我的影响列=“紧急”而我的部门=“商店”,我需要将“报告日期”列增加到18/12/2018

但是,如果我的影响列=“标准”和部门=“地板”,我需要将“报告日期”列增加到20/12/208

理想情况下,我不希望触摸原始的“报告日期”列,而是将这个新值移动到另一列。

到目前为止,我已经创建了一个自定义列,这是我的代码,但是它不起作用。

AmendedDateReported = if(And(SurveyCorrectiveAction[Impact] = "Urgent", SurveyCorrectiveAction[LookUp] = "Stores"), Date.AddDays([DateReported],1),Blank ())

谢谢

宝拉

更新的代码,该公式似乎正常,但日期部分不会更新:

#"Sorted Rows" = Table.Sort(Source,{{"DateReported", Order.Ascending}}),
#"Changed Type" = Table.TransformColumnTypes(#"Sorted Rows",{{"DateReported", type date}}),
#"Sorted Rows1" = Table.Sort(#"Changed Type",{{"DateReported", Order.Descending}}),
#"Added Custom" = Table.AddColumn(#"Sorted Rows1", "Date Repaired", each ""),
#"Changed Type1" = Table.TransformColumnTypes(#"Added Custom",{{"Date Repaired", type text}}),
#"Duplicated Column" = Table.DuplicateColumn(#"Changed Type1", "DateReported", "DateReported - Copy"),
#"Renamed Columns" = Table.RenameColumns(#"Duplicated Column",{{"DateReported - Copy", "AmendedDateReported"}}),

#"Merged Amendments" = Table.NestedJoin(#"Renamed Columns",{"Impact", "Department"},TLU_FaultTimeScales,{"Impact", "Department"},"TLU_FaultTimeScales",JoinKind.LeftOuter),
 #"Expanded Amendments" = Table.ExpandTableColumn(#"Merged Amendments", "TLU_FaultTimeScales", {"Amendment Day"}, {"Amendment Day"}),
  AmendedDateReported = Table.AddColumn(#"Expanded Amendments", "AmendedDateReported", each try Date.AddDays([DateReported],[Amendment Day]) otherwise [DateReported], type date)
 in 
 #"Renamed Columns"

1 个答案:

答案 0 :(得分:1)

您可以尝试:

AmendedDateReported = 
    Table.AddColumn(
        #"Previous Step", 
        "Amended Date Reported", 
        each Date.AddDays(
            [Date Reported], 
            if [Impact] = "Urgent" and [Department] = "Stores" then 1 
            else if [Impact] = "Standard" and [Department] = "Floor" then 3 
            else 0 
        ), 
        type date
    )

如果影响/部门的几种组合对修改日期有不同的影响,则将它们放在单独的表中会更有意义:

+----------+------------+----------------+
|  Impact  | Department | Amendment Days |
+----------+------------+----------------+
| Urgent   | Stores     |              1 |
| Standard | Floor      |              3 |
+----------+------------+----------------+

然后您可以加入此表以获取修订日期:

    #"Merged Amendments" = Table.NestedJoin(#"Previous Step",{"Impact", "Department"},tblAmendments,{"Impact", "Department"},"tblAmendments",JoinKind.LeftOuter),
    #"Expanded Amendments" = Table.ExpandTableColumn(#"Merged Amendments", "tblAmendments", {"Amendment Days"}, {"Amendment Days"}),
    AmendedDateReported = Table.AddColumn(#"Expanded Amendments", "Amended Date Reported", each try Date.AddDays([Date Reported],[Amendment Days]) otherwise [Date Reported], type date)
in
    AmendedDateReported

记住要在in子句之后更新最终变量名。