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

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
JPBTech
Regular Visitor

Slight Modification to the Previous Day, Month, and Year Calculations

The source is Direct Query SQL Server

I am trying to determine the previous day (month, year) count, but only to the current point in time.

End goal: IsToday = 1, IsPreviousDay = 1, IsCurrentMonth = 1, IsLastMonth = 1
...but only up to this point in time (NOW()).

Example
Previous Day Total is 75
Previous Day Total up to NOW() is33

pDtH stands for "Previous Day to Hour

 

The code snippet below works, but it returns the entire hour. I need it to stop at the current minute...I hope that makes sense.

 

pDtH =
IF.EAGER (
    YEAR ( CFS[Date CFS Received] ) = YEAR ( TODAY () )
        && MONTH ( CFS[Date CFS Received] ) = MONTH ( TODAY () )
        && DAY ( CFS[Date CFS Received] )
            <= DAY ( TODAY () ) - 1
        && HOUR ( CFS[Time CFS Received] ) <= HOUR ( NOW () ),
    1,
    0
)


The code below is where I am needing assistance.

pDtH =
IF.EAGER (
    YEAR ( CFS[Date CFS Received] ) = YEAR ( TODAY () )
        && MONTH ( CFS[Date CFS Received] ) = MONTH ( TODAY () )
        && DAY ( CFS[Date CFS Received] )
            <= DAY ( TODAY () ) - 1
        && HOUR ( CFS[Time CFS Received] ) <= HOUR ( NOW () )

        && MINUTE ( CFS[Time CFS Received] ) <= MINUTE( ( NOW() )),    <--- Causes issues

    1,
    0
)

Maybe I have been looking at this too long to see the obvious.  🙂

1 ACCEPTED SOLUTION

For what it might be worth, here is a working version done in Microsft SQL:

FROM [database].[dbo].[table] t
WHERE YEAR([t].[DateTimeReceived]) = YEAR(GETDATE())

AND DATEPART(MONTH, [t].[DateTimeReceived]) = DATEPART(MONTH, GETDATE()) --Gets the Current Month
AND DATEPART(DAY, [t].[DateTimeReceived]) = DATEPART(DAY, GETDATE()) -1 --Gets the Previous Day
AND CONVERT(VARCHAR, [t].[DateTimeReceived], 114) <= CONVERT(VARCHAR, GETDATE(), 114) --Gets the Current Time Stamp

View solution in original post

3 REPLIES 3
Greg_Deckler
Super User
Super User

@JPBTech  Can you just do this?

pDtH = IF(CFS[Date CFS Received] <= NOW() - 1,1,0)

@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

For what it might be worth, here is a working version done in Microsft SQL:

FROM [database].[dbo].[table] t
WHERE YEAR([t].[DateTimeReceived]) = YEAR(GETDATE())

AND DATEPART(MONTH, [t].[DateTimeReceived]) = DATEPART(MONTH, GETDATE()) --Gets the Current Month
AND DATEPART(DAY, [t].[DateTimeReceived]) = DATEPART(DAY, GETDATE()) -1 --Gets the Previous Day
AND CONVERT(VARCHAR, [t].[DateTimeReceived], 114) <= CONVERT(VARCHAR, GETDATE(), 114) --Gets the Current Time Stamp

HI, @Greg_Deckler, thank you for taking the time to reply.

Implementing that piece of code returns the value of 1 for all dates "below" now.

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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