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

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply
Strongbuck
Helper I
Helper I

Averaging a Numeric Column and Filter the Data by Day, Hour and Minutes

I need to average a numeric column and filter the data by day, hour and minutes.  I'm trying to display a chart that shows the sound levels data for workday hours.  The workday hours are 6:00 AM to 8:00 PM.

 

I've been able to accomplish this by day and hour but I haven't been able to isolate only data that is between the work hours because it needs to include the minutes.

 

I've tried to add a filter to an average forumla to isolate the hours.  I was able to add a column with only the hours data as numeric.  This let me code an equation but the data returned included hours after 8:00 PM (i.e. 8:10 PM, 8:30 PM, etc.)

 

There has to be a better way of doing this.  I tried searching for a solution that included hours and minutes but none of them included it.  This is the Sound_Level_History_All table:

 

LocationDateTimeSound_Levels
Fitzrandolph1/4/2021 0:0040.3
Fitzrandolph1/4/2021 0:1040.1
Fitzrandolph1/4/2021 0:1540.1
Fitzrandolph1/5/2021 0:2040.4
Fitzrandolph1/5/2021 0:2540.5
Fitzrandolph1/5/2021 15:3040.7
Fitzrandolph1/6/2021 0:3541.1
Fitzrandolph1/6/2021 0:4040.9
Fitzrandolph1/6/2021 0:4540.8
Fitzrandolph1/7/2021 0:5040.8
Fitzrandolph1/7/2021 15:4040.9

 

This is the DAX for averaging by day.  I also tried to search for how to add the hour and minutes elements to the date hierachy so I could just replace the ".[Day] piece with it.

 

Avg Sound by Day =
AVERAGEX(
    KEEPFILTERS(VALUES('Sound_Level_History_All'[DateTime].[Day])),
    CALCULATE(AVERAGE('Sound_Level_History_All'[Sound_Levels]))
)

I've spent way to much time trying to figure this out myself.  Any help would be appreciated! 

1 ACCEPTED SOLUTION

@Strongbuck OK, should be this then:

Avg Sound by Day Measure = 
  VAR __Table = 
    FILTER(
      ADDCOLUMNS( 'Sound_Level_History_All', "__Hour", HOUR([DateTime]) ),
      [__Hour] >= 6 && ([DateTime] - INT([DateTime])) <= TIME(20, 0, 0)
    )
  VAR __Result = AVERAGEX(__Table, [Sound_Levels])
RETURN
  __Result

A datetime is a decimal value with the integer portion the data and the decimal portion the time. So subtracting the integer value from the datetime value leaves just the time which you can then compare to a time value created by the TIME function in which you specify a particular time (which in this case excludes minutes and seconds or sets them to zero anyway) which means that times like 16:10 (8:10 PM) are larger than 16:0:0 (8:00:00).



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!:
Power BI Cookbook Third Edition (Color)

DAX is easy, CALCULATE makes DAX hard...

View solution in original post

6 REPLIES 6
Greg_Deckler
Super User
Super User

@Strongbuck Maybe:

Avg Sound by Day Measure =
  VAR __Table = 
    FILTER(
      ADDCOLUMNS( 'Sound_Level_History_All', "__Hour", HOUR([DateTime]) ),
      [__Hour] >= 6 && [__Hour] <= 20
    )
  VAR __Result = AVERAGEX(__Table, [Sound Levels])
RETURN
  __Result


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!:
Power BI Cookbook Third Edition (Color)

DAX is easy, CALCULATE makes DAX hard...

@Greg_Deckler I tried something similar but the issues is that the data is not capped at 8:00 PM.  It includes 8:10 PM, 8:20 PM, etc.

 

[_Hour] >=6 && [_Hour] <= 20

 

Sorry for the confusion.  I updated my original post.  I need the filter to include the hour and  minutes. 

@Strongbuck So, [_Hour] >=6 && [_Hour] < 21 ?



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!:
Power BI Cookbook Third Edition (Color)

DAX is easy, CALCULATE makes DAX hard...

@Greg_Deckler One approach that might address the issue I am seeing, is rounding up the minutes.

 

My tests will be 8:00, 8:05, 8:15 and 8:30.  Only the record that has the 8:00 PM value should be included in the Average.  None of the others should be included.

 

Do you know the syntax on how to round the minutes up the the next hour if their value is anything but 00?

Hi @Strongbuck 

You can create a calculated column in table.

TimeValue = TIMEVALUE([DateTime])

Then create the measure.

Avg Sound by Day =
VAR _6 =
    TIMEVALUE ( "6:00" )
VAR _8 =
    TIME ( 20, 0, 0 )
RETURN
    CALCULATE (
        AVERAGE ( Sound_Level_History_All[Sound_Levels] ),
        FILTER ( Sound_Level_History_All, [TimeValue] >= _6 && [TimeValue] <= _8 )
    )

Output

vxinruzhumsft_0-1708326706898.png

 

Best Regards!

Yolo Zhu

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

 

@Strongbuck OK, should be this then:

Avg Sound by Day Measure = 
  VAR __Table = 
    FILTER(
      ADDCOLUMNS( 'Sound_Level_History_All', "__Hour", HOUR([DateTime]) ),
      [__Hour] >= 6 && ([DateTime] - INT([DateTime])) <= TIME(20, 0, 0)
    )
  VAR __Result = AVERAGEX(__Table, [Sound_Levels])
RETURN
  __Result

A datetime is a decimal value with the integer portion the data and the decimal portion the time. So subtracting the integer value from the datetime value leaves just the time which you can then compare to a time value created by the TIME function in which you specify a particular time (which in this case excludes minutes and seconds or sets them to zero anyway) which means that times like 16:10 (8:10 PM) are larger than 16:0:0 (8:00:00).



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!:
Power BI Cookbook Third Edition (Color)

DAX is easy, CALCULATE makes DAX hard...

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

Dec Fabric Community Survey

We want your feedback!

Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.