Microsoft Access Query - Finding Data Based On Date - excel

I have an excel table in which there are multiple date columns. Each date column has another column next to it that has the total number of items for that day. So, for example, the table has Date 1, then Number of Items 1, then Date 2 and Number of Items 2 and so on and so forth up until 10.
Example:
Date 1 --| Number of Items 1| Date 2-----| Number of Items 2
9/10/13 -| -------2---------| 9/11/13----|-------3---------
9/9/13 --| -------2---------| 9/10/13----|-------3---------
I need a way in Microsoft Access to design a query that can grab the total number of items for a specified date in each row. The issue is that the dates that are entered into each date column aren't going to be uniform, meaning that the first row entry for Date 1 and Date 2 might be 9/10/13 and 9/11/13 but the entry into the next row for Date 1 and Date 2 might be 9/09/13 and 9/10/13 respectively (see the example above).
The purpose of this is to be able to determine the total number of items people have entered into the table based on the date I specify. As you can see, this presents an issue because if I needed to find the number of items for 9/10/13, I would need to somehow pull in the data in the first row from the Number of Items 1 column and from row two in the Number of Items 2 column. Although the structure of the table isn't ideal for what I'm trying to accomplish, I have no control over its design.
Is this possible, and if so how can it be accomplished?

You can pull the first pair of columns in one SELECT query, then union that with a SELECT of the second pair, and so forth ...
SELECT [Date 1] As the_date, [Number of Items 1] AS number_of_items
FROM YourTable
UNION ALL
SELECT [Date 2] As the_date, [Number of Items 2] AS number_of_items
FROM YourTable
You can later use that big union query as the input for another where you do a GROUP BY date and sum the number of items for each date.

Related

Calculating days between two oldest dates on varying amount of date values

I'm trying to calculate a number of days between two oldest days. The task would be easy, if it were only two dates, however - there could be 3 or 4 dates - I need to take the oldest and the earliest and just subtract them, however the challenge is varying amount of dates per ID.
I've got a pivot table where on the A column I have the ID and on the B column I have multiple rows of sorted dates for that ID. Example of Table (also adding a picture for reference):
Column A
Column B
ID 1
Date 1
Date 2
ID 2
Date 3
Date 4
Date 5
ID 3
Date 6
Date 7
Example: For ID 1 (in Column A) - to get the result that I need - I would do Date 2 - Date 1
Example: for ID 2 (in Column A) - to get the result that I need - I would have to do Date 5 - Date 3
Is there an easy way to do it in Excel? Or would I have to result to a programming language - putting IDs into Objects and storing all the dates into arrays and then manipulating it in arrays?
I've tried finding suitable functions in Excel and reading more about Pivot tables, but with no luck. Maybe someone can put in me on the right path.
If you are using Excel 365 you can use the following formulas.
If not, you have to replace the formula in D4 with a unique list of your IDs. And you will have to drag down the formulas for column E and F.
Using MINIFS/MAXIFS then retrieves the min/max dates of a single ID
"=(MAX(FILTER(B:B;A:A=A1)))-MIN(FILTER(B:B;A:A=A1))"
This solution works with Excel 365.
Filter function acts as a VLOOKUP function, but it can actually store multiple values and you can find Minimum and Maximum values for each ID and subtract them .

Sum of specific number of rows for a id in excel

I am pretty new to excel formulas. I want to calculate the sum of prices for every unique id. Using SUMIF I was able to do that for every unique id but I only want to calculate the sum of a number of rows for that id.
=SumIF(A:A;C2;B:B)
Sample data is attached. Actual data set is quite large. For Example, For Id 1, I only want to calculate the sum of first 3 rows (Column price) corressponding to that id, for id 2 the sum of first 4 rows and so on.
Sample data:
This formula works for sorted and non sorted data:
=SUMIF($A$2:INDEX(A:A,AGGREGATE(15,6,ROW($A$2:$A$20)/($A$2:$A$20=C2),D2)),C2,$B$2:INDEX(B:B,AGGREGATE(15,6,ROW($A$2:$A$20)/($A$2:$A$20=C2),D2)))
Enter this as an array formula (ctrl+shift+enter):
=IFERROR(SUMPRODUCT(IF(ROW($A$2:$A$20)>SMALL(IF($A$2:$A$20=C2,ROW($A$2:$A$20),FALSE),MIN(D2,COUNTIF($A$2:$A$20,C2))),0,1),IF($A$2:$A$20=C2,1,0),$B$2:$B$20),0)

Finding MIN Invoice Number Per Customer and Removing All Rows That Don't Match

I have an Excel file that shows online sales data. There are multiple columns, as you would expect, two of which are Customer ID and Invoice Number.
My aim is to only keep rows where the Invoice Number is the first (or MIN) Invoice Number for each Customer.
On a duplicate tab, we want the same thing, but only for the second Invoice Number per Customer (i.e. the second purchase).
On a side note, there can be multiple rows for the same Customer ID and same Invoice Number because the rows are at Item level, so it may look like this:
Row 1: Customer ID = 24; Invoice Number = 1014; Item = Jelly Beans
Row 2: Customer ID = 24; Invoice Number = 1014; Item = Candy Bars
So, that's okay. We just want to make sure any rows where the Customer's Invoice Number does not reflect their first purchase (or second purchase, in the second instance) is removed from the spreadsheet.
Is there a VBA (or even just functions) to achieve this? Even in steps: such as first highlighting those rows and then using another VBA to delete rows that are highlighted?
Again, in a nutshell I'm looking to only keep rows with the MIN(Invoice Number) per Customer ID.
Use COUNTIFS to count the number of times the customer number has appeared. Use a reference that is anchored to the first cell and changes as you drag down.
Assuming your customer numbers are in column A, insert this into B1 and copy down:
=COUNTIFS($A$1:A1,A1)
Filter on 1 or 2 accordingly.
Edit:
I didn't realise that you only wanted the first row. You can exploit the fact that MATCH will return the first result and check if it equals the row number:
=MATCH(A1,A:A,0)=ROW(A1)
Note that you may need to offset MATCH slightly if your data doesn't start from row 1. For example, if your data starts in row 3, you will need to offset your MATCH result by 2:
=MATCH(A1,A:A,0)+2=ROW(A1)
Now you can filter on the first column equalling 1 or 2 and the second column equalling TRUE.

Excel: sum the last entries at month end query

I have a list of entries on a spreadsheet with a date and a percentage score. I need help in devising a formula whereby a user can select a month end date (i.e. 31 Jan) from a drop down and the average percentage of the last 15 entries will be returned.
There may be multiple entries on the same date but only the last 15 (based on the month end date) should be used. For simplicity the entries would be input in date order.
Let me know if you require any further information.
Thank you
P
Assuming your dates are in column A, the values in column B and the dropdown in D2:
=AVERAGE(OFFSET(B1,MATCH(D2,A:A,1)-1,0,-15,1))
*For this to work, your data will need to be sorted in date order!
**Also won't work if there are less than 15 rows (I assume this wouldn't be the case anyway)

Retrieve multiple results in a data table

I have set up this Excel page with approximately 40 rows and 8 columns to compare the financial benefits of 2 different products based on age.
Once I input an age at the top, the next row will have that age +1 for approximately 40 rows, and then the columns beside it will value my products at the new age in every column.
I want certain information from my 5th column, based on the age that is being inputted (I figured out this part). I have a list of ages that I need to use as inputs and want the information from column 5 for each one of those input ages at the same time. I tried using a data table.
To grab the information from column 5 I use this formula: =INDEX($B$9:$F$49,MATCH(E$55,$B$9:$B$49,),5)
if i understand, the age is in column B, and the needed info in column F ?
Also E55 is the searched Age ?
Then you where near :
try
= INDEX($B$9:$B$49,MATCH(E$55,$B$9:$B$49,),5)

Resources