Horizontal SUMIFS with two vertical criteria - excel-formula

I am given the following sales table which provide the sales that each employee made, but instead of their name I have their ID and each ID may have more than 1 row.
To map the ID back to the name, I have a look up table with each employee's name and ID.
Sales Table:
Year
ID
North
South
West
East
2020
A
58
30
74
72
2020
A
85
40
90
79
2020
B
9
82
20
5
2020
B
77
13
49
21
2020
C
85
55
37
11
2020
C
29
70
21
22
2021
A
61
37
21
42
2021
A
22
39
2
34
2021
B
62
55
9
72
2021
B
59
11
2
37
2021
C
41
22
64
47
2021
C
83
18
56
83
ID table:
ID
Name
A
Allison
B
Brandon
C
Chris
I am trying to sum up each employee's sales by a given year, and aggregate all their transactions by their name (rather than ID), so that my result looks like the following:
Result:
Report
2021
Allison
258
Brandon
307
Chris
414
I want the user to be able to select the year, and the report would automatically sum up each person's sales by the year and their name.
Any ideas on how I can accomplish this?

With FILTER:
=SUM(FILTER($C$2:$F$13,($B$2:$B$13=INDEX($I$2:$I$4,MATCH(N3,$J$2:$J$4,0)))*($A$2:$A$13=$N$2)))
With SUMPRODUCT:
=SUMPRODUCT($C$2:$F$13*($B$2:$B$13=INDEX($I$2:$I$4,MATCH(N3,$J$2:$J$4,0)))*($A$2:$A$13=$N$2))

Related

How can I split rows in excel based on a specific criteria?

I am trying to figure out a way to split a given set of customers into equally sized territories based on their known potential. Taking the example below I would need to split the 50 customers into 5 territories, 10 customers each with their potential summing up to approximately the average potential of the whole set 25.5 in the example worksheet.
Is that even possible in Excel? Would like to keep it in Excel to be able to share as a template, but I am opened to any other possibilities.
Thank you in advance
Customer Potential
Customer 1 1
Customer 2 2
Customer 3 3
Customer 4 4
Customer 5 5
Customer 6 6
Customer 7 7
Customer 8 8
Customer 9 9
Customer 10 10
Customer 11 11
Customer 12 12
Customer 13 13
Customer 14 14
Customer 15 15
Customer 16 16
Customer 17 17
Customer 18 18
Customer 19 19
Customer 20 20
Customer 21 21
Customer 22 22
Customer 23 23
Customer 24 24
Customer 25 25
Customer 26 26
Customer 27 27
Customer 28 28
Customer 29 29
Customer 30 30
Customer 31 31
Customer 32 32
Customer 33 33
Customer 34 34
Customer 35 35
Customer 36 36
Customer 37 37
Customer 38 38
Customer 39 39
Customer 40 40
Customer 41 41
Customer 42 42
Customer 43 43
Customer 44 44
Customer 45 45
Customer 46 46
Customer 47 47
Customer 48 48
Customer 49 49
Customer 50 50

How to compare two dataframes based on certain column values and remove them in pandas

I have two data frames.
df1:
userID ID Sex Date Month Year Security
John 45 Male 31 03 1975 Low
Tom 22 Male 01 01 1990 High
Mary 33 Female 23 05 1990 Medium
Hary 56 Male 15 09 1970 High
df2:
userID ID Sex Date Month Year
Hari 45 Male 31 03 1975
Luka 22 Male 01 01 1990
Johan 33 Female 23 05 1990
Irfan 56 Male 29 09 1971
John 45 Male 31 03 1975
Tom 22 Male 01 01 1990
Mary 34 Female 34 05 1980
Hary 56 Male 15 09 1970
I wanted to compare df2 with df1 and keep only those rows in df2 which are having
common values in columns (userID,ID,Date,Month,Year)
So my new df2 should look like this:
John 45 Male 31 03 1975
Tom 22 Male 01 01 1990
Hary 56 Male 15 09 1970
What could be the best approach get this in pandas?
Can someone help me in this?
Just do with simple merge follow with dropna
df2.merge(df1,how='left').dropna().drop('Security',1)
Out[318]:
userID ID Sex Date Month Year
4 John 45 Male 31 3 1975
5 Tom 22 Male 1 1 1990
7 Hary 56 Male 15 9 1970
Define the key columns which you want to merge on, and then perform an inner merge between df2 and only the key columns of df1. The default for merge is inner, so you don't need to specify it explicitly. Subsetting df1 to only these key columns ensures that you don't bring any of its columns over to df2 with the merge.
key_cols = ['userID', 'ID', 'Date', 'Month', 'Year']
df2.merge(df1.loc[:, df1.columns.isin(key_cols)])
Outputs:
userID ID Sex Date Month Year
0 John 45 Male 31 3 1975
1 Tom 22 Male 1 1 1990
2 Hary 56 Male 15 9 1970

Rearrange and regroup stacked data

I have Excel data as follows:
Mon 34
Mon 76
Mon 86
Tue 24
Tue 34
Tue 66
Wed 88
Wed 89
Wed 87
Is there a way with a formula to rewrite this data as follows:
Mon Tue Wed
34 24 88
76 66 89
86 66 87
Assuming 76 is in B2, insert a column on the left and a row above. Label the columns (say ID, day and value) and in A2 enter 1 and series fill down to A4. Then select A2:A4 and series fill down to suit.
Build a PivotTable with ID for ROWS, day for COLUMNS and value for VALUES.
Won't give quite the result you show from the data sample:

How to Transpose the data in Pivot table?

I have a source content as shown below
Name Age Month Maths Science Physics
John 21 1 80 88 76
John 21 2 89 99 78
John 21 3 76 76 89
John 21 4 78 78 90
John 21 5 88 89 96
Sara 22 1 76 76 89
Sara 22 2 78 78 90
Sara 22 3 88 89 96
Sara 22 4 76 76 89
Sara 22 5 78 78 90
and i am looking to create a pivot table in excel something like this.
Name John
Age All
Month 1 2 3 4 5
Maths 80 89 76 78 88
Science 88 99 76 78 89
Physics 76 78 89 90 96
Is this possible? Thanks for looking
On your destination sheet, select an empty range with the correct number of columns and rows.
On the Formulas ribbon, select Insert Function and then specify All Functions in the dialog box.
In the list of functions, select Transpose, and give the entire range from the source sheet that you wish to transpose;
Click OK - your data range should now be transposed on the destination sheet
Select the entire destination range and then on the Data Ribbon select Auto-Filter.
You can now filter on any of the columns as desired.
In the above answer, at step 3, you need to make the TRANSPOSE function an array formula. In other words, hit "C-S-E" -- Control-Shift-Enter. Then your function is surrounded by curly brackets indicating it is an array function.

How can I add dates to column but repeat each 24 times, in Excel?

Here is a sample from the data that I am looking at.
Hour Index Visits
0 67
1 22
2 111
3 22
4 0
5 0
6 22
7 44
8 0
9 89
10 22
11 111
12 44
13 89
14 44
15 111
16 177
17 89
18 44
19 44
20 89
21 22
22 89
23 44
24 133
25 44
26 22
27 22
28 44
29 22
30 44
31 44
32 22
what I want to do is add another column that contains dates starting with Monday which is repeated 24 times then go to Tuesday (repeated 24 times) and so on. So the result should look like:
Hour Index Visits Day
0 67 MONDAY
1 22 MONDAY
2 111 MONDAY
3 22 MONDAY
4 0 MONDAY
5 0 MONDAY
6 22 MONDAY
7 44 MONDAY
8 0 MONDAY
9 89 MONDAY
10 22 MONDAY
11 111 MONDAY
12 44 MONDAY
13 89 MONDAY
14 44 MONDAY
15 111 MONDAY
16 177 MONDAY
17 89 MONDAY
18 44 MONDAY
19 44 MONDAY
20 89 MONDAY
21 22 MONDAY
22 89 MONDAY
23 44 MONDAY
24 133 TUESDAY
25 44 TUESDAY
26 22 TUESDAY
27 22 TUESDAY
28 44 TUESDAY
29 22 TUESDAY
30 44 TUESDAY
31 44 TUESDAY
32 22 TUESDAY
I know how to get the dates to increment, but not repeat 24 times then increment. Can someone show me how to do this with Excel?
try to use this formula (I suppose that your Hour column starts from A2 cell):
=TEXT(1+MOD(1+INT(A2/24),7),"dddd")
Note, that formula works well if your excel dates starts from 01.01.1900 (which is usually default for excel on PC).
If you are using 1904 date system, you should use next formula:
=TEXT(2+MOD(1+INT(A2/24),7),"dddd")
Please try: =UPPER(TEXT(DAY(2+A2/24),"dddd")). The first 2 is to control when the sequence starts.

Resources