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
Bridge2022
Regular Visitor

How to calculate the sum in a specific hour span across two days

Hi all, I encounter a question about calculating the sum in a special time span. 

Here is a part of the table.

How to filter the time from yesterday 23:00 to the next day 6:00?

The DATESBETWEEN function returns a table of dates, but not the hours.

 

Bridge2022_1-1652177041971.png

 

 

 

1 ACCEPTED SOLUTION
amitchandak
Super User
Super User

@Bridge2022 , Assume you have selected date of date in context

 

Measure =

var _sel = maxx(allselected('Date'), Date[Date])

var _min=_sel-1 + time(23,0,0)

var _max_sel  + time(6,0,0)

 

return 

calculate(sum(Table[Value]), filter(Table, Table[Date] >= _min &&Table[Date]<=_max))

 

 

or create a date only column in your table and use that

new column

Date1 = datevalues([Date])

 

Measure =

var _sel = maxx(allselected('Table'), Table[Date1])

var _min=_sel-1 + time(23,0,0)

var _max_sel  + time(6,0,0)

 

return 

calculate(sum(Table[Value]), filter(Table, Table[Date] >= _min &&Table[Date]<=_max))

 

Join us as experts from around the world come together to shape the future of data and AI!
At the Microsoft Analytics Community Conference, global leaders and influential voices are stepping up to share their knowledge and help you master the latest in Microsoft Fabric, Copilot, and Purview.
️ November 12th-14th, 2024
 Online Event
Register Here

View solution in original post

3 REPLIES 3
Bridge2022
Regular Visitor

Thanks for your help. 😀
I will try it tommorrow.

Greg_Deckler
Super User
Super User

@Bridge2022 Try:

Measure =
  VAR __Today = TODAY()
  VAR __Yesterday = __Today - 1
  VAR __Start = __Yesterday + TIME(23,0,0)
  VAR __End = __Start + 7/24
RETURN
  SUMX(FILTER('Table',[Date]>=__Start && [Date]<=__End),[Sales])


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

@Bridge2022 , Assume you have selected date of date in context

 

Measure =

var _sel = maxx(allselected('Date'), Date[Date])

var _min=_sel-1 + time(23,0,0)

var _max_sel  + time(6,0,0)

 

return 

calculate(sum(Table[Value]), filter(Table, Table[Date] >= _min &&Table[Date]<=_max))

 

 

or create a date only column in your table and use that

new column

Date1 = datevalues([Date])

 

Measure =

var _sel = maxx(allselected('Table'), Table[Date1])

var _min=_sel-1 + time(23,0,0)

var _max_sel  + time(6,0,0)

 

return 

calculate(sum(Table[Value]), filter(Table, Table[Date] >= _min &&Table[Date]<=_max))

 

Join us as experts from around the world come together to shape the future of data and AI!
At the Microsoft Analytics Community Conference, global leaders and influential voices are stepping up to share their knowledge and help you master the latest in Microsoft Fabric, Copilot, and Purview.
️ November 12th-14th, 2024
 Online Event
Register Here

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.