Get the extension from a filepath

Advertisements

I need to test the extension of the imported file as to if it is CSV or XLSX

I am trying

FP = Text.AfterDelimiter([Path], ".", {0, RelativePosition.FromEnd}),
let 
   FilePath = Excel.CurrentWorkbook(){[Name="fp"]}[Content]{0}[TheFilePath],
   FP = Text.AfterDelimiter([FilePath], ".", {0, RelativePosition.FromEnd}),
   Source = Excel.Workbook(File.Contents(FilePath))
in Source

But I get the error

There is an unknown identifier. Did you use the [field] shorthand for a _[field] outside of an 'each' expression?

I did try

 FP = Text.AfterDelimiter(_[FilePath], ".", {0, RelativePosition.FromEnd})

This did not correct the issue

>Solution :

How about this, which finds the part after the last period?

let  FilePath = Excel.CurrentWorkbook(){[Name="fp"]}[Content]{0}[TheFilePath],
Extension=List.Last(Text.Split(FilePath,"."))
in Extension

Leave a Reply Cancel reply