Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
7 years ago
Solved

Try Otherwise in M (Error Replacement)

Hello,

 

I got a question. I got a lot of errors because the date field is empty. So I thought a error handling with try otherwise could help me out but how can I implement it? (I just put in 1 column for example.) Maybe someone can give me a hint.

 

let
    Quelle = Csv.Document(File.Contents("\\file\file.csv"),[Delimiter=";", Columns=161, Encoding=1252, QuoteStyle=QuoteStyle.None]),
                #"Höher gestufte Header" = Table.PromoteHeaders(Quelle, [PromoteAllScalars=true]),
                #"Geänderter Typ" = Table.TransformColumnTypes(#"Höher gestufte Header",{{"Zahlungsdatum", type date}}),

                #"Error Date" = Try (#"Geänderter Typ",{{Zahlungsdatum", type date}}) Otherwise Error ""

in
               #"Error Date"

 

  • Anonymous's avatar
    Anonymous
    7 years ago

    It was easier then i thought

     

    #"Replace Error" = Table.ReplaceErrorValues(#"Geänderter Typ", {{"Pat-Geb-Datum", ""}})

     

    But is there a way to make a for each loop for all columns with dates in the whole table? 

4 Replies

  • Anonymous's avatar
    Anonymous
    Not applicable

    Anonymous,

    Please change your code to the following:

    let
        Quelle = Csv.Document(File.Contents("\\file\file.csv"),[Delimiter=";", Columns=161, Encoding=1252, QuoteStyle=QuoteStyle.None]),
                    #"Höher gestufte Header" = Table.PromoteHeaders(Quelle, [PromoteAllScalars=true]),
                    #"Geänderter Typ" = Table.TransformColumnTypes(#"Höher gestufte Header",{{"Zahlungsdatum", type date}}),
    
                    #"Added Custom" = Table.AddColumn(#"Geänderter Typ", "Custom", each try Date.From([Zahlungsdatum]) otherwise null)
    in
        #"Added Custom"
    
    




    Reference:
    https://blog.gbrueckl.at/2013/12/error-handling-in-power-query/

    Regards,
    Lydia

    • Anonymous's avatar
      Anonymous
      Not applicable

      Anonymous

      Is there a possibility to make a Table.TransformColumns instead of Table.AddColumn ?

       

                  #"Error Date" = Table.TransformColumns(#"Geänderter Typ", each try Date.From([Rezeptdatum]) otherwise null) 
                  #"Error Date" = Table.TransformColumns(#"Geänderter Typ", {{"Rezeptdatum", each try type date otherwise null}}) 

       

      • Anonymous's avatar
        Anonymous
        Not applicable

        It was easier then i thought

         

        #"Replace Error" = Table.ReplaceErrorValues(#"Geänderter Typ", {{"Pat-Geb-Datum", ""}})

         

        But is there a way to make a for each loop for all columns with dates in the whole table?