簡體   English   中英

具有條件的單列中各行之間的 Power BI 日期差異

[英]Power BI date difference between rows in a single column with conditions

我有一個包含以下列的表格:帳戶、當前階段、舊階段、新階段、更改日期(請參見下面的示例)。 我需要在帳戶上找到每個階段長度的 datediff。 Dax 或 Power Query,以更容易的為准。 甚至有可能得到下面示例中看到的結果嗎?

Account   Current Stage   Old Stage    New Stage    Change Date    Stage Length(days)
ABC       Testing         Demo         Testing      1/1/2022           31
ABC       Testing         Testing                   2/1/2022    
XYZ       Completed       Demo         Testing      1/1/2022           31
XYZ       Completed       Testing      Completed    2/1/2022           59
XYZ       Completed       Completed                 4/1/2022    

要獲取上一個更改日期,在 Power Query 中,將表與自身合並到 [Account] 和 [Old Stage] 等於 [Account] 和 [New Stage]。 從合並中提取 [Change Date]。

合並

然后,您可以使用 Power Query 或 DAX 來計算兩個日期之間的差異。 以下是如何在 Power Query 中使用 Duration.Days function:

= Table.AddColumn(#"Expanded Changed Type1", "DateDiff", each Duration.Days([Change Date]-[Previous Change Date]))

結果

這是完整的 M 代碼:

let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WcnRyVtJRCkktLsnMSweyXFJz81EEDPUN9Y0MjIyUYnUwVSNYQGSErDIiMgoo5JyfW5CTWpKaQtBkTPUIhciiBCxBZgORCVx1LAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Account = _t, #"Current Stage" = _t, #"Old Stage" = _t, #"New Stage" = _t, #"Change Date" = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Account", type text}, {"Current Stage", type text}, {"Old Stage", type text}, {"New Stage", type text}, {"Change Date", type date}}),
#"Merged Queries" = Table.NestedJoin(#"Changed Type", {"Account", "Old Stage"}, #"Changed Type", {"Account", "New Stage"}, "Changed Type", JoinKind.LeftOuter),
#"Expanded Changed Type1" = Table.ExpandTableColumn(#"Merged Queries", "Changed Type", {"Change Date"}, {"Previous Change Date"}),
#"Added Custom" = Table.AddColumn(#"Expanded Changed Type1", "DateDiff", each Duration.Days([Change Date]-[Previous Change Date]))
in
    #"Added Custom"

在 Power Query 中,您可以

  • 按帳戶分組
  • 向每個子表添加一列,使用 List.Generate function 計算從一行到下一行的天差
  • 重新展開表格
let

//change next line to reflect your actual data source
    Source = Excel.CurrentWorkbook(){[Name="Table20"]}[Content],

    #"Grouped Rows" = Table.Group(Source, {"Account"}, {
        {"With Stage Length", (t)=>
            Table.FromColumns(
                Table.ToColumns(t) & {
                    List.Generate(
                        ()=>[d=try Duration.Days(t[Change Date]{1} - t[Change Date]{0}) otherwise null, idx = 0],
                        each [idx] < Table.RowCount(t),
                        each [d=try Duration.Days(t[Change Date]{[idx]+2} - t[Change Date]{[idx]+1}) otherwise null, idx = [idx]+1],
                        each [d])},
                            {"Account","Current Stage","Old Stage","New Stage","Change Date","Stage Length (days)"})}       
        }),
    #"Expanded With Stage Length" = Table.ExpandTableColumn(#"Grouped Rows", "With Stage Length", {"Current Stage", "Old Stage", "New Stage", "Change Date", "Stage Length (days)"}, {"Current Stage", "Old Stage", "New Stage", "Change Date", "Stage Length (days)"}),
    #"Changed Type" = Table.TransformColumnTypes(#"Expanded With Stage Length",{{"Account", type text},{"Current Stage", type text}, {"Old Stage", type text}, {"New Stage", type text}, {"Change Date", type date}, {"Stage Length (days)", Int64.Type}})
in
    #"Changed Type"

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM