Forum Discussion
Capitalgman987
Dec 08, 2024Copper Contributor
How to check if a list is blank and then take action based on the result.
I am a very new with Power Query and M. The following code works as long as the list (lstFund) has a value. If not, the query rightfully errors. How do I check if the list is empty (List.IsEmpty??...
- Dec 16, 2024
Thank you all for your replies. I couldn't get any variation of your suggestions to work. I finally got it to work by using if List.NonNullCount(list Name)<1 then...
Thanks again.
SergeiBaklan
Dec 11, 2024Diamond Contributor
You may rely on Understanding query evaluation and query folding in Power Query - Power Query | Microsoft Learn instead of using native query. Power Query generates SQL in background, you may use M-script only. In your case it could be
let
lstFund = {"a", "b"},
lstFY = {2024},
Source = Sql.Database("SQL5200", "Data"),
GetData = Source{[Schema="dbo",Item="JRNL_CA"]}[Data],
SelectColumns = Table.SelectColumns(
GetData,
{"REC_NO", "FUND_CD", "SFUND_CD", "UNIT_CD"}
),
FilterTable =
if List.Count(lstFund) > 0
then Table.SelectRows(
SelectColumns,
each List.Contains(lstFund, [FUND_CD])
and List.Contains(lstFY, [FY_DC])
)
else SelectColumns
in
FilterTable
It's easy to break query folding, you may check on each step from right click menu if native query was generated till this step or not.
For the above script generated sql (which works directly on server) will be like
select [_].[REC_NO],
[_].[FUND_CD],
[_].[SFUND_CD],
[_].[UNIT_CD]
from
(
select [REC_NO],
[FUND_CD],
[SFUND_CD],
[UNIT_CD]
from [dbo].[JRNL_CA] as [$Table]
) as [_]
where ([_].[FUND_CD] in ('a', 'b')) and ([_].[FY_DC] in (2024))
but in most cases you may not care which sql was generated.