Calculate band on Hours/Time passed

farhan11941234

New Member
Joined
Dec 14, 2019
Messages
29
Office Version
  1. 365
Platform
  1. Windows
Please help me out to figure out a formula to calculate Date/Time Base Bins (See attached Image).Whenever The ID Creation(Column A) on Current Day and Time 3:00 PM to 6:00 PM, In Column C "> 3 to 6 PM" , Current Date and time between 6 to 7 PM in Column C result should be " > 6 to 7 PM", after 7:00 PM , in Column C should be "> 7:00 PM onwards.
when time > 14 Hours it should be >14 Hours and continue the this logic to >24 and >48 hours.
I would be very thankful to help me devolope the formula for this.

Sorry, for not using the XL2BB becuase of the admin Limitations.

Thanks
 

Attachments

  • Sample.jpg
    Sample.jpg
    98.4 KB · Views: 42

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
I would be really helpful if the solution formula based on Column B where Hours are becuase in column A Date time is a text field in UK time formate dd/mm/yyyy
and its difficult for me to change it back to US format which often result errors since start date from 1 to 11 becomes month rather than days.
 
Upvote 0
I hope you're willing to try Power Query. It is set up to handle complicated time calculations like this a LOT better than Excel!
Starting with this Table named Timesheet:
Book1
AB
1Time StampNow
212/14/22 12:12 AM12/15/22 3:03 PM
312/13/22 3:33 PM12/15/22 3:03 PM
412/14/22 7:56 AM12/15/22 3:03 PM
512/13/22 6:08 PM12/15/22 3:03 PM
612/13/22 6:59 PM12/15/22 3:03 PM
712/13/22 9:38 AM12/15/22 3:03 PM
812/13/22 1:48 AM12/15/22 3:03 PM
912/14/22 3:27 AM12/15/22 3:03 PM
1012/13/22 5:23 PM12/15/22 3:03 PM
1112/14/22 4:48 AM12/15/22 3:03 PM
1212/13/22 5:37 PM12/15/22 3:03 PM
1312/13/22 2:25 PM12/15/22 3:03 PM
1412/14/22 11:16 PM12/15/22 3:03 PM
1512/13/22 12:14 PM12/15/22 3:03 PM
1612/14/22 11:20 AM12/15/22 3:03 PM
1712/14/22 5:22 AM12/15/22 3:03 PM
1812/14/22 1:56 PM12/15/22 3:03 PM
1912/13/22 11:26 AM12/15/22 3:03 PM
2012/14/22 7:58 AM12/15/22 3:03 PM
2112/14/22 10:31 PM12/15/22 3:03 PM
Sheet1

Pulled the Table into Power Query,
Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Timesheet"]}[Content],
    ChangedType = Table.TransformColumnTypes(Source,{{"Time Stamp", type datetime}, {"Now", type datetime}}),
    InsertedTimeSubtraction = Table.AddColumn(ChangedType, "Total Hours", each [Now] - [Time Stamp], type duration),
    CalculatedTotalHours = Table.TransformColumns(InsertedTimeSubtraction,{{"Total Hours", Duration.TotalHours, type number}}),
    RoundedOff = Table.TransformColumns(CalculatedTotalHours,{{"Total Hours", each Number.Round(_, 1), type number}})
in
    RoundedOff
and just a few easy steps gets the answer, resulting in this:
Book1
DEF
1Time StampNowTotal Hours
212/14/2022 00:1212/15/2022 15:0338.8
312/13/2022 15:3312/15/2022 15:0347.5
412/14/2022 07:5612/15/2022 15:0331.1
512/13/2022 18:0812/15/2022 15:0344.9
612/13/2022 18:5912/15/2022 15:0344.1
712/13/2022 09:3812/15/2022 15:0353.4
812/13/2022 01:4812/15/2022 15:0361.2
912/14/2022 03:2712/15/2022 15:0335.6
1012/13/2022 17:2312/15/2022 15:0345.7
1112/14/2022 04:4812/15/2022 15:0334.2
1212/13/2022 17:3712/15/2022 15:0345.4
1312/13/2022 14:2512/15/2022 15:0348.6
1412/14/2022 23:1612/15/2022 15:0315.8
1512/13/2022 12:1412/15/2022 15:0350.8
1612/14/2022 11:2012/15/2022 15:0327.7
1712/14/2022 05:2212/15/2022 15:0333.7
1812/14/2022 13:5612/15/2022 15:0325.1
1912/13/2022 11:2612/15/2022 15:0351.6
2012/14/2022 07:5812/15/2022 15:0331.1
2112/14/2022 22:3112/15/2022 15:0316.5
Sheet1

I obviously didn't use your sample. I generated the Time Stamp column like this:
Book1
L
1Timestamp
212/14/22 2:59 PM
Sheet1
Cell Formulas
RangeFormula
L2:L21L2=RANDARRAY(20,,DATEVALUE("12/13/2022"),DATEVALUE("12/14/2022"),TRUE)+RANDARRAY(20,,0,1,FALSE)
Dynamic array formulas.

Once I had the Date and Time, I Copy/Paste Valued the column, added NOW() to generate the stop time (not in sample) and let Power Query do the rest.
To do this with Formulas is much more difficult. Give it a try!
 
Upvote 0
that was helpful but it did not solve my problem, becuause the Hours calculation was including when I get the Data from database.What I need is to create
a Column next to hours column Name Hours Bands.
The formula I used for calculation is pasted below , Now I like to Have if the ID Creation Date is current Date and ID Creation time is between 3:00PM to 06:00PM
In Hours Band Column Should be "> 3 to 6PM" and if creation time between 6 to 7 then "6 to 7 PM" and next is when ID creation time > 7 then ">7 PM Onward". This Band for current date only as when the hours exceeds > 14 then " > 14 Hours" , ">24 Hours", "> 48 Hours".
I hope that you understand what I am trying to acheive.


Hours in Column "B"
=IF(B2>48,">48 hours", IF(B2>18, ">18 hours, <48 hours", "<18 hours"))
 
Upvote 0
Sorry I didn't understand that. What you need is an APROXIMATE Lookup. This may not be exactly what you need, but should show you how to do what you need:
Book2
ABCDEF
1Time StampHoursCategoryTimeCategory
212/14/22 12:12 AM38.8Midnight to 3 AM12:00 AMMidnight to 3 AM
312/13/22 3:33 PM47.5Noon to 3 PM3:00 AMMidnight to 3 AM
412/14/22 7:56 AM31.13 AM to 6 AM6:00 AM3 AM to 6 AM
512/13/22 6:08 PM44.93 PM to 6 PM9:00 AM6 AM to 9 AM
612/13/22 6:59 PM44.13 PM to 6 PM12:00 PM9 AM to Noon
712/13/22 9:38 AM53.46 AM to 9 AM3:00 PMNoon to 3 PM
812/13/22 1:48 AM61.2Midnight to 3 AM6:00 PM3 PM to 6 PM
912/14/22 3:27 AM35.6Midnight to 3 AM9:00 PM6 PM to 9 PM
1012/13/22 5:23 PM45.7Noon to 3 PM11:59 PM9 PM to Midnight
1112/14/22 4:48 AM34.2Midnight to 3 AM
1212/13/22 5:37 PM45.4Noon to 3 PM
1312/13/22 2:25 PM48.69 AM to Noon
1412/14/22 11:16 PM15.86 PM to 9 PM
1512/13/22 12:14 PM50.89 AM to Noon
1612/14/22 11:20 AM27.76 AM to 9 AM
1712/14/22 5:22 AM33.7Midnight to 3 AM
1812/14/22 1:56 PM25.19 AM to Noon
1912/13/22 11:26 AM51.66 AM to 9 AM
2012/14/22 7:58 AM31.13 AM to 6 AM
2112/14/22 10:31 PM16.56 PM to 9 PM
Sheet1
Cell Formulas
RangeFormula
C2:C21C2=XLOOKUP(TIMEVALUE(TEXT([@[Time Stamp]],"h:mm AM/PM")),TimeCategories[Time],TimeCategories[Category],,-1)

The Category table shows how much of a pain Time can be!
Note that the Lookup Value in the XLOOKUP formula TIMEVALUE(TEXT([@[Time Stamp]],"h:mm AM/PM")) is what I needed because the values in the Time Stamp column were actual Date/Time values. You would need to adapt depending on what format your data comes in as. It does need to be in a numeric format though, so that you can do the approximate lookup.
The Approximate Value part of the XLOOKUP formula is the -1 fourth parameter [match_mode] part of the formula. What that means is that XLOOKUP will find the first value in the lookup_array that is as large as possible without being more than the lookup_value and return the corresponding return_array item.

Hope that gets you on the right track!
 
Upvote 0
Solution
Your requirements are not clear.
A concise example with expected results would help. N.B. You can post an extract with the forum's tool named XL2BB.
You can review the following which is a different spin based on post #5.

Time.xlsm
ABCDEF
1Time StampHoursTimeCategory
214-Dec-22 0:1238.8Midnight to 3 AM0Midnight to 3 AM
313-Dec-22 15:3347.53 PM to 6 PM03:003 AM to 6 AM
414-Dec-22 7:5631.16 AM to 9 AM06:006 AM to 9 AM
513-Dec-22 18:0844.96 PM to 9 PM09:009 AM to Noon
613-Dec-22 18:5944.16 PM to 9 PM12:00Noon to 3 PM
713-Dec-22 9:3853.49 AM to Noon15:003 PM to 6 PM
813-Dec-22 1:4861.2Midnight to 3 AM18:006 PM to 9 PM
914-Dec-22 3:2735.63 AM to 6 AM21:009 PM to Midnight
1013-Dec-22 17:2345.73 PM to 6 PM
1114-Dec-22 4:4834.23 AM to 6 AM
1213-Dec-22 17:3745.43 PM to 6 PM
1313-Dec-22 14:2548.6Noon to 3 PM
1414-Dec-22 23:1615.89 PM to Midnight
1513-Dec-22 12:1450.8Noon to 3 PM
1614-Dec-22 11:2027.79 AM to Noon
1714-Dec-22 5:2233.73 AM to 6 AM
1814-Dec-22 13:5625.1Noon to 3 PM
1913-Dec-22 11:2651.69 AM to Noon
2014-Dec-22 7:5831.16 AM to 9 AM
2114-Dec-22 22:3116.59 PM to Midnight
10d
Cell Formulas
RangeFormula
D2:D21D2=XLOOKUP(MOD(A2,1),$E$2:$E$9,$F$2:$F$9,,-1)
 
Upvote 0
Sorry I didn't understand that. What you need is an APROXIMATE Lookup. This may not be exactly what you need, but should show you how to do what you need:
Book2
ABCDEF
1Time StampHoursCategoryTimeCategory
212/14/22 12:12 AM38.8Midnight to 3 AM12:00 AMMidnight to 3 AM
312/13/22 3:33 PM47.5Noon to 3 PM3:00 AMMidnight to 3 AM
412/14/22 7:56 AM31.13 AM to 6 AM6:00 AM3 AM to 6 AM
512/13/22 6:08 PM44.93 PM to 6 PM9:00 AM6 AM to 9 AM
612/13/22 6:59 PM44.13 PM to 6 PM12:00 PM9 AM to Noon
712/13/22 9:38 AM53.46 AM to 9 AM3:00 PMNoon to 3 PM
812/13/22 1:48 AM61.2Midnight to 3 AM6:00 PM3 PM to 6 PM
912/14/22 3:27 AM35.6Midnight to 3 AM9:00 PM6 PM to 9 PM
1012/13/22 5:23 PM45.7Noon to 3 PM11:59 PM9 PM to Midnight
1112/14/22 4:48 AM34.2Midnight to 3 AM
1212/13/22 5:37 PM45.4Noon to 3 PM
1312/13/22 2:25 PM48.69 AM to Noon
1412/14/22 11:16 PM15.86 PM to 9 PM
1512/13/22 12:14 PM50.89 AM to Noon
1612/14/22 11:20 AM27.76 AM to 9 AM
1712/14/22 5:22 AM33.7Midnight to 3 AM
1812/14/22 1:56 PM25.19 AM to Noon
1912/13/22 11:26 AM51.66 AM to 9 AM
2012/14/22 7:58 AM31.13 AM to 6 AM
2112/14/22 10:31 PM16.56 PM to 9 PM
Sheet1
Cell Formulas
RangeFormula
C2:C21C2=XLOOKUP(TIMEVALUE(TEXT([@[Time Stamp]],"h:mm AM/PM")),TimeCategories[Time],TimeCategories[Category],,-1)

The Category table shows how much of a pain Time can be!
Note that the Lookup Value in the XLOOKUP formula TIMEVALUE(TEXT([@[Time Stamp]],"h:mm AM/PM")) is what I needed because the values in the Time Stamp column were actual Date/Time values. You would need to adapt depending on what format your data comes in as. It does need to be in a numeric format though, so that you can do the approximate lookup.
The Approximate Value part of the XLOOKUP formula is the -1 fourth parameter [match_mode] part of the formula. What that means is that XLOOKUP will find the first value in the lookup_array that is as large as possible without being more than the lookup_value and return the corresponding return_array item.

Hope that gets you on the right track!
Thanks you so much for your time, With your help I am able to get the desired result. I also used used a macro for date conversion.

Formula = =IF(AND(DAY(A2)=DAY(TODAY()),B2<14),XLOOKUP(TIMEVALUE(TEXT(TIME(HOUR(A2),MINUTE(A2), SECOND(A2)),"h:mm AM/PM")),$C$2:$C$23,$D$2:$D$23,,-1),IF(B2>72,">72 Hours",IF(B2>48,">48 Hours",IF(B2>36,">36 Hours",IF(B2>24,">24 Hours",IF(B2>14,">14 Hours","<14 Hours"))))))
 
Upvote 0
Are the brackets that you are using correct? What should show for 3:27?
You could use a lookup for the hours part.

Time.xlsm
HIJKL
1HoursText
2oror0<= 14 Hours
3>36 Hours> 36 Hours> 36 Hours14.001> 14 Hours
424.001> 24 Hours
536.001> 36 Hours
672.001> 72 Hours
10d
Cell Formulas
RangeFormula
H3H3=IF(B2>72,">72 Hours",IF(B2>48,">48 Hours",IF(B2>36,">36 Hours",IF(B2>24,">24 Hours",IF(B2>14,">14 Hours","<14 Hours")))))
I3I3=LOOKUP(B2,K2:L6)
J3J3=LOOKUP(B2,aL)


J3 Names the lookup information.
 
Upvote 0
Your requirements are not clear.
A concise example with expected results would help. N.B. You can post an extract with the forum's tool named XL2BB.
You can review the following which is a different spin based on post #5.

Time.xlsm
ABCDEF
1Time StampHoursTimeCategory
214-Dec-22 0:1238.8Midnight to 3 AM0Midnight to 3 AM
313-Dec-22 15:3347.53 PM to 6 PM03:003 AM to 6 AM
414-Dec-22 7:5631.16 AM to 9 AM06:006 AM to 9 AM
513-Dec-22 18:0844.96 PM to 9 PM09:009 AM to Noon
613-Dec-22 18:5944.16 PM to 9 PM12:00Noon to 3 PM
713-Dec-22 9:3853.49 AM to Noon15:003 PM to 6 PM
813-Dec-22 1:4861.2Midnight to 3 AM18:006 PM to 9 PM
914-Dec-22 3:2735.63 AM to 6 AM21:009 PM to Midnight
1013-Dec-22 17:2345.73 PM to 6 PM
1114-Dec-22 4:4834.23 AM to 6 AM
1213-Dec-22 17:3745.43 PM to 6 PM
1313-Dec-22 14:2548.6Noon to 3 PM
1414-Dec-22 23:1615.89 PM to Midnight
1513-Dec-22 12:1450.8Noon to 3 PM
1614-Dec-22 11:2027.79 AM to Noon
1714-Dec-22 5:2233.73 AM to 6 AM
1814-Dec-22 13:5625.1Noon to 3 PM
1913-Dec-22 11:2651.69 AM to Noon
2014-Dec-22 7:5831.16 AM to 9 AM
2114-Dec-22 22:3116.59 PM to Midnight
10d
Cell Formulas
RangeFormula
D2:D21D2=XLOOKUP(MOD(A2,1),$E$2:$E$9,$F$2:$F$9,,-1)
Really? That's the same exact thing except you didn't use a Table which not only does it make very clear what's going on, and expand/contract as needed with no change in formulas, but also prevents cell locking mistakes. There are few reasons to NOT use a full Excel Table instead of flat addresses.
 
Upvote 0
If you prefer, you can use a Table, Name the range of lookup information, or name the array of lookup information.

The Lookup information is not the same. Post #5 has the category "Midnight to 3 AM" twice.
Why do 3:27 and 5:22 AM show as Midnight to 3 AM?
 
Upvote 0

Forum statistics

Threads
1,224,822
Messages
6,181,164
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top