Forum Discussion

JRParker's avatar
JRParker
Helper III
3 years ago
Solved

DataFormat.Error: We couldn't parse the input provided as a Date value. Details:

In PQ, initiated a New Source from a folder with a host of Excel files with the MMDDYYYY embeded in their file names.

 

After combining and transforming, the Source.Name field includes the MMDDYYYY within the respective Excel file names.  

 

After adding a custom colum to extract the MMDDYYYY (via Text.Middle([Source.Name], 16, 8)), one is left with Custom column Type Any.  The error (in the subject line of this correspondence) results when simply trying to transform to Date.  First transforming to either Text or Whole number doesn't solve the issue;  neither does Trim and/or Clean.  Any ideas?

 

Looked at prior posts related to this subject but they're all slightly different context.  

 

Chat GPT couldn't figure it out. 🙂

Thanks in advance. 

  • Just tested and that mmddyyyy text string couldn't be parse as a date even if I added a culture. Try this custom column

    let 
    dt = Text.BetweenDelimiters([Column1], " ", ".x"),
    yr = Number.From(Text.End(dt, 4)),
    mo = Number.From(Text.Start(dt, 2)),
    dy = Number.From(Text.Range(dt,2,2))
    in #date(yr, mo, dy)

     

  • That's just foolproofing which files to get. I or someone else might erroneously save a non-relevant file in those folders.

10 Replies

    • JRParker's avatar
      JRParker
      Helper III

      When getting data from a folder of Excel workbooks, regardless of the underlying structure of the workbooks within the folder, the very first couple of PQ steps are columns of meta data including a source column of the workbook names.  The file name naming convention of the underlying workbooks withing the folder is critical in that a MMDDYYYY is embedded into each file name.  For example, "FILE05312023", "FILE06302023", etc.  The intent of this thread was to find a way to extract the MMDDYYYY out of each file name and include this Date in each row of the respective workbooks in the folder. 

       

      Along with the meta data columns is the Table column which is each workbook to be expanded to include all the underlying fields .  Below is a sample where all the meta data columns have been removed other than the Source and table columns:

      SourceTransform File
      FILE01312023Table
      FILE02282023Table
      FILE03312023Table
      FILE04302023Table
      FILE05312023Table
      FILE06302023Table


      The query would look like this after expanding the table:

       

      SourceField 1Field 2Field 3
      FILE01312023value 1value 1value 1
      FILE01312023value 2value 2value 2
      FILE01312023value 3value 3value 3
      FILE02282023value 4value 4value 4
      FILE02282023value 5value 5value 5
      FILE02282023value 6value 6value 6
      FILE03312023value 7value 7value 7
      FILE03312023value 8value 8value 8
      FILE03312023value 9value 9value 9
      FILE03312023value 10value 10value 10
      FILE04302023value 11value 11value 11
      FILE05312023value 12value 12value 12
      FILE05312023value 13value 13value 13
      FILE05312023value 14value 14value 14
      FILE06302023value 15value 15value 15
      FILE06302023value 16value 16value 16

       

      The intent is to add a custom column by extracting the date from the Source column:

       

      SourceField 1Field 2Field 3Date
      FILE01312023value 1value 1value 11/31/2023
      FILE01312023value 2value 2value 21/31/2023
      FILE01312023value 3value 3value 31/31/2023
      FILE02282023value 4value 4value 42/28/2023
      FILE02282023value 5value 5value 52/28/2023
      FILE02282023value 6value 6value 62/28/2023
      FILE03312023value 7value 7value 73/31/2023
      FILE03312023value 8value 8value 83/31/2023
      FILE03312023value 9value 9value 93/31/2023
      FILE03312023value 10value 10value 103/31/2023
      FILE04302023value 11value 11value 114/30/2023
      FILE05312023value 12value 12value 125/31/2023
      FILE05312023value 13value 13value 135/31/2023
      FILE05312023value 14value 14value 145/31/2023
      FILE06302023value 15value 15value 156/30/2023
      FILE06302023value 16value 16value 166/30/2023

       

      ...then remove the Source column and proceed with addtional query steps.... hope this clarifies.

      • danextian's avatar
        danextian
        Super User

        My style, when connecting to a folder, is to make sure that I am getting data from the right files so I apply filters to the file name (contains or starts with a particular string and doesn't contain $ as this is a temporary file) and the extension. I also keep the filename so I know which file my data is from. Very helpful when I expect some data for a column or more with blank rows  or when I expect this x count of files to be loaded but some are missing.

  • It is possible that the date starts at 15 and not at 16 or any other number.

    • danextian's avatar
      danextian
      Super User

      Just tested and that mmddyyyy text string couldn't be parse as a date even if I added a culture. Try this custom column

      let 
      dt = Text.BetweenDelimiters([Column1], " ", ".x"),
      yr = Number.From(Text.End(dt, 4)),
      mo = Number.From(Text.Start(dt, 2)),
      dy = Number.From(Text.Range(dt,2,2))
      in #date(yr, mo, dy)

       

      • JRParker's avatar
        JRParker
        Helper III

        I don't know enough about M language to embed what you have into my existing query.  This is what I have so far... can you help with embedding?

         

        let
        Source = Folder.Files("C:\FVE TB Folder"),
        #"Filtered Hidden Files1" = Table.SelectRows(Source, each [Attributes]?[Hidden]? <> true),
        #"Invoke Custom Function1" = Table.AddColumn(#"Filtered Hidden Files1", "Transform File (4)", each #"Transform File (4)"([Content])),
        #"Renamed Columns1" = Table.RenameColumns(#"Invoke Custom Function1", {"Name", "Source.Name"}),
        #"Removed Other Columns1" = Table.SelectColumns(#"Renamed Columns1", {"Source.Name", "Transform File (4)"}),
        #"Expanded Table Column1" = Table.ExpandTableColumn(#"Removed Other Columns1", "Transform File (4)", Table.ColumnNames(#"Transform File (4)"(#"Sample File (4)")))
        in
        #"Expanded Table Column1"

    • JRParker's avatar
      JRParker
      Helper III

      Thanks Danextian, have confirmed the date starts at 16, and the result of the custom field is for example '01312023', extracted from the value 'GL_TrialBalance 01312023.xls' from Source.Name column. 

       

      Perhaps it would be best to create a Date custom field in one fell swoop; extract and format at the same time, rather than creating the custom field of characters and then trying to format?