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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

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
Super User
Super User

@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()))
  )


@ 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...

View solution in original post

5 REPLIES 5
Greg_Deckler
Super User
Super User

@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()))
  )


@ 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...

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
  )

@ 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...

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

@ 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...

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.

Top Solution Authors