Forum Discussion

KDS's avatar
KDS
Helper I
4 years ago
Solved

Parameter with a variable file name

I created parameters that pick up the path and file dates and plug them into my query.     I have a table that pulls in the file path using the following formula: =LEFT(CELL("filename",B1),FIND("["...
  • jennratten's avatar
    4 years ago

    Hello - this is definitely doable. It can be done sucessfully on an ongoing basis if you come up with a consistent file naming convention - for example, based on your sample file name of 7.12.21 ABC Report.xlsx, your naming convention could be '[Date] [FileName].[FileExtension]', and if so, you could set your query evaluate text in the file name that appears after the first space.

     

    Then you could retrieve the file like so...  In the query, I have added comments explaining what's going at each step.

     

    let
        //-------------------------------------------------------------------
        // Set some variables.
        //-------------------------------------------------------------------
        // Path to the file's folder.  Be sure to include the slash at the end.
        folderPath = "Your Path Goes Here\",
        // File extension.
        fileExt = "xlsx",
        // Character in the file name that separates the date and the text to find.
        fileNameDelimiter = "_",
        // Text that appears after the file name delimiter, excluding the file extension.
        fileNameKeyphrase = "demographics_analysis_data",
        //-------------------------------------------------------------------
        // Transformation steps.
        //-------------------------------------------------------------------
        // Retrieve all files in the folder.
        folderContents = Folder.Contents ( folderPath ),
        // Limit the files to only those meeting the file extension criteria.  Evaluate extensions case insensitively.
        filterExtensions = Table.SelectRows(folderContents, each Text.Contains([Extension], fileExt, Comparer.OrdinalIgnoreCase)),
        // Add a new column with text appearing after the delimiter and before the file extension.
        textBtwnDelimiters = Table.AddColumn(filterExtensions, "File Name Keyphrase", each Text.BetweenDelimiters([Name], fileNameDelimiter, [Extension]), type text),
        // Limit the files to only those containing the file name keyphrase.
        // To evaluate case insensitively...
        // Option 1: Use Text.Contains with the optional argument of Comparer.OrdinalIgnoreCase
        // Option 2: Use equals and wrap both the keyphrase variable and the file name text in Text.Lower().
        //filterFileNames = Table.SelectRows(textBtwnDelimiters, each Text.Contains([File Name Keyphrase], fileNameKeyphrase))
        filterFileNames = Table.SelectRows(textBtwnDelimiters, each Text.Lower ( [File Name Keyphrase] ) = Text.Lower ( fileNameKeyphrase )),
        // Create a way to narrow the results to only one file, should multiple files be returned.
        filterFileCreatedDates = Table.SelectRows(filterFileNames, let latest = List.Max(filterFileNames[Date created]) in each [Date created] = latest)
    in
        filterFileCreatedDates