DAX expression to filter dates verses Query Editor date filter

I originally filtered the dates in my report using the Query Editor, to only allow current month. If the current date is the first of the month, the table loads for the previous month.

FilterDates = Table.SelectRows( Source, each (if Date.Day(Today)=1 then Date.IsInPreviousMonth([Date]) else Date.IsInCurrentMonth([Date]) and [Date] < Today ))

Now they want to see history and I need to know what the equivalent of the above is using Advanced Filtering.

I want the first original page to just pull current dates due to measures using the current month and then provide a separate page to allow them to pull dates using a date slicer.

Hi @LASHBURN.

To help us further analyze your current state and visualize your issue, could you please provide as many as you can of:

  • Your work-in-progress PBIX file, using sanitized data if necessary
  • Your dataset as an Excel file (again, sanitized if necessary)
  • A detailed mock-up (marked-up screenshot or Excel file) of your desired outcome.

Also, if you provide DAX or Power Query code in your post, please format it using the built-in formatter.

Greg
_eDNA Forum - Format DAX or PQ

Thanks Greg, I will get that worked up and provide it.

1 Like

Hi Greg, I am going to just bring in another table because it needs more fields than the last. I’ll load all the records and filter on each page accordingly.

How do I get the URL of my demo .pbix to share it?

Hi @LASHBURN. No URL required … just use the “up-arrow” upload button to upload the PBIX file itself.
Greg
_eDNA Forum - Format DAX or PQ

DEMO.pbix (679.9 KB)

Hi @LASHBURN. Can you as well please upload a marked-up screenshot of the visual that’s in error or an Excel mock-up of your desired outcome?
Greg

Hi Greg,
The more I’ve looked in to it, it may be a calculated column that I need to use for my relative date filter.

I originally built the solution for the current month. In Power Query, I just loaded the table with current month.

= Date.From(DateTime.LocalNow())

= Table.SelectRows(#“Changed Type”, each
(if Date.Day(Today)=1 then Date.IsInPreviousMonth([Date]) else Date.IsInCurrentMonth([Date]) and [Date] < Today ))

Now I have a need to load the table with a wider date range and since I originally loaded the table for only current month, I need to be able to emulate the Power Query above in a DAX calculation.

Current Month Filter = IF(DAY(1),PREVIOUSMONTH(‘All Billing’[Bill Date]),STARTOFMONTH(‘All Billing’[Bill Date].[Date]))

DEMO.pbix (907.0 KB)

Here is another try at it, still not working, meaning the filtered dates should only show this month

Current Month Filter 2 = IF(DAY(1),PREVIOUSMONTH('All Billing'[Bill Date]), DATESBETWEEN('All Billing'[Bill Date],DATE(YEAR(TODAY()), MONTH(TODAY()), 1),TODAY()))

DEMO.pbix (907.3 KB)

Hi @LASHBURN,

Instead of Table.SelectRows use Table.AddColumns to create that filter column in your Date table query. Like so:

let fnDateTable = (StartDate as date, EndDate as date, FYStartMonth as number) as table =>
  let
    Today = Date.From( DateTime.FixedLocalNow()),
    DayCount = Duration.Days(Duration.From(EndDate - StartDate))+1,
    Source = List.Dates(StartDate,DayCount,#duration(1,0,0,0)),
    TableFromList = Table.FromList(Source, Splitter.SplitByNothing()),   
    ChangedType = Table.TransformColumnTypes(TableFromList,{{"Column1", type date}}),
    RenamedColumns = Table.RenameColumns(ChangedType,{{"Column1", "Date"}}),
    InsertYear = Table.AddColumn(RenamedColumns, "Year", each Date.Year([Date]),type text),
    InsertYearNumber = Table.AddColumn(RenamedColumns, "YearNumber", each Date.Year([Date])),
    InsertQuarter = Table.AddColumn(InsertYear, "QuarterOfYear", each Date.QuarterOfYear([Date])),
    InsertMonth = Table.AddColumn(InsertQuarter, "MonthOfYear", each Date.Month([Date]), type text),
    InsertDay = Table.AddColumn(InsertMonth, "DayOfMonth", each Date.Day([Date])),
    InsertDayInt = Table.AddColumn(InsertDay, "DateInt", each [Year] * 10000 + [MonthOfYear] * 100 + [DayOfMonth]),
    InsertMonthName = Table.AddColumn(InsertDayInt, "MonthName", each Date.ToText([Date], "MMMM"), type text),
    InsertCalendarMonth = Table.AddColumn(InsertMonthName, "MonthInCalendar", each (try(Text.Range([MonthName],0,3)) otherwise [MonthName]) & " " & Number.ToText([Year])),
    InsertCalendarQtr = Table.AddColumn(InsertCalendarMonth, "QuarterInCalendar", each "Q" & Number.ToText([QuarterOfYear]) & " " & Number.ToText([Year])),
    InsertDayWeek = Table.AddColumn(InsertCalendarQtr, "DayInWeek", each Date.DayOfWeek([Date])),
    InsertDayName = Table.AddColumn(InsertDayWeek, "DayOfWeekName", each Date.ToText([Date], "dddd"), type text),
    InsertWeekEnding = Table.AddColumn(InsertDayName, "WeekEnding", each Date.EndOfWeek([Date]), type date),
    InsertWeekNumber= Table.AddColumn(InsertWeekEnding, "Week Number", each Date.WeekOfYear([Date])),
    InsertMonthnYear = Table.AddColumn(InsertWeekNumber,"MonthnYear", each [Year] * 10000 + [MonthOfYear] * 100),
    InsertQuarternYear = Table.AddColumn(InsertMonthnYear,"QuarternYear", each [Year] * 10000 + [QuarterOfYear] * 100),
    ChangedType1 = Table.TransformColumnTypes(InsertQuarternYear,{{"QuarternYear", Int64.Type},{"Week Number", Int64.Type},{"Year", type text},{"MonthnYear", Int64.Type}, {"DateInt", Int64.Type}, {"DayOfMonth", Int64.Type}, {"MonthOfYear", Int64.Type}, {"QuarterOfYear", Int64.Type}, {"MonthInCalendar", type text}, {"QuarterInCalendar", type text}, {"DayInWeek", Int64.Type}}),
    InsertShortYear = Table.AddColumn(ChangedType1, "ShortYear", each Text.End(Text.From([Year]), 2), type text),
    AddFY = Table.AddColumn(InsertShortYear, "FY", each "FY"&(if [MonthOfYear]>=FYStartMonth then Text.From(Number.From([ShortYear])+1) else [ShortYear])),
    AddCurrentDates = Table.AddColumn( AddFY, "Current Month Filter",  each (if Date.Day(Today)=1 then Date.IsInPreviousMonth([Date]) else Date.IsInCurrentMonth([Date]) and [Date] < Today ), type logical)
in
    AddCurrentDates
in
    fnDateTable 

.
Now you can dragg the Current Month Filter into the page level filter section of the filter pane.
I hope this is helpful.

This worked perfectly, thank you so much, Melissa!

Hi Melissa. My current month filter worked in May but now that we are in a new month, I am seeing False for June dates. I used the solution you provided on 4/27/2021. Any suggestions of what might be wonky?

When was the Date table in the model refreshed?

yes.
AddCurrentDates = Table.AddColumn( AddFY, "Current Month Filter", each (if Date.Day(Today)=1 then Date.IsInPreviousMonth([Date]) else Date.IsInCurrentMonth([Date]) and [Date] < Today ), type logical)

What is the value for the “Today” variable?

Not seeing Today in the date table

All I see in the Date Query for Today is
Today = Date.From( DateTime.FixedLocalNow())

Okay, I just run the query and that seems to be working as expected.

This row returns TRUE and that is correct according to the defined logic.
So the only issue could be that the report was refreshed in the Power BI Service before midnight (UTC).

That is so strange. When I refresh in Desktop, it is False for Current Month. Yesterday it picked up previous month dates fine.