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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
JohnUnfiro
New Member

Previous hour and previous day, DAX.

Hi, 

 

I'm trying to calculate the previous hour and previous day for a simple table in powerbi. For previous hour, I have the following calculation. I would like the count for IDs with status 3 for the previous hour. 

PreviousHourTest with status 3 =
CALCULATE (
    COUNT ( TestTable[Id] ),
    TestTable[Status] = 3,
    ( DATEDIFF ( TestTable[Created Time and Date], TODAY (), HOUR ) <= 1 )
)

 For previous day, I have the following, I would also like to filter this for status = 3:

Count Prev Day = 
CALCULATE (
    COUNT ( TestTable[Id] ),
    FILTER (
        ALL ( TestTable[Created Time and Date] ),
        'TestTable'[Created Time and Date] = MAX ( TestTable[Created Time and Date] )
    )
)

Unfortunately, the results I receive are not what I expect. 

My table is as follows: 

2022-01-13_21h18_42.png

Can anyone help me please? 

1 ACCEPTED SOLUTION
Greg_Deckler
Community Champion
Community Champion

@JohnUnfiro Try these:

Previous Hour =
  CALCULATE(
    COUNT( TestTable[Id]),
    ALL(TestTable),
    TestTable[Status] = 3,
    TestTable[Created Time and Date] >= TODAY()-1/24,
    TestTable[Created Time and Date] < TODAY()
  )

Previous Day
  CALCULATE(
    COUNT( TestTable[Id]),
    ALL(TestTable),
    TestTable[Status] = 3,
    TestTable[Created Time and Date] >= DATE(YEAR(TODAY()),MONTH(TODAY()),DAY(TODAY()))-1,
    TestTable[Created Time and Date] < DATE(YEAR(TODAY()),MONTH(TODAY()),DAY(TODAY()))
  )



Follow on LinkedIn
@ 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!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...

View solution in original post

5 REPLIES 5
Greg_Deckler
Community Champion
Community Champion

@JohnUnfiro Try these:

Previous Hour =
  CALCULATE(
    COUNT( TestTable[Id]),
    ALL(TestTable),
    TestTable[Status] = 3,
    TestTable[Created Time and Date] >= TODAY()-1/24,
    TestTable[Created Time and Date] < TODAY()
  )

Previous Day
  CALCULATE(
    COUNT( TestTable[Id]),
    ALL(TestTable),
    TestTable[Status] = 3,
    TestTable[Created Time and Date] >= DATE(YEAR(TODAY()),MONTH(TODAY()),DAY(TODAY()))-1,
    TestTable[Created Time and Date] < DATE(YEAR(TODAY()),MONTH(TODAY()),DAY(TODAY()))
  )



Follow on LinkedIn
@ 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!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...

Thanks @Greg_Deckler. Previous day looks good. 

I added a new line to the data but I get a blank value for previous hour. It's 21:53 here now. 

Does previous hour mean within the last 60 minutes or is it the time from 20:00 to 21:00?

 

2022-01-13_21h52_167.png

 

I would like the value between 20:00 and 21:00. Thanks again. 

@JohnUnfiro As coded it was in the last 60 minutes. Let me think, maybe:

Previous Hour =
VAR __Today = DATE(YEAR(NOW()),MONTH(NOW()),DAY(NOW()))
VAR __CurrentHour = HOUR(__Today)
VAR __1HourAgo = DATE(YEAR(NOW()),MONTH(NOW()),DAY(NOW()))-1/24
VAR __LastHour = HOUR(__1HourAgo)
VAR __Max = __Today + __CurrentHour/24
VAR __Min = __1HourAgo + __LastHour/24
RETURN
  CALCULATE(
    COUNT( TestTable[Id]),
    ALL(TestTable),
    TestTable[Status] = 3,
    TestTable[Created Time and Date] >= __Min,
    TestTable[Created Time and Date] <= __Max
  )


Follow on LinkedIn
@ 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!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...

VAR __1HourAgo looks off to me (always 11 PM?).

 

It seems to me like this should work (but I haven't tested it):

Previous Hour =
VAR __Max = NOW ()
VAR __Min = __Max - 1 / 24
RETURN
    CALCULATE (
        COUNT ( TestTable[Id] ),
        ALL ( TestTable ),
        TestTable[Status] = 3,
        TestTable[Created Time and Date] >= __Min,
        TestTable[Created Time and Date] <= __Max
    )

@AlexisOlson Yeah, it should have just been: 

VAR __1HourAgo = NOW()-1/24


Follow on LinkedIn
@ 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!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.