Forum Discussion

EmQuery's avatar
EmQuery
Frequent Visitor
3 years ago
Solved

Expression.Error: We cannot apply field access to the type Duration

Still expanding on learning M.

 

I have come across an issue I can't resolve.

 

My original data has Duration values, but some of them start with a colon (":"). In order to fix that, I create a formula to remove the colon. When I try to create a final column to pull in the final amounts, I get error "Expression.Error: We cannot apply field access to the type Duration". I am at a loss of what else to try. Any advise is highly appreciated. Thank you!

 

Code is:

let

    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],

    #"Removed Top Rows" = Table.Skip(Source,2),

    #"Promoted Headers" = Table.PromoteHeaders(#"Removed Top Rows", [PromoteAllScalars=true]),

    #"Removed Other Columns" = Table.SelectColumns(#"Promoted Headers",{"Agent Name", "Login ID", "Login Time", "Logout Time", "Login Duration"}),

 

    //Correct the values starting with Colon

    #"Added Custom" = Table.AddColumn(#"Removed Other Columns", "Duration Fix", each if Text.Start([Login Duration], 1) = ":" then "00:"&Text.End([Login Duration], Text.Length([Login Duration]) -1) else [Login Duration]),

 

   // Try Duration Fix

    #"Added Custom1" = Table.AddColumn(#"Added Custom", "Try Duration Fix", each try [Duration Fix]),

    #"Changed Type" = Table.TransformColumnTypes(#"Added Custom1",{{"Agent Name", type text}, {"Login ID", Int64.Type}, {"Login Time", type time}, {"Logout Time", type time}, {"Duration Fix", type duration}, {"Login Duration", type duration}}),

    #"Added Custom3" = Table.AddColumn(#"Changed Type", "Final Duration", each if [Try Duration Fix][HasError] then [Login Duration] else [Duration Fix][Value])

in

    #"Added Custom3"

 

  • You have: [Duration Fix][Value]

    It should be:

    [Try Duration Fix][Value]

    or

    [Duration Fix]

     

    P.S. This can be simplified to 

    try [Duration Fix] otherwise [Login Duration]

2 Replies

  • artemus's avatar
    artemus
    Microsoft Employee

    You have: [Duration Fix][Value]

    It should be:

    [Try Duration Fix][Value]

    or

    [Duration Fix]

     

    P.S. This can be simplified to 

    try [Duration Fix] otherwise [Login Duration]
  • EmQuery's avatar
    EmQuery
    Frequent Visitor

    Thank you, Sir!! Much appreciated. Worked like a charm.