Forum Discussion

jader3rd's avatar
jader3rd
Icon for Microsoft Employee rankMicrosoft Employee
9 years ago
Solved

Applying filtered rows to the source

I'm connecting to a SQL view which has a lot of rows. Most of the time when we view the data we're going to be most interested in the last week or twos worth of data. I created StartTime and EndTime parameters and have them filter out rows as part of the query. I'm beginning to suspect that it's not working how I thought it would though. I suspect what's happening is that Power BI is querying the entire SQL view, grabbing the first 100,000 (or whatever) rows returned, and then applying the filter. The result being that there are days' worth of rows not showing up in the Power BI report, but I can see them when doing a query in SQL Server Management Studio.

So is there a way to push the Power BI parameters, into the Source of the Power BI query, (a simple WHERE clause), to filter out the rows from the SQL query?

  • Anonymous's avatar
    Anonymous
    9 years ago

    jader3rd,

    Change your codes to the following.

    let
        SQLSource = (StartTime as datetime, EndTime as datetime) => 
    let
        Source = Sql.Database("MF-APDM-Shared1AlwaysOnVIP.OBDStripeDedicated-Prod-Bn2.Bn2.ap.gbl,89", "MDP_EXOMI", [Query="SELECT CONVERT(DATE, [START_TIME]) AS Day, [ACTION_REASON], [CHECKFAILED], COUNT(*) AS Cnt FROM [dbo].[HAOpBcsFailed] WHERE [START_TIME] >= '"& DateTime.ToText(StartTime) & "' AND [START_TIME] <= '"& DateTime.ToText(EndTime)&"' GROUP BY CONVERT(DATE, [START_TIME]), [ACTION_REASON], [CHECKFAILED]"]) 
    in
        Source
    in 
        SQLSource


    Regards,
    Lydia

5 Replies

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi jader3rd,

    Open Power BI Desktop, and choose “Get Data->Blank query”, then click Advanced Editor, paste the following code in it.

    let
        SQLSource = (param1 as date, param2 as date) =>
    
    let
        Source = Sql.Database("servername", "databasename", [Query="select * from viewname where DateColumn>='"& Date.ToText(param1) & "' and DateColumn <='" & Date.ToText(param2)&"'"])
    
    in
        Source
    in
        SQLSource

     

    Then you can enter parameter values and click “Invoke” button to generate filtered table.



    Regards,
    Lydia Zhang

    • jader3rd's avatar
      jader3rd
      Icon for Microsoft Employee rankMicrosoft Employee

      I'm so excited that this could work.

      This is what I have.

      let
          SQLSource = (StartTime as datetime, EndTime as datetime) => 
      let
          Source = Sql.Database("MF-APDM-Shared1AlwaysOnVIP.OBDStripeDedicated-Prod-Bn2.Bn2.ap.gbl,89", "MDP_EXOMI", [Query="SELECT CONVERT(DATE, [START_TIME]) AS Day, [ACTION_REASON], [CHECKFAILED], COUNT(*) AS Cnt FROM [dbo].[HAOpBcsFailed] WHERE [START_TIME] >= '"& Date.ToText(StartTime) & "' AND [START_TIME] <= '"& Date.ToText(EndTime)&"' GROUP BY CONVERT(DATE, [START_TIME]), [ACTION_REASON], [CHECKFAILED]"]) 
      in
          Source
      in 
          SQLSource

       

      And here's the error

      An error occurred in the ‘HAOpBcsFailedFunc’ query. Expression.Error: We cannot convert the value #datetime(2017, 5, 1, 0, 0, 0) to type Date.
      Details:
          Value=5/1/2017 12:00:00 AM
          Type=Type

      • Anonymous's avatar
        Anonymous
        Not applicable

        jader3rd,

        Change your codes to the following.

        let
            SQLSource = (StartTime as datetime, EndTime as datetime) => 
        let
            Source = Sql.Database("MF-APDM-Shared1AlwaysOnVIP.OBDStripeDedicated-Prod-Bn2.Bn2.ap.gbl,89", "MDP_EXOMI", [Query="SELECT CONVERT(DATE, [START_TIME]) AS Day, [ACTION_REASON], [CHECKFAILED], COUNT(*) AS Cnt FROM [dbo].[HAOpBcsFailed] WHERE [START_TIME] >= '"& DateTime.ToText(StartTime) & "' AND [START_TIME] <= '"& DateTime.ToText(EndTime)&"' GROUP BY CONVERT(DATE, [START_TIME]), [ACTION_REASON], [CHECKFAILED]"]) 
        in
            Source
        in 
            SQLSource


        Regards,
        Lydia

  • jader3rd's avatar
    jader3rd
    Icon for Microsoft Employee rankMicrosoft Employee

    To show what I'm seeing, here are the numbers from Power BI desktop:

    StartDateCount of OP_ID
    5/1/2017 0:007599
    5/2/2017 0:006510
    5/3/2017 0:007687
    5/4/2017 0:0012801
    5/5/2017 0:0013200
    5/6/2017 0:0017838
    5/7/2017 0:0011555
    5/8/2017 0:007494
    5/9/2017 0:009311
    5/10/2017 0:0012743
    5/11/2017 0:0015495
    5/12/2017 0:0013319
    5/13/2017 0:0012553
    5/14/2017 0:007402

    And here are the numbers from a SQL query doing what I expect Power BI is doing. They are very different.

     

    StartDateC
    5/1/2017128627
    5/2/2017113405
    5/3/2017139923
    5/4/2017180977
    5/5/2017170624
    5/6/2017261793
    5/7/2017150938
    5/8/201761490
    5/9/2017144524
    5/10/2017134023
    5/11/2017173369
    5/12/2017174371
    5/13/2017231799
    5/14/2017174641
    5/15/2017112142
    5/16/2017167455