Excel Vlookup a total that equals to the sum of some rows in an array - excel

2 tables
day |product| detail amount
Sunday| water| 9
Sunday| mango | 10
Sunday| onion| 15
Sunday| cocacola| 3
Sunday| tomato| 14
day| place| total amount
Sunday| grocery| 39
Sunday| supermarket| 12
i want the result
result
day| product| detail amount| place
Sunday| water| 9| supermarket
Sunday| mango| 10| grocery
Sunday| onion| 15| grocery
Sunday cocacola 3 supermarket
Sunday tomato 14 grocery
the logic of it is to
1.only on sunday
2.serach for the numbers in details table that will match totals table
3.the 2 tables are always equal in grand total

Related

Sum up unique values in column per criteria in other column (if values are on the left side of the criteria column)

| A B
---|-----------------------------------------
1 | 1.900
---|-----------------------------------------
2 | 700 Product_A
3 | 700 Product_A
---|-----------------------------------------
4 | 300 Product_B
---|-----------------------------------------
5 | 200 Product_C
6 | 200 Product_C
---|------------------------------------------
7 | 700 Product_D
8 | 700 Product_D
9 | 700 Prodcut_D
10 |
With reference to the answer from this question I wanted to sum up the unique values per product in Cell A1.
Therefore, I tried to go with this formula:
A1 =SUM(INDEX(UNIQUE(IF(SUBTOTAL(2;OFFSET(B2:B9;ROW(B2:B9)-ROW(B2);1;1));A2:B9));;1))
However, as a result now I get 0 instead of 1.900.
I assume the issue results because I have the values on the left side of the criteria column.
Do you have any idea how I need to modify the formula to also make it work in the displayed column order?
All you needed to do was to change the 1 into -1 on the OFFSET():
=SUM(INDEX(UNIQUE(IF(SUBTOTAL(2,OFFSET(B2:B9,ROW(B2:B9)-ROW(B2),-1,1)),A2:B9)),,1))

doing cumulative sum for each year and month in sparksql

Input:
item loc qty year month
A IND 10 2019 13
A IND 20 2020 1
A IND 10 2020 2
A IND 40 2020 3
A IND 50 2020 5
A IND 10 2020 6
OUTPUT:
item loc sum(qty) year month
A IND 0 2019 13
A IND 10 2020 1
A IND 30 2020 2
A IND 40 2020 3
A IND 50 2020 5
A IND 90 2020 6
description:
how will i get my output is as follows:
if i am calculationg for year 2020 and month 3 then i need to consider the sum(qty) between (month-3) and (month-1) i.e. in this case it will be from year 2019 month 12 to year 2020 and month 2
so for year 2020 and month 3 the ouput will be sum(qty)=10+20+10=40
now for year 2020 and month 6
sum(qty) will be between year 2020 and month -3=3 and year 2020 and month-1=5
so sum(qty)=0(0 for month 4 which is not in the table)+40+50=90
Try this.
df.createOrReplaceTempView("test")
spark.sql("""
SELECT
item,
loc,
COALESCE(
SUM(qty) OVER (
PARTITION BY item
ORDER BY (year - 2000) * 13 + month
RANGE BETWEEN 3 PRECEDING AND 1 PRECEDING
), 0) as sum_qty,
year,
month
FROM
test
""").show
+----+---+-------+----+-----+
|item|loc|sum_qty|year|month|
+----+---+-------+----+-----+
| A|IND| 0|2019| 13|
| A|IND| 10|2020| 1|
| A|IND| 30|2020| 2|
| A|IND| 40|2020| 3|
| A|IND| 50|2020| 5|
| A|IND| 90|2020| 6|
+----+---+-------+----+-----+

How to get data from other sheet using date

i have
sheet1,
Name Date Amount
Ali 1-Sep 50
Ali 2-Sep 100
Ali 5-Sep 30
13-Sep 40
9-Aug 50
25-Sep 60
and sheet2,
Name Date Amount
Ali 1-Sep 100
Ali 2-Sep 100
Ali 5-Sep 85
Ali 13-Sep 34
Ali 9-Aug 88
Ali 25-Sep 25
on sheet 3, how do i search both sheet1 and sheet2 using dates, get the amount from both sheets and sum both amount on sheet3
output :
Nama Tarikh Total
Ali 1-Sep 150
Ali 2-Sep 200
3-Sep
4-Sep
5-Sep 115
6-Sep
7-Sep
8-Sep
9-Sep 138
10-Sep
11-Sep
12-Sep
13-Sep 74
thank you in advance
You can use the VLOOKUP function to get the total. Enter the following formula into cell C2 in Sheet3:
=IFERROR(VLOOKUP(B2, Sheet1!$B$2:$C$7, 2, 0) + VLOOKUP(B2, Sheet2!$B$2:$C$7, 2, 0), 0)
This assumes that your data in sheets 1 and 2 are structured as follows:
A B C
1 Name | Date | Amount
2 Ali | 1-Sep | 50
3 Ali | 2-Sep | 100
4 Ali | 5-Sep | 30
5 | 13-Sep | 40
6 | 9-Aug | 50
7 | 25-Sep | 60
You can use =Sheet1!A1 and =Sheet2!A1 on Sheet3.
In general: use "=[SheetName]![CellName].
To see this easier: type '=' in the required ceel in sheet3 and click another sheet to chose the required cell. Excel will do the job for you

How to split a string variable and add its values in separate rows

I want to add multiple rows by deriving them from a string column in Stata.
I have a dataset like the following one:
year countryname intensitylevel
1990 India, Pakistan 1
1991 India, Pakistan 1
1992 India, Pakistan 1
1996 India, Pakistan 1
To be more precise, I want to split the country name variable for each country separately.
In the end, I want to have a dataset like the one below:
year countryname intensitylevel
1990 India 1
1990 Pakistan 1
1991 India 1
1991 Pakistan 1
This is a simple split and reshape:
clear
input year str15 countryname intensitylevel
1990 "India, Pakistan" 1
1991 "India, Pakistan" 1
1992 "India, Pakistan" 1
1996 "India, Pakistan" 1
end
split countryname, p(,)
drop countryname
reshape long countryname, i(countryname* year)
sort year countryname
list year countryname intensitylevel, abbreviate(15) sepby(year)
+-------------------------------------+
| year countryname intensitylevel |
|-------------------------------------|
1. | 1990 Pakistan 1 |
2. | 1990 India 1 |
|-------------------------------------|
3. | 1991 Pakistan 1 |
4. | 1991 India 1 |
|-------------------------------------|
5. | 1992 Pakistan 1 |
6. | 1992 India 1 |
|-------------------------------------|
7. | 1996 Pakistan 1 |
8. | 1996 India 1 |
+-------------------------------------+

Excel, Libreoffice/Openoffice Calc: count 'right' answers

I have a table with students' answers to 20 math problems like this:
A | B | C | D | E |...
------------+-----+-----+-----+-----+...
problem no | 1 | 2 | 3 | 4 |...
------------+-----+-----+-----+-----+...
right answer| 3 | 2 | A | 15 |...
------------+-----+-----+-----+-----+...
student1 | 3 | 4 | A | 12 |...
student2 | 2 | 2 | C | 15 |...
student3 | 3 | 2 | A | 13 |...
Now a need a column that counts the 'right' answers for each student.
I can do it this so: =(IF(D$3=D5;1;0))+(IF(E$3=E5;1;0))+(IF(F$3=F5;1;0))+...
...but it's not the nicest way :)
This is a typical use case for SUMPRODUCT:
A B C D E F G
1 problem no 1 2 3 4
2 right answer 3 2 A 15 right answers per student
3 student1 3 4 A 12 2
4 student2 2 2 C 15 2
5 student3 3 2 A 13 3
Formula in G3:
=SUMPRODUCT($B$2:$E$2=$B3:$E3)
If there are more problem numbers, then the column letters in $E$2 and $E3 have to be increased.
How it works:
SUMPRODUCT takes its inner functions as array formulas. So the $B$2:$E$2=$B3:$E3 becomes a matrix of {TRUE, FALSE, TRUE, FALSE} depending of if $B$2=$B3, $C$2=$C3, $D$2=$D3, $E$2=$E3.
In Libreoffice or Openoffice TRUE is 1 and FALSE is 0. So the SUMPRODUCT sums all TRUEs.
In Excel you have to get the boolean values in numeric context first. So the Formula in Excel will be =SUMPRODUCT(($B$2:$E$2=$B3:$E3)*1).
The formula in Row 3 then can be filled down for all student rows. The $ before the row number 2 ensures that thereby the row of the right answers not changes.
Greetings
Axel

Resources