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

The Power BI DataViz World Championships are on! With four chances to enter, you could win a spot in the LIVE Grand Finale in Las Vegas. Show off your skills.

Reply
cheid_4838
Helper III
Helper III

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
v-shex-msft
Community Support
Community Support

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

Community Support Team _ Xiaoxin
If this post helps, please consider accept as solution to help other members find it more quickly.

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

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

Community Support Team _ Xiaoxin
If this post helps, please consider accept as solution to help other members find it more quickly.

Helpful resources

Announcements
Feb2025 Sticker Challenge

Join our Community Sticker Challenge 2025

If you love stickers, then you will definitely want to check out our Community Sticker Challenge!

Jan NL Carousel

Fabric Community Update - January 2025

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