How to print top 3 employees in a region in excel? - excel

I have a table with three columns, region, emp_name, salaries.
I want to add one more column to this table in excel.
That new column should have either 1 or 0. 1 indicates one of the top3 employee based on salaries in a particular region.
For example, Region1 has 7 employees, namely emp1, emp2, so on emp7. emp2, emp4, emp7 have top 3 salaries.
So in the new column, emp2, emp4, emp7 must have 1 and others have 0. Let's say this table contains n number of regions.

Put this in D2 and copy down:
=IF(C2>=AGGREGATE(14,7,$C$2:$C$1000/($A$2:$A$1000=A2),MIN(COUNTIF(A:A,A2),3)),1,0)

Use a pivot table
Insert the pivot using your whole range
Add Region/Employee to Rows field (In that order)
Add Salary (SUM) to the Values field
Below Pivot Table Design = No Subtotals, No Grandtotals, Tabular Layout
Select the drop down on Employee, Select Value Filter, Select Top 10...
Complete form with Top 3 Items by Sum of Salary
If a region does not have 3 options, the top n will appear where n < 3.
If you need to use the 1, 0 option you could just do a vlookup on this table too. Note that a error will return if they are not on this list which means they are not top 3. You can use some error handling to convert this to 1, 0

Related

Multiply and Sum Data from 2 tables based off of Multiple Criteria

In one formula, I am trying to multiply and then sum up data in 2 different tables based off of criteria selected for both tables.
So, if the user picks data from column 1 in the first table but column 3 in the second table, I want to use the corresponding amounts. I tried using Sumproduct but couldn't get it to work.
I used =SUMPRODUCT(ExpenseBase * ExpenseMultiplier*(ModelTypeBase=Type)*(MultiplierTypes=Multiplier))
ExpenseBase is the data in table 1, ExpenseMultiplier is data in table 2, ModelTypeBase is the top labels in table 1 and MultiplierTypes is the top labels in table 1. If I choose X and A, I get the right answer, but if I choose B in the second table it goes to 0.
Base Tables
[1]: https://i.stack.imgur.com/S72yY.png
Use INDEX to return the correct column
Capital:
=SUMPRODUCT(INDEX($B$3:$D$10,0,MATCH($B$17,$B$2:$D$2,0)),INDEX($G$3:$I$10,0,MATCH($B$18,$G$2:$I$2,0)))
Then for Expense we change the lookup ranges:
=SUMPRODUCT(INDEX($B$12:$D$14,0,MATCH($B$17,$B$2:$D$2,0)),INDEX($G$12:$I$14,0,MATCH($B$18,$G$2:$I$2,0)))

Spotfire: calculated column based on column in another table

Is it possible to insert a calculated column (table 1) based on another table (table 2)? Prefer to do it this way instead of joining table because the data on table 2 may keep on changing.
Calc column is derived by taking the f1 value on Table 2 based on matching value from col X on Table 1 to the nearest p1 value of Table 2. If it is possible to do a calculated column based on another table, how do I create an expression for it?
#p.ysl - In order to add column 'f1' to Table 1, Columns 'x' from Table 1 and 'p1' from Table 2 should be matched. As the format of these columns are not the same (one is real and the other is integer), we cannot match them. Though we do, 'f1' added to Table 1 will be blank as the values don't match.
You can add a calculated column to Table 1 with round values. example: 29.23 will be 29 in this column and then add column 'f1' from Table 2 by comparing calculated column 'round(x,0)' from Table 1 and column 'p1' from Table 2. But, the issue is calculated columns cannot be used for relating tables.
One solution is to freeze this calculated column in order to use it for matching columns. But, when we freeze the column, the entire table will be embedded and data cannot be refreshed.
However, you can accomplish this with an R-script.
Go to Register data function under Tools menu:
In the script section, add below script.
t3 <- cbind(t1, round(t1,0))
In Input Parameters section, define what 't1' is.
In Output parameters section, define what t3 is. In this case, the result will be stored as a table.
Now, RUN the script
It will prompt you to assign input and output parameters. Input - assign column 'x' from table 1.
Output parameter will be a new table.
Note: You can save this data function if you want to.
Table 't3' is created. Now, you can add 'f1' column from Table 2 to this table by matching 'column2' and 'p1' as shown in the screenshot below.
To ensure this runs dynamically, Table 1 and Table 2 can be embedded but t3 table should be linked to source so that when new data gets added to Table 1/ Table 2, t3 will be updated automatically.
Hope this helps!

excel pivot table - count appearance of certan value in column

let's say i have folowing data for my excel pivot table:
country_id user_id answer
---------- ------- ------
1 1 Y
1 2 Y
2 3 N
2 4 Y
3 5 N
i would like to count how many "Y" i have per country.
If i put "answer" in Values as "Count of answer" i get 1 for each row. How can i count only "Y" answers?
so the result will be:
country_id answerY
----------- -------
1 2
2 1
3 0
br
Y
I have a solution similar to #pkg. Add one more column in your data and call it "answerY" with the formula:
=IF(C4="Y",1,0)
or, if your data is in a table:
=IF([#answer]="Y",1,0)
Now, set up your pivot table as follows:
Row lables: country
Values: answerY (sum)
Ordinarily I'd say to add a calculated field in a pivot table, but calculated fields work off of the aggregate values, and I can't think of a way to do this with a straight pivot table.
If you have the ability to use PowerPivot, you could create a custom column or a Dax expression that would handle this.
I'm not sure you can do that in a pivot table, but if you would like to do it outside of a pivot table you could make a couple of columns with these formulas:
Column D:
=IF(C2="Y",1,0)*A2
Column E:
=COUNTIF(D$2:D$6,B2)
This assumes that all user IDs are unique and sequential, and D$6 needs to be replaced with whatever is the last value in the column. Column E will have the values you described as answerY.

Left outer join sum with DAX

I have 2 tables:
Fact sales table (sales)
Dimensional period table (dimperiod)
I have joined them through my model on the Period column. However, when I display the sales sum I got this.
I would like to have a row where no sales were made with a sum equal to 0. As below:
I've used the following DAX but didn't work.
CustomSales:=CALCULATE(SUM([Sales]), NATURALLEFTOUTERJOIN(sales,dimperiod))
Just change the pivot table to display blanks.
Or add 0 to your measure. Pivot tables do not display blanks by default. The first option above forces the pivot to display blanks, but you'd need to set this per pivot table. Addition implicitly coerces a blank to a numeric type.
SumSales:=
SUM( 'Sales'[Sales] ) + 0

Get maximum value based on unique values

I have two columns in Excel as follows:
col1 col2
1 10
1 22
1 11
1 23
1 14
2 16
2 12
2 10
2 9
How do I write a command to returns the maximum value from col2 corresponding to each unique value in col1?
So here I need to write a command to get 23 and 16.
The data I have shown here are dummies; I have 600 unique values in col1 in my actual data set.
Pivot tables: the single most underutilized and powerful feature of excel.
File menu:
Insert
Pivot Table
Select range
Fill in as indicated in image below
Step by Step:
Place cursor in cell to right of all data on sheet.
Select insert from menu
select pivot table icon
select pivot table from pop-up of icon
use the button to select the range of cells included (all 600+ rows in 2 columns) (or just enter $A:$B)
select ok
A pivot table field list appears on the right:
Drag and drop row 1 into row labels.
drag and drop row 2 into values area.
left click on "... of Row 2"
select value field settings
select "Max" instead of count or sum
select ok and you should have desired results.
The pivot table will find the max value in row 2 for each unique value in row 1 which is what I believe you are after.
Even if a solution has been found. I think it is too complicated and oversized to use the pivot for this and it might not be possible in all scenarios. Here is one that can be found searching the internet for "max if excel" (e.g. see here; adapted)
{=MAX(IF(A:A=A1,B:B))}
Attention! This is an array formula, you have to enter it without the curly brackets and then press Ctrl+Shift+Enter to submit the formula.
Since the left hand values seem to be in order, Subtotal should work.

Resources