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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
jader3rd
Employee
Employee

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?

1 ACCEPTED SOLUTION

@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

Community Support Team _ Lydia Zhang
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

5 REPLIES 5
v-yuezhe-msft
Employee
Employee

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.
1.PNG


Regards,
Lydia Zhang

Community Support Team _ Lydia Zhang
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

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

@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

Community Support Team _ Lydia Zhang
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

@v-yuezhe-msft 

Hi, 

In my case I need to get first parameter from Database itself which I can't have as inner query due to some Cosmos limitation. 

SQL 1 - First query is to get the max of column value  - select max(column) from table

SQL 2 - select * from table where id = Value received from first query

Thank you in advance

 

jader3rd
Employee
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

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.