Forum Discussion

daez12's avatar
daez12
Frequent Visitor
3 years ago
Solved

Unpivot Date Column - First Post

Hi Power BI Community!

 

This is my first post to this forum, so please let me know if I need to make any adjustments. 

 

Essentially what I'm trying to do is take each unique ATM ID and show status by date over the last 30 days, with each date/status in their own columns. For example:

 

ATM IDDate 30 Days AgoStatus 30 Days AgoDate 29 Days AgoStatus 29 Days AgoDate 28 Days AgoStatus 28 Day Agoetc.etc.etc.
11/21/23Online1/22/23Erroring1/23/23Online   
21/21/23Erroring1/22/23Erroring1/23/23Online   

 

Here is a screenshot of what I've done so far. This Database table is a snapshot by day of status for each ATM. I've filtered the snapshot column to only be within the last 30 days. Thoughts on what I can do here?

 

  • let
    Source = Sql.Database("pmgsql.database.windows.net", "btmdata"),
    dbo_MachineStatusSnapshots = Source{[Schema="dbo",Item="MachineStatusSnapshots"]}[Data],
    #"Filtered Rows" = Table.SelectRows(dbo_MachineStatusSnapshots, each Date.IsInPreviousNDays([Snapshot], 30)),
    #"Removed Other Columns" = Table.SelectColumns(#"Filtered Rows",{"AtmId", "Status", "Snapshot"}),
    #"Inserted Date" = Table.AddColumn(#"Removed Other Columns", "Date", each Date.From([Snapshot]), type date),
    #"Removed Columns" = Table.RemoveColumns(#"Inserted Date",{"Date"}),

    NewStep=Table.FromRecords(Table.Group(PreviousStepName,"Atmid",{"n",each let a=Date.From(DateTime.LocalNow()),b=Table.TransformColumns(_,{"Snapshot",Date.From}) in Record.FromTable(Table.FillDown(#table({"Name","Value"],{{"Atmid",a[Atmid]{0}}}& List.TransformMany({0..29},each {"Date","Status"},(x,y)=>{y&" "&Text.From(30-x)&" Days Ago",if y="Date" then Date.AddDays(a,x-30) else b{[Snapshot=Date.AddDays(a,x-30)]}?[Status]?})),{"Value"}))})[n])
    in
    NewStep

7 Replies

  • wdx223_Daniel's avatar
    wdx223_Daniel
    Community Champion

    NewStep=Table.FromRecords(Table.Group(PreviousStepName,"Atmid",{"n",each let a=Date.From(DateTime.LocalNow()),b=Table.TransformColumns(_,{"Snapshot",Date.From}) in Record.FromTable(Table.FillDown(#table({"Name","Value"],{{"Atmid",a[Atmid]{0}}}& List.TransformMany({0..29},each {"Date","Status"},(x,y)=>{y&" "&Text.From(30-x)&" Days Ago",if y="Date" then Date.AddDays(a,x-30) else b{[Snapshot=Date.AddDays(a,x-30)]}?[Status]?})),{"Value"}))})[n])

    • daez12's avatar
      daez12
      Frequent Visitor

      I'm excited to try this! Thank you for your response. How can I go about adding this? Here is my current advanced editor in power query:

      let
      Source = Sql.Database("pmgsql.database.windows.net", "btmdata"),
      dbo_MachineStatusSnapshots = Source{[Schema="dbo",Item="MachineStatusSnapshots"]}[Data],
      #"Filtered Rows" = Table.SelectRows(dbo_MachineStatusSnapshots, each Date.IsInPreviousNDays([Snapshot], 30)),
      #"Removed Other Columns" = Table.SelectColumns(#"Filtered Rows",{"AtmId", "Status", "Snapshot"}),
      #"Inserted Date" = Table.AddColumn(#"Removed Other Columns", "Date", each Date.From([Snapshot]), type date),
      #"Removed Columns" = Table.RemoveColumns(#"Inserted Date",{"Date"})
      in
      #"Removed Columns"

      • wdx223_Daniel's avatar
        wdx223_Daniel
        Community Champion

        let
        Source = Sql.Database("pmgsql.database.windows.net", "btmdata"),
        dbo_MachineStatusSnapshots = Source{[Schema="dbo",Item="MachineStatusSnapshots"]}[Data],
        #"Filtered Rows" = Table.SelectRows(dbo_MachineStatusSnapshots, each Date.IsInPreviousNDays([Snapshot], 30)),
        #"Removed Other Columns" = Table.SelectColumns(#"Filtered Rows",{"AtmId", "Status", "Snapshot"}),
        #"Inserted Date" = Table.AddColumn(#"Removed Other Columns", "Date", each Date.From([Snapshot]), type date),
        #"Removed Columns" = Table.RemoveColumns(#"Inserted Date",{"Date"}),

        NewStep=Table.FromRecords(Table.Group(PreviousStepName,"Atmid",{"n",each let a=Date.From(DateTime.LocalNow()),b=Table.TransformColumns(_,{"Snapshot",Date.From}) in Record.FromTable(Table.FillDown(#table({"Name","Value"],{{"Atmid",a[Atmid]{0}}}& List.TransformMany({0..29},each {"Date","Status"},(x,y)=>{y&" "&Text.From(30-x)&" Days Ago",if y="Date" then Date.AddDays(a,x-30) else b{[Snapshot=Date.AddDays(a,x-30)]}?[Status]?})),{"Value"}))})[n])
        in
        NewStep

  • Hi,

    it is not clear to me which is your table and which is your desired result

     

    • daez12's avatar
      daez12
      Frequent Visitor

      Thank you for your reply! I, unfortunately, haven't found a solution yet.

       

      To clarity, the 1st table in the post is my desired result. The screenshot below it is my current table / Power Query screen.

  • daez12's avatar
    daez12
    Frequent Visitor

    Hi David - I corrected the error and was able to run the step you identified. It did make the ATM Id error out, but I was able to replicate the solution using the matrix visual. I appreciate your willingness to solve this problem with me, and would greatly appreciate your help/advice in the future.