cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
jader3rd Regular Visitor
Regular Visitor

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

Accepted Solutions
Highlighted
Moderator v-yuezhe-msft
Moderator

Re: Applying filtered rows to the source

@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
jader3rd Regular Visitor
Regular Visitor

Re: Applying filtered rows to the source

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
Moderator v-yuezhe-msft
Moderator

Re: Applying filtered rows to the source

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.
jader3rd Regular Visitor
Regular Visitor

Re: Applying filtered rows to the source

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

Highlighted
Moderator v-yuezhe-msft
Moderator

Re: Applying filtered rows to the source

@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

TDwivedi Frequent Visitor
Frequent Visitor

Re: Applying filtered rows to the source

@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

 

Helpful resources

Announcements
Ask Amanda Anything Q&A

Ask Amanda Anything Q&A

Learn the answers to some of the questions asked during the Amanda Triple A event.

October 2019 Community Highlights

October 2019 Community Highlights

October was a busy month in the community. Read the recap article to learn about some of the events and content.

New Solution Badges

New Solution Badges

Two waves of brand new solution badges are coming! Read the article for more information on our new community badges.

Analytics in Azure virtual event

Analytics in Azure virtual event

Experience a limitless analytics service built to ingest, prep, manage, and serve data for immediate use in Power BI.

Users Online
Currently online: 125 members 1,513 guests
Please welcome our newest community members: