Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
6 years ago

multiple table reports from run one stored procedure

I posted original question in https://community.powerbi.com/t5/Desktop/SQL-Stored-Procedure-with-2-result-sets/td-p/1149559

 

But did not get any clear reply to it so I am following up now.

 

basically I need to run one stored procedure, and get 2 resultsets both of them in a separate table report on the page.

Ex: if I run one stored procedure in SQL Management Studio then I can get 2 resultsets (or even more). 

Is there any way to do this in Power Bi?

 

Thank you,

3 Replies

  • rishab's avatar
    rishab
    Regular Visitor

    Okay, here's how I solved it at my work (I needed to import data from 1 Stored Proc, which provided 2 outputs):

    • Considerations for the SQL side:
      1. Create a passable variable "@Worksheet" in your Stored Proc. I'm guessing that for you, the start of your stored proc will look something like this:
         

    ALTER PROCEDURE mystoredproc

    --@TodaysDate datetime, -- Add date or other parameters as needed

    @WorkSheet INT -- This parameter will help you "break" each output

     AS
     

    1. Break each output section/select statement as follows:
      IF @Worksheet = 1
      BEGIN
      SELECT * FROM yabbadabbadooDatabase
      END
      ELSE IF @Worksheet = 2
      BEGIN
      SELECT * FROM benandjerryDatabase
      END
    2. Here's how your exec statement will look like:
      • exec mystoredproc 1
      • exec mystoredproc 2
    • Considerations on the Power BI side (you probably know most of this stuff anyway, based on your comments above):
      1. Import SQL Server data
      2. Add Server and Database names as needed.
      3. Select "Import" under "Data Connectivity mode"
      4. Write the execute query for stored proc for only the 1st output:
        • exec mystoredproc 1
      5. Transform/load the data as needed.
      6. Repeat steps 1-5 for 2nd output:
        • exec mystoredproc 2

    This will create 2 datasets in your Power BI file, which can be updated by clicking the "Refresh" button.

     

    I hope this finally helps you 🙂

    I noticed that other answers weren't as useful here either, nor in your original post. Thankfully someone at work showed me it, and it works for me.

  • v-lili6-msft's avatar
    v-lili6-msft
    Icon for Community Support rankCommunity Support

    hi  Anonymous 

    Power BI will not ingest more than one table at once returned from a SP.

     

    Use the filter queries inside your SP to fetch data inside Power BI.

    Basically, you will have two separate queries for two tables.

     

    Regards,

    Lin

    • Anonymous's avatar
      Anonymous
      Not applicable

      Hi Lin,

      Can you please explain what you mean by: "Use the filter queries inside your SP to fetch data inside Power BI."

      Thank you