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

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
cheid_4838
Helper IV
Helper IV

Filter out data after specific period of time

I have a sql query where I am wanting to filter out "Do Not Invoice" order status seven days after the order completion date and leave all other statuses unchanged. I am fairly new to writing SQL and DAX queries. I have tried a couple of different ways to write the logic, but not having any luck.

 

 

Available 

Do Not Invoice 

Pending

Prepared

 

I have tried to incorporate the below logic into a CASE statement which I think is how it should be done, but not having any luck.

when DATEDIFF(DAY,[Completion Date], GETDATE() <= 7

 

Is there anyway to do this in SQL or in DAX?  Thanks.

4 REPLIES 4
Anonymous
Not applicable

Hi @cheid_4838,

Can you please share some dummy data that keep the raw data structure with expected results? It should help us clarify your scenario and test to coding formula.

How to Get Your Question Answered Quickly  

Regards,

Xiaoxin Sheng

I tried this logic, but keep getting NULL in my results.

 

CASE
WHEN [Invoice Status] IN ('CAN') THEN 
CASE WHEN DATEDIFF(DAY, [Completion Date], CAST(GETDATE() AS DATE)) <= 7 THEN 'CAN' ELSE [Invoice Status] END 
END as [InvoiceStatus]
,CASE
WHEN [Order Status] IN ('XIN') THEN
CASE WHEN DATEDIFF(DAY, [Completion Date], CAST(GETDATE() AS DATE)) <= 7 THEN 'XIN' ELSE [Order Status] END
END AS [OrderStatus]

Here is some sample data.   What I want to see is all the "Do Not Invoice" status (bold) removed if the number of days is more than seven days from the current date.  I also want all other statuses to remain unchanged and included in the report.  Does this help.

 

Order StatusCompletion Date Days from Today's Date (6/25/24) 
Available 1/24/2024153
Do Not Invoice 1/4/2024173
Pending2/5/2024141
Prepared3/21/202496
Available 6/23/20242
Do Not Invoice 6/20/20245
Pending6/17/20248
Prepared6/1/202424
Available 4/24/202462
Do Not Invoice 4/1/202485
Pending3/25/202492
Prepared6/24/20241
Anonymous
Not applicable

HI @cheid_4838,

Here is the measure formula to check if the records is 'Do Not Invoice' status and over 7 days, you can use this on the 'visual level filter' to filter records:

Expired flag = 
VAR rowDate =
    MAX ( T1[Completion Date] )
VAR sysDate = TODAY ()
VAR _currStatus =
    SELECTEDVALUE ( T1[Order Status] )
RETURN
    IF (
        _currStatus = "Do Not Invoice"
            && DATEDIFF ( rowDate, sysDate, DAY ) >= 7,
        "Y",
        "N"
    )

1.png

Regards,

Xiaoxin Sheng

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

Check out the June 2025 Power BI update to learn about new features.

June 2025 community update carousel

Fabric Community Update - June 2025

Find out what's new and trending in the Fabric community.