Forum Discussion

joshrumbawa's avatar
joshrumbawa
Frequent Visitor
2 years ago

Date and Time Relative Filter

Hello PBI Experts, need your help with regards to Date and Time Filter. I have this requirement to create a slicer (Last 5 minutes, Last 15 minutes, Last Hour, Today, Yesterday, Last Month, Last Year, etc.) based on the Date/Time Closed from Current Day/Time.

 

I have 3 fact tables that needs to be filter, They all have Date/Time column. I can achieve the Today, Yesterday, Weeks, Months, and Year using Date Dimension table. However, I have no idea how to integrate this with Time (Last 6 hours, Last 5 minutes, etc.)

 

Is someone here have the same scenario, or how do you work on this? Any inputs is highly appreciated. Thank you!

They require me to make a filter something like this.

 

6 Replies

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi joshrumbawa ,

     

    According to your description, here are my steps you can follow as a solution.

    (1) This is my test data. 

    (2) We can create a slicer table. 

    Slicer = DATATABLE ( 
        "Slicer", STRING, 
        {
            { "Last 5 minutes" },
            { "Last 6 hours" },
            { "Last 2 days" },
            { "Last Month" },
            { "Last Year" }
        }
    ) 
    

    (3) We can create a measure.

    Flag = 
     SWITCH(TRUE(),
     ISFILTERED('Slicer'[Slicer])=FALSE(),1,
     SELECTEDVALUE('Slicer'[Slicer])="Last 5 minutes" && MAX('FactTable1'[Date])<=NOW() && MAX('FactTable1'[Date])>=NOW()-TIME(0,5,0),1,
     SELECTEDVALUE('Slicer'[Slicer])="Last 6 hours" && MAX('FactTable1'[Date])<=NOW() && MAX('FactTable1'[Date])>=NOW()-TIME(6,0,0),1,
     SELECTEDVALUE('Slicer'[Slicer])="Last 2 days" && MAX('FactTable1'[Date])<=NOW() && MAX('FactTable1'[Date])>=TODAY()-1,1,
     SELECTEDVALUE('Slicer'[Slicer])="Last Month" && MAX('FactTable1'[Date])<=NOW() && MAX('FactTable1'[Date])>=EDATE(TODAY(),-1),1,
     SELECTEDVALUE('Slicer'[Slicer])="Last Year" && MAX('FactTable1'[Date])<=NOW() && MAX('FactTable1'[Date])>=DATE(YEAR(TODAY())-1,MONTH(TODAY()),DAY(TODAY())),1,0)

    (4) Place [Flag=1] on the visual object screening and then the result is as follows.

    Best Regards,

    Neeko Tang

    If this post  helps, then please consider Accept it as the solution  to help the other members find it more quickly. 

    Date and Time Relative Filter.pbix44 KB
    • joshrumbawa's avatar
      joshrumbawa
      Frequent Visitor

      Hi Anonymous , I appreciate your response and that might be the solution. 

      Currently, I have a calculated column that connected to my Dim Date to filter those Dates Period then I will use the Selection column in the slicer.



      I love your idea. However, I have multiple fact tables that needs to filter with one slicer from (This year, This month, etc. up to Last 5 mins). Do you know how do I do this? To be honest I got stuck on this issues and I'm having problems to find a solution. Appreciate your response.

       

      • Anonymous's avatar
        Anonymous
        Not applicable

        Hi joshrumbawa ,

         

        I'm sorry I can't understand your needs, please provide some sample data in your tables (exclude sensitive data) with Text format and your expected result with backend logic and special examples. It is better if you can share a simplified pbix file. Thank you.

         

        Best Regards,

        Neeko Tang

    • joshrumbawa's avatar
      joshrumbawa
      Frequent Visitor

      Hello Anonymous , me again. I tried to test your concept, and it's working fine for dates Last 2 days, This Month, This Year. However, when I tried to filter Last 6 hours and Last 5 minutes I'm not getting any good result. It seems the condition is not met for the calculation of date time though the values of that measure seems right. Any thoughts? Thank you!

       

  • Greg_Deckler's avatar
    Greg_Deckler
    Community Champion

    joshrumbawa So I think this generally falls into the camp of a Complex Selector. Your measure would look at the value in the slicer and then filter the rows according to the criteria. You may not use it as a filter in the Filters pane in your case but rather within the measure calculate the result you need. https://community.fabric.microsoft.com/t5/Quick-Measures-Gallery/The-Complex-Selector/m-p/1116633#M534

     

    This may also help as it shows how to use a disconnected table like your slicer in a single measure that does all the heavy lifting. Disconnected Table Trick as this article demonstrates: https://community.powerbi.com/t5/Community-Blog/Solving-Attendance-with-the-Disconnected-Table-Trick/ba-p/279563