Forum Discussion
Switch datasource using IF statement in Power Query
- 2 years ago
I finally solved it by not referencing the existing sources, instead I put it all in the same place:
let // Get data from API SourceAPI = Xml.Tables(Web.Contents(GatewayURL)), // Get data from file SourceFile = Xml.Tables(File.Contents(SavedScheduleXML)), // Select the sources based on the parameter value Source = if DataSource="API Broadcast" then SourceAPI else SourceFile, .......
Thanks! I had tried this already, referencing the datasource worked for both the API and XML. Here's an example:
let
Source1 = #"_Get XML from API Broadcast",
Source2 = #"_Get XML from File",
// This one works
// Source = Source1
// This one does not work
Source = if (1=1) then Source1 else Source2
in
Source
With the inverted condition and swapped Source 1/2 it doesn't work either. Very strange.
That is indeed very strange and does not conform with my understanding of the evaluation model.
Maybe isolating Source1 and Source2 declaration from the if statement by putting the if into another let/in environment could help:
let
Source1 = #"_Get XML from API Broadcast",
Source2 = #"_Get XML from File",
getData = (choice as text) =>
let
source = if choice = "api" then Source1 else if choice = "file" then Source2 else null
in
source
Source = getData("api")
- AlexHeinze2 years agoFrequent Visitor
Thanks for looking into this, WanderingBI . I had to tweak the code slightly...
let Source1 = #"_Get XML from API Broadcast", Source2 = #"_Get XML from File", getData = (choice as text) => let funcsource = if choice = "api" then Source1 else if choice = "file" then Source2 else null in funcsource, Source = getData ("api") in Source...unfortunately the result for "api" is still broken (while it still works for "file":
- WanderingBI2 years ago
Resolver III
Wish I could do more than guessing.
When assuming that the issue is only that the column "Table" is of data type any instead of table and this messes with the software receiving the query output (Power BI), would maybe setting the column type to table manually do any good?
let Source1 = #"_Get XML from API Broadcast", Source2 = #"_Get XML from File", getData = (choice as text) => let funcsource = if choice = "api" then Source1 else if choice = "file" then Source2 else null in funcsource, Source = getData ("api"), SourceAdjustColumnType = Value.ReplaceType(Source, type table [Table = Table.Type]) in SourceAdjustColumnType- AlexHeinze2 years agoFrequent Visitor
I finally solved it by not referencing the existing sources, instead I put it all in the same place:
let // Get data from API SourceAPI = Xml.Tables(Web.Contents(GatewayURL)), // Get data from file SourceFile = Xml.Tables(File.Contents(SavedScheduleXML)), // Select the sources based on the parameter value Source = if DataSource="API Broadcast" then SourceAPI else SourceFile, .......