Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
JimJaggers
Frequent Visitor

Conditionally creating queries

Is it possible to create a set of queries from a universe of possible queries based on what is available in a data source?

 

That is, say I have a spreadsheet that might contain tabs labled A, B, C, & D.  I want to create a seperate query for each tab that is present, but not create the query if the tab is not present.

 

First time poster, so sorry if my question isn't very clear.  Please feel free to ask me questions.

1 ACCEPTED SOLUTION
BA_Pete
Super User
Super User

Hi @JimJaggers ,

 

You can't control the actual creation of queries per se, but you can have all the queries already in place and conditionally populate them using error handling i.e. you would set up the query with the source as 'Tab C', but then put an evaluation into the query that says "if the Tab C source returns an error (i.e. tab not present) then just run as blank" or similar.

 

Generally accepted technique on Chris Webb's blog here, but make sure to read the comments as Ken Puls adds some crucial adjustments to the method:

https://blog.crossjoin.co.uk/2014/09/18/handling-data-source-errors-in-power-query/ 

 

Pete



Now accepting Kudos! If my post helped you, why not give it a thumbs-up?

Proud to be a Datanaut!




View solution in original post

4 REPLIES 4
JimJaggers
Frequent Visitor

I'd like to add one change to the most excellent solution that @BA_Pete pointed me towards.

 

In the article by Chris Webb that Pete linked to the problem being addressed was a missing file.  When the file is missing the query will error out (at least I assume so, I'm still fairly new to PBI) and the final test to decide whether to output the intened table or an alternate version of it is:

 

Output = if TestForError[HasError] then AlternativeOutput else #"Changed Type"

 

However, in my particular problem, the Excel file will always be there, but some worksheets may not be.  So the above test does not work as the query does not error out in the absence of a the expected worksheet, it just generates an empty table.

 

I found this, very similar test works for my case:

 

Output = if Table.IsEmpty(#"Changed Type") then AlternativeOutput else #"Changed Type"

 

Merely changing the test condition from testing for an error to testing for an empty table.

 

I'm not sure yet how an empty table will affect my model.  I'll test that soon and post an update for any that are interested.

Just in case anyone is following this thread, and interested.  So far using an empty table, as opposed to an alternative table with a single dummy record, seems to work fine.  The model relationship to other tables still seems to hold and any measures against the now empty table just return nothing.

 

I'm still in the early stages of exploring the application I have in mind (basically a generic reporting model for data from an application where different users of the application will be interested in different output).  And this exercise was definitely useful for what I learned from it.  Thank you guys for your generous help.

 

Indeed, this was the "crucial adjustment" from Ken Puls that I mentioned.

Glad it's worked for you in the end.

 

Pete



Now accepting Kudos! If my post helped you, why not give it a thumbs-up?

Proud to be a Datanaut!




BA_Pete
Super User
Super User

Hi @JimJaggers ,

 

You can't control the actual creation of queries per se, but you can have all the queries already in place and conditionally populate them using error handling i.e. you would set up the query with the source as 'Tab C', but then put an evaluation into the query that says "if the Tab C source returns an error (i.e. tab not present) then just run as blank" or similar.

 

Generally accepted technique on Chris Webb's blog here, but make sure to read the comments as Ken Puls adds some crucial adjustments to the method:

https://blog.crossjoin.co.uk/2014/09/18/handling-data-source-errors-in-power-query/ 

 

Pete



Now accepting Kudos! If my post helped you, why not give it a thumbs-up?

Proud to be a Datanaut!




Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Solution Authors