Reading csv PowerQuery [Source = try ... otherwise ... ] fails, paths are valid

JackDanIce

Well-known Member
Joined
Feb 3, 2010
Messages
9,922
Office Version
  1. 365
Platform
  1. Windows
Hi,

I'm using my PC and my laptop, both connected to a personal OneDrive account. Microsoft 365 (personal version, non scripts anyway)

I have code like:
Power Query:
let
    Source = try
                Csv.Document(File.Contents("C:\Users\JackDanOneDrive\Work\Karrus Capital\Earthi\Data\Copper Futures Historical Data.csv"), [Delimiter=",", Columns=7, Encoding=65001, QuoteStyle=QuoteStyle.None])
            otherwise
                Csv.Document(File.Contents("C:\Users\Admin\OneDrive\Work\Karrus Capital\Earthi\Data\Copper Futures Historical Data.csv"),[Delimiter=",", Columns=7, Encoding=65001, QuoteStyle=QuoteStyle.None]),
Code is trying to read data from a personal cloud folder, which is mapped to each machine as paths imply, difference 1 folder name JackDan vs Admin.

When I open this file on my laptop, the csv file loads in fine.
When I open it on my pc, the csv file does not load in, nor does the laptop equivalent.

If I switch positions of the paths in the statement, close, then open on each machine, I get the opposite behaviour, csv data loads file into PC, but not laptop
Power Query:
let
    Source = try
                Csv.Document(File.Contents("C:\Users\\OneDrive\Work\Data\Copper Futures Historical Data.csv"),[Delimiter=",", Columns=7, Encoding=65001, QuoteStyle=QuoteStyle.None])
            otherwise
                Csv.Document(File.Contents("C:\Users\JackDan[/COLOR]\OneDrive\Work\Data\Copper Futures Historical Data.csv"), [Delimiter=",", Columns=7, Encoding=65001, QuoteStyle=QuoteStyle.None]) ,
First reaction is I have syntax wrong, but I am not getting any syntax errors
Second reaction, I have the paths wrong, but if I use Data->From Text/CSV as separate queries from both machines, the paths in the generated code are identical.
Third reaction, if try fails, otherwise should catch error and continue but I'm finding data is only read in from the machine I'm at, IF the path to the needed CSV file is after try, not otherwise.

So far workaround has been two separate queries
Then a 3rd query that is like
Power Query:
let
    Source = try laptopcsv otherwise pccsv,


So far this set up is the only way I can easily open the Excel file and read in the data as needed.

Any ideas what I'm doing wrong or what's causing this behaviour and how to resolve?

TIA,
Jack
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
let
Source = try
Csv.Document(File.Contents("C:\Users\\OneDrive\Work\Data\Copper Futures Historical Data.csv"),[Delimiter=",", Columns=7, Encoding=65001, QuoteStyle=QuoteStyle.None])
otherwise
Csv.Document(File.Contents("C:\Users\JackDan[/COLOR]\OneDrive\Work\Data\Copper Futures Historical Data.csv"), [Delimiter=",", Columns=7, Encoding=65001, QuoteStyle=QuoteStyle.None]) ,

Unless you were intentionally masking the paths, neither of them will work but have no syntax problems!
 
Upvote 0
Hi there,
Thanks for replying, apologies delayed reply. Yes, I was attempting to mask paths(!) but the basic folder pattern is the same after the User name for both, e.g.

Power Query:
let
    Source = try
                Csv.Document(File.Contents("C:\Users\jackdanice\OneDrive\Work\Earthi\Data\Copper Futures Historical Data.csv"), [Delimiter=",", Columns=7, Encoding=65001, QuoteStyle=QuoteStyle.None])
            otherwise
                Csv.Document(File.Contents("C:\Users\Admin\OneDrive\Work\Earthi\Data\Copper Futures Historical Data.csv"),[Delimiter=",", Columns=7, Encoding=65001, QuoteStyle=QuoteStyle.None]),
The two paths are nearly identical, except the username inbetween "\Users\" and "\OneDrive" ( bit like =Substitute("C\Users\ABC\file.csv", "ABC", "DEF") )

If I run the code from PC, the csv data only imports if the PC path is before otherwise.
If try from laptop, the data only imports if the laptop path is before otherwise.

Limited data to test with but the experience so far is making me wonder if it's a security or some issue that when i test for a path it can't find depending on machine used, it isn't trying the otherwise part either

(Files are 122Kb and 91KB in size respectively, copied and pasted into the file, inserted a table, pulls into PQ fine - surprise! As tempfix, it'll do)
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,169
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top