简体   繁体   中英

Compare an hour's Availability Count with previous and next hour per category-DeliveryDate

I have a data with Product Category, DeliveryDate, AvailbilityCount (how many products are available for this DeliveryDate), RequestTime (what date,time the user checked the availability - Date will be a constant since it is one-day's data).

I want to identify for which category and deliveryDate, the AvailbilityCount dropped to 0 during the day and wasn't 0 for its before and after requests (based on time). I am attaching a sample data (Sheet 1) and its expected output (Sheet 2).

Sample data is:

Category    DeliveryDate    RequestTime AvailablilityCount
A   9/4/21  7/4/21 12:00    3
A   9/4/21  7/4/21 2:00     2 
A   9/4/21  7/4/21 18:00    2
B   10/4/21 7/4/21 1:00     4 
B   10/4/21 7/4/21 5:00     0
B   10/4/21 7/4/21 6:00     4 
A   9/4/21  7/4/21 19:00    0 
A   9/4/21  7/4/21 20:00    2 
C   9/4/21  7/4/21 12:00    2 
C   9/4/21  7/4/21 13:00    3
C   9/4/21  7/4/21 0:00     4 
C   9/4/21  7/4/21 17:00    2 
C   9/4/21  7/4/21 19:00    2 
C   9/4/21  7/4/21 12:00    2 
A   10/4/21 7/4/21 12:00    3
A   10/4/21 7/4/21 2:00     3 
D   9/4/21  7/4/21 12:00    2 
D   9/4/21  7/4/21 1:00     0 
D   9/4/21  7/4/21 14:00    4 
D   9/4/21  7/4/21 1:00     0
E   10/4/21 7/4/21 2:00     0 
E   10/4/21 7/4/21 11:00    0 
E   10/4/21 7/4/21 14:00    0 
F   9/4/21  7/4/21 3:00     3 
F   9/4/21  7/4/21 11:00    0
F   9/4/21  7/4/21 11:30    4 
F   9/4/21  7/4/21 12:00    4

Explanation is as below: Final Output Explanation *Category Delivery Date Count

A 9/4/21 1

For, Category A with Delivery Date as 9 April, at 19hours, AvailbilityCount is 0 where as its previous & next hours (18hrs & 20hrs) is not 0.
B 10/4/21 1

For Category B with Delivery Date as 10 April, at 5AM, the AvailbilityCount is 0 where as the availbility count before & after this (1AM & 6AM) is not 0. Since, there is no record for Catgory B with DeliveryDate between the hours 2AM & 4AM, the previous time will be 1AM."
D 9/4/21 2

Count will be 2 as there are 2 records with 0 value at 1AM.


Category C will not be in output as the AvailbilityCount is not 0 throughtout the day.
Category E will not be in output. Explanation: There are 3 records with time as 2AM, 11AM, 2PM. Record for 2AM (AvailbilityCount =0) will not be in output, as there is no record before 2AM to compare to, even though there is a record after 2AM with AvailbilityCount =0. Record at 11AM (AvailbilityCount =0) will not be output, because the Availability Count before (at 2AM) and after (at 2PM) is also 0. Record at 2PM will not be output, because there is no record after 2PM to compare to, even though there is a record before it with AvailbilityCount =0."
F 9/4/21 1 Category F will be in output. There are 4 records with time at 3AM, 11AM, 11:30AM, 12PM. Record at 3AM & 12PM will not be in output, as there is either no before/after record to compare to. Record at 11AM will be in output, since the value at 3AM & 12PM is not 0. Record at 11:30AM will be bypassed. So, basically we will take the minimum value at each hour and compare with one another.*

There's one thing not clear in your data which is AM/PM of request_time . Once you sort that out, you'll get nearly same desired output.

Another thing is that tableau is not very good at while handling table calculations or look-up functions. If you intend to use some programming language like R or python it'll be a minutes job.

Please proceed in tableau as follows-

  • Needless to say handle AM/PM in your data correctly becuase in D category tableau or any other software won't know that 1:00 is actually 1 PM whereas in some other rows you are writing it in 24 hour format.
  • create a new CF by the following calculation
INT((LOOKUP(Min([Availablility Count]), -1) <> 0) AND 
(LOOKUP(min([Availablility Count]), 1) <> 0) and
(LOOKUP(min([Availablility Count]), 0) = 0))
  • make the following settings in table calculation

在此处输入图像描述

  • generate a view like this

在此处输入图像描述

The 1 s as shown in the view, are your desired output rows.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM