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

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply
Anonymous
Not applicable

Issue with Filtering Data using IF ELSE Condition In Power Query

Hi Mates,

 

I am facing issue while filtering the data based on specific If Else Condition. So generally I have a data that I want to filter based on Current Day Name. If Current Day is 'Monday' then I want to load last 3 days data based on my Date Column, and if Current Day is in 'Tuesday To Friday' then I want to load only prior day's data. I have a code that is skipping then condition and only loading Prior Day data even if the condition is true. To test the code I have changed the Current Date to check as 'Thursday'. Please help me if I am doing something wrong in code. Thank in advance.

 

Below is the M Query Code and the Red colored code is the culprit one,

 

let
Source = Sql.Database("VMCEDW01", "DATAMART_OPERATIONS"),
FieldSales_vw_CountSheetHistory = Source{[Schema="FieldSales",Item="vw_CountSheetHistory"]}[Data],
#"Changed Type" = Table.TransformColumnTypes(FieldSales_vw_CountSheetHistory,{{"Created On", type date},{"Current Date", type date}}),

#"Filtered Rows" = if {{"Current Date", each Date.DayOfWeekName(_), type text}} = "Thursday"
then Table.SelectRows(#"Changed Type", each Date.IsInPreviousNDays([Created On], 3))
else Table.SelectRows(#"Changed Type", each Date.IsInPreviousNDays([Created On], 1)),

#"Grouped Rows" = Table.Group(#"Filtered Rows", {"Location"}, {{"PD Count", each Table.RowCount(_), Int64.Type}}),
#"Renamed Columns" = Table.RenameColumns(#"Grouped Rows",{{"Location", "PD Stores"}})
in
#"Renamed Columns"

1 ACCEPTED SOLUTION
AlexisOlson
Super User
Super User

Try this for your red part instead:

CurrentDate = Date.From(DateTime.LocalNow()),
DaysToLoad = if Date.DayOfWeekName(CurrentDate) = "Monday" then 3 else 1,
#"Filtered Rows" = Table.SelectRows(#"Changed Type", each Date.IsInPreviousNDays([Created On], DaysToLoad)),

View solution in original post

2 REPLIES 2
AlexisOlson
Super User
Super User

Try this for your red part instead:

CurrentDate = Date.From(DateTime.LocalNow()),
DaysToLoad = if Date.DayOfWeekName(CurrentDate) = "Monday" then 3 else 1,
#"Filtered Rows" = Table.SelectRows(#"Changed Type", each Date.IsInPreviousNDays([Created On], DaysToLoad)),
Anonymous
Not applicable

Thank you @AlexisOlson , It works perfectly as I am expecting..!!

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

Dec Fabric Community Survey

We want your feedback!

Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.

Top Solution Authors