Power BI/Query:在 if 语句的结果中包含来自单元格的值

Power BI/Query: Include value from cell in the result of if statement

我对 Power BI 和 Power Query 还很陌生。我有一个 table,它有三个单独的日期字段,我需要将它们相互比较。我编写了以下有效的自定义列,但我试图让它在返回的值中包含日期,但我无法弄清楚我需要如何引用日期。如果有更优雅的方法来进行比较,我不会感到惊讶,但这就是我想出的方法。我遇到的问题在代码的第 2 行。按照我现在的方式,我只得到“附件日期不匹配。事件日期:[事件日期] 附件日期:[附件日期]”。如果可能的话,我将不胜感激。如果有人能告诉我引文中的部分叫什么,我也将不胜感激。我试图找到这个问题的答案,但我不确定是否应该将其称为值、字符串或其他名称。

if [Event Date] = [Date Last Updated] and [Date Last Updated] = [Attachment Date] then "Dates Match" else
if [Event Date] = [Date Last Updated] and [Date Last Updated] <> [Attachment Date] then "Attachment Date Mismatch. Event Date: [Event Date] Attachment Date: [Attachment Date]" else
if [Event Date] = [Attachment Date] and [Attachment Date] <> [Date Last Updated] then "Upload Date Mismatch" else
if [Attachment Date] = [Date Last Updated] and [Date Last Updated] <> [Event Date] then "Event Date Mismatch" else 
if [Event Date] <> [Attachment Date] and [Attachment Date] <> [Date Last Updated] then "Dates Mismatch" else null

您忘记包含信息以便能够重现您的问题,所以我无法完成,但一般来说,您需要将变量排除在引号内。

那些你想包含日期的子句应该看起来像(假设你的各种日期列是 Date.Type 而不是 Text.Type):

... "Attachment Date Mismatch. Event Date: " & Date.ToText([Event Date],"desired_format") & " Attachment Date: " & Date.ToText([Attachment Date],"desired_format") ...

格式代码的链接可在 Date.ToText 的 MS 帮助文章中找到。一个示例可能是 "dd-MM-yyyy",但还有许多其他示例。

看似很长的公式,也可以用let...in这样缩短

let
    e = [Event Date]
    f = [Date Last Updated]
    g = [Attachment Date]
in 
    if e = f and f = g then "Dates Match" else
    if e = f then "Attachment Date Mismatch. Event Date: " & Date.ToText(e, "M/d/yyyy") & " Attachment Date: " & Date.ToText(g,"M/d/yyyy") else
    if e = g then "Upload Date Mismatch" else
    if f = g then "Event Date Mismatch" else 
    "Dates Mismatch"

M 语言不会自动将不匹配的 data-types 转换为匹配,因此您不能将日期与文本字符串连接,因此需要 Date.ToText