Mar 20, 2021 03:02 AM
:wave: Hello Airtable community!
Here’s my use case:
I have a table Items where each items has a price and a date period. Given their period of validity, I can or cannot add the item to an invoice. Example: I can add item A anytime in the year as its validity starts 01/01 and ends on 12/31. Item B however is only available from 03/01 to 03/31.
I have a second table Invoices where each invoice covers a period of time, from a start date to an end date. Example: For customer A, I issue invoice #1 starting at 01/01 and ending at 02/15 and invoice B starting at 02/16 and ending at 04/30. For customer B, I only issue invoice #3 which starts at 02/15 and ends at 03/15.
So given these parameters, I should have these items in my invoices:
Does anyone have an idea how to write a formula that can check if the date period of each item overlaps with the date period of each invoice? The rule is as soon as they have one day in common, the item is eligible for the invoice.
If formulas can’t help here, I’m open to any another method that would do the trick (scripts for instance is fine too).
Cheers!
Solved! Go to Solution.
Mar 22, 2021 08:28 AM
Hi @ScottWorld, the logic was right with the combination of AND() nested in OR(), there’s just one missing AND() condition where the item start date is before or equal to the invoice start date && the item end date is equal or after the invoice end date:
IF(
OR(
AND({Item End Date}>={Invoice Start Date},{Item End Date}<={Invoice End Date}),
AND({Item Start Date}<={Invoice End Date},{Item Start Date}>={Invoice Start Date}),
AND({Item Start Date}<={Invoice Start Date},{Item End Date}>={Invoice Start Date}),
),
"Valid Item","Invalid Item"
)
Thanks for your help!
Mar 20, 2021 06:23 AM
This seems fairly straightforward. This same sort of a logic puzzle would apply to any sort of a rental business, such as a camera equipment rental business (which is a database system that I created for a large TV & film industry entertainment client of mine).
You essentially have 2 different requirements, and either one of them can be true in order for a particular item to be valid for a particular invoice:
So your formula could look something like this:
IF(
OR(
AND({Item End Date}>={Invoice Start Date},{Item End Date}<={Invoice End Date}),
AND({Item Start Date}<={Invoice End Date},{Item Start Date}>={Invoice Start Date})
),
"Valid Item","Invalid Item"
)
To further strengthen this formula to prevent against possible typos from your users, you may want to add in some additional checks, such as:
Hope this helps! :slightly_smiling_face:
p.s. If you have a budget for your project and you’d like to hire an expert Airtable consultant to help you with your system, please feel free to contact me through my website at ScottWorld.com.
Mar 22, 2021 08:28 AM
Hi @ScottWorld, the logic was right with the combination of AND() nested in OR(), there’s just one missing AND() condition where the item start date is before or equal to the invoice start date && the item end date is equal or after the invoice end date:
IF(
OR(
AND({Item End Date}>={Invoice Start Date},{Item End Date}<={Invoice End Date}),
AND({Item Start Date}<={Invoice End Date},{Item Start Date}>={Invoice Start Date}),
AND({Item Start Date}<={Invoice Start Date},{Item End Date}>={Invoice Start Date}),
),
"Valid Item","Invalid Item"
)
Thanks for your help!