Forum Discussion

ebrodie's avatar
ebrodie
Frequent Visitor
3 years ago
Solved

date filtering usint 2 dates in the table

I am new to PowerBI and loving it so far

 

I have a simple task list with start and close dates – at least it seems simple.  I would usually use multiple OR conditions in an MS Access query

 

I want to filter the records to show all tasks that are open within a date range

 

if I want to see tasks between 9/1/22 and 9/30/22, I want to see all tasks where:

  • Opened anytime and closed within the date range
  • Opened before 9/30/22 and still open
  • Opened before 9/30/22 and closed after 9/30/22
Task start dateclose dateDISPLAY
17/1/229/15/22YES
28/1/22 YES
38/1/2210/15/22YES
48/1/228/25/22NO
510/15/22 NO

 

  • jbwtp's avatar
    jbwtp
    3 years ago

    This returns a table which you can feed to measure, something like:

    Measure = SUMX(Filter('ve2doTaskALL', ([ClosedDate] > Date(2022,9,1) && [closeddate] < Date(2022,9,30)) || ([startdate] < Date(2022,9,30) && (ISBLANK([closeddate]) || [ClosedDate] > Date(2022,9,30)))), [Revenue])

     

    Cheers,

    John

     

7 Replies

  • jbwtp's avatar
    jbwtp
    Memorable Member

    Hi ebrodie,

     

    In Power Query you can use OR/AND in PBI in the same fashion as you would do in MS Access:

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUTLXN9Q3MgIyLPUNTSGsSNdgpVidaCUQ2wImrYAkYYwsYWiAodEEWd5C3wgq7ecPljVF1aQAlYkFAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Task " = _t, #"start date" = _t, #"close date" = _t, DISPLAY = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Task ", Int64.Type}, {"start date", type text}, {"close date", type text}, {"DISPLAY", type text}}),
        Main = Table.TransformColumns(#"Changed Type",{{"start date", each Date.From(DateTimeZone.From(_, "en-US")), type date}, {"close date", each Date.From(DateTimeZone.From(_, "en-US")), type date}}),
        OpenedBeforeClosedAfter = Table.SelectRows(Main, each [start date] < #date(2022, 9, 30) and [close date] > #date(2022, 9, 30)),
        OpenedBeforeNotClosed = Table.SelectRows(Main, each [start date] < #date(2022, 9, 30) and [close date] = null),
        ClosedWithinRage = Table.SelectRows(Main, each [close date] >= #date(2022, 9, 1) and [close date] <= #date(2022, 9, 30)),
        CombinedAndOptimised = Table.SelectRows(Main, each ([start date] >= #date(2022, 9, 1) and [close date] <= #date(2022, 9, 30)) or ([start date] < #date(2022, 9, 30) and ([close date] = null or [close date] > #date(2022, 9, 30))))
    in
        CombinedAndOptimised

     

    In DAX you replace it with II (for OR) and && (for AND):

    Filter('Table', ([close date] > Date(2022,9,1) && [close date] < Date(2022,9,30)) || ([start date] < Date(2022,9,30) && (ISBLANK([close date])  || [close date] > Date(2022,9,30))))

     

    Cheers,
    John

     

     

    • ebrodie's avatar
      ebrodie
      Frequent Visitor

      i think i'm more of a rookie than i thought...

       

      do i add this as a measure?

      Measure = Filter('ve2doTaskALL', ([ClosedDate] > Date(2022,9,1) && [closeddate] < Date(2022,9,30)) || ([startdate] < Date(2022,9,30) && (ISBLANK([closeddate]) || [ClosedDate] > Date(2022,9,30))))
       
      i get the error "The expression refers to multiple columns. Multiple columns cannot be converted to a scalar value"
       
      thanks in advance
      • jbwtp's avatar
        jbwtp
        Memorable Member

        This returns a table which you can feed to measure, something like:

        Measure = SUMX(Filter('ve2doTaskALL', ([ClosedDate] > Date(2022,9,1) && [closeddate] < Date(2022,9,30)) || ([startdate] < Date(2022,9,30) && (ISBLANK([closeddate]) || [ClosedDate] > Date(2022,9,30)))), [Revenue])

         

        Cheers,

        John