Create UI to filter and aggregate in Excel - excel

I have a long excel containing dates and many value columns. I would want to create a dynamic filter where the user can input a date range in two cells and the values from each column will be filtered by the date range and an aggregated value calculated from the filtered values will be shown.
For example in the table:
Row year weight_pounds gestation_weeks
1 2005 6.0627122049999995 38
2 2006 7.12534030784 40
3 2007 7.4406013425 39
4 2008 3.30032006214 43
5 2009 7.1099079495 41
6 2010 7.31273323054 40
7 2010 9.31232594688 42
I want to filter by year 2007 to 2009 and show the average on both value columns in the sheet.
How can I approach this in excel? I have no experience to VBA but I am open to VBA if it is required.

Define a helper column that tests whether the row is within the required date range and filter based on that column.
Suppose the start date is in X1 and end date is in X2. And suppose your main data table starts with the heading Row in A1.
Then the formula in D2 might be something like =and(B2>=X1,B2<=X2)
Copy that formula down the whole list to give a column with TRUE and FALSE values
Turn on filters on the heading row
Select just TRUE in the helper column
For the average you can either use =SUBTOTAL(101,C:C) (the 101 parameter says to give the average of the rows that are still visible) or =AVERAGEIFS(C:C,B:B,">="&X1,B:B,"<="&X2) (which works regardless of how the main data table has been filtered.

Related

Conditional Formatting in Pivot Table in Excel based on text field

I would like to color code a pivot table based on a text field within the data.
This is a mock up, as I'm at a loss as to how this can be done. One of the tricky parts is that you can't actually display the text field that is the basis for the color.
Thank you,
Neil
Contract
Month
PayStatus
Payment
1
Jan-21
Complete
90
2
Jan-21
Complete
95
3
Jan-21
Complete
93
4
Jan-21
Complete
94
1
Feb-21
Pending
91
2
Feb-21
Complete
95
3
Feb-21
Complete
92
4
Feb-21
Complete
94
2
Mar-21
Complete
96
3
Mar-21
Pending
93
4
Mar-21
Pending
94
2
Apr-21
Complete
95
3
Apr-21
Pending
94
Mock-up Pivot Mock-up (color coded)
You still don't need to use VBA to get the results you're looking for, but there is a little work to do in order to set it up. The solution is to use Conditional Formatting in which the formula is VLOOKUP. To make it work, you need to modify your table slightly.
VLOOKUP works by finding a unique value in the first column of a table or range. The first column values in your table are not unique, nor are the values in the second column. But if you combine the first and second columns, you can create a unique value. A column must be added to the left of your data to create this unique value:
The formula in Column A combines the values in columns B and C:
=B2&"+"&TEXT(C2,"mmm")
(There's a reason to format the date as just the Month, more later)
What we're ultimately looking for in your formatting rules is when any value in the pivot table, for a given month and contract, matches either "Complete" or "Pending". Because we've set up the data with the helper column, we can now create a `VLOOKUP' that can work for us. So we need a formula that does this:
VLOOKUP(unique value based on month and contract,get the pay status) = "Complete"
VLOOKUP(unique value based on month and contract,get the pay status) = "Pending"
The formula in the VLOOKUP has to reference back to the source data (when it's inside the pivot table). In order to lookup that unique value, we have to create it by combining values from the pivot table. If the VLOOKUP formula was in Cell B6 in the pivot table:
Then the formula will be:
=VLOOKUP($A6&"+"&B$4,Sheet1!$A$1:$E$14,4,FALSE)="Complete"
Notice how the "lookup value" is not a single cell, but we're creating the same unique string/value in the helper column.
In your pivot table, click in the values area ("Sum of Payment") in Cell B6, then select Home -->Conditional Formatting-->Manage Rules. Next "Add New Rule" and then make sure your rule looks like this:
Create a second rule in the same manner and apply it to the pivot table.

Copy Data from once cell to another cell based on conditions

I am facing an issue in excel in driving the formulas. I have data (Year, Salary, Name) in one sheet. It is updated vertically. In another sheet, I want to get list of salary for particular year. For 2019 there may be 4 rows for salary so I want all of them one by one. Below is the data:
Salary EMP Year
10000 a 2017
11500 b 2018
13000 c 2019
14500 d 2017
16000 f 2018
17500 s 2019
19000 g 2017
20500 h 2018
22000 h 2019
Output I require:
Year 2017 2018 2019
10000 11500 13000
14500 16000 17500
19000 20500 22000
So output should contain list of data for 2019 when I enter 2019 (all rows). there may be 50 rows in actual as I have given simple example of 8 rows only.
Kindly help me on this.
Thanks, Sharmil
I tried with pivot but it is giving total value rather than individual values. Also I tried with OFFSET, Index + Match function but it does not provide me correct output. Combination of OFFSET, Index + Match gives all rows for all single year.
Excuse me for the German language. SUMMEWENN is, indeed, SUMIF function. You can use a sumif Formula:
Sumif(Your criteria, Your search criterua, Your desired range to be summed)
Please see the below screenshot illustrating your example
Your criteria: Range C2 to C11
Your search criteria: B5 --> 2017
Your desired range to be summed: A2 to A11
Pivot Table is a possible solution but you will need a helper column to "rank" the Year.
Put the following formula in Cell D2 suppose your data range is A2:C13:
=COUNTIF($C$2:C2,C2)
Dray it down to apply across rows.
Then sort the data range by Year and then by Salary from Smallest to Largest.
Then insert a pivot table and layout the data as shown below.
The problem can also be solved by formula or power query but if a list like that is all you want I believe using Pivot Table is the most efficient way.
Let me know if you have any question. Cheers :)

How to do a running count of dates within a date range in excel?

I am trying to build a formula in excel, that will do a running count of dates within a specified date range for an item. For example: Item A has a Start Date of 7/15 and an End Date of 7/18. I need a formula that will "tally" or add "1" to a running count for 7/15, 7/16, 7/17, 7/18 in another column that has a comprehensive list of dates. How can I do this?
EXAMPLE ATTACHED AS LINK
Well... Some examples would be helpful, but it sounds like you're looking for countifs.
If we create two excel tables, one called Items with Start and End dates:
Title Start End
ItemA 7/15/2017 7/18/2017
ItemB 7/16/2017 7/20/2017
And one called Dates which counts the Items
Date Count
7/15/2017 1
7/16/2017 2
7/17/2017 2
7/18/2017 2
7/19/2017 1
7/20/2017 1
Then our formula in the Count column of Dates would be
=COUNTIFS(Items[Start],"<="&[#Date],Items[End],">="&[#Date])
In plain english, this is saying "Count all items in Table Items where the value in the Start column is less than or equal to the value in my Date column and the End column is greater than or equal to the value in my Date column. Note that these formulas are entirely in Excel Table format, which makes for more readable formulas. If we want A1 notation, we will assume that the Items table starts at A1, which would result in the following formula:
=COUNTIFS($B$2:$B$3,"<="&$E2,$C$2:$C$3,">="&$E2)

Excel: Select Range in Formula based on column value

I have two tables that have similar columns:
Table1:
Date Ticker Data
May 2008, XYZ, 6
Aug 2010, XYZ, 5
Sep 2008, ABC, 7
Table2:
Date Ticker Data
EDIT:
* Apply SMALL formula below to ABC tickers, XYZ etc...
I'm trying to apply this formula: =SMALL($A:$A,COUNTIF($A:$A,"<="&A1))
which finds the nearest date in Table 2 from the date in Table 1
Table 2 is huge and has numerous entries of all kinds of dates and tickers.
The problem is I need to apply the SMALL formula above only to similar tickers. How do I adjust the range in the formula to ONLY apply to range with the same ticker in Table 1 and Table2? There are a few thousand so can't manually name the ranges.
Thanks!
This seemed to work for me for Excel 2010 & 2013:
{=max(if(ticker range = TICKER,if(date range <= DATE,date range)))}
CTRL+SHFT+ENTER as it is an array
I believe Excel 2016 has MaxIFS function that might make it bit easier. Will have to apply to spreadsheet and see how it works.

Get average of cells conditionally in Excel 2010

I have a table in Excel with 2 columns. Column A (Owner) has a list of names. Column B (Duration) has a list of numbers. I need to get the average of numbers for each of the owner. So for James it would be (4 + 5 + 18) / 3. I know how to get the average in Excel but I don't know how to conditional say:
Use the value in Column B if value in A = "James"
A B
---------------
Owner Duration
James 4
Dan 67
Ger 3
James 5
Ger 75
James 18
The AVERAGEIF function will allow you to do this.
=AVERAGEIF(A2:A7,"James",B2:B7)
The first argument is the range which will be subject to the criteria
The second argument is the criteria.
The third argument is the range (corresponding to the first range) with values to be averaged.
There is also a SUMIF, COUNTIF, and others.
You will need to do this in a pivot table.
Select all your data.
Go to Insert >> pivot table >> click ok
Select both columns (COLA, COLB) from the list of fields
available on the right.
By default COLB will go under VALUES.
Click on the arrow that says Sum of COLB >> click on value field
settings >> Select Average function instead of sum.
Check this link https://support.office.com/en-us/article/Create-or-delete-a-PivotTable-or-PivotChart-report-d09e4d07-8cd6-4b60-afad-8fb67418800f?CorrelationId=78b545a8-649b-400a-9941-a23ef409c95b&ui=en-US&rs=en-US&ad=US#_Toc263767342 for more info on pivot tables

Resources