Trying to get the size (in %) of subcategories in a pivot - google-sheets

I am keeping track of my (stock) portfolio in Google Sheets, as follows:
category subcategory company amount
-------------------------------------------
health care diagnostics AA 100
health care diagnostics AB 50
materials mining BA 75
financials banks CA 30
financials insurers CB 35
financials banks CC 10
financials banks CD 40
financials hedge fund CE 5
health care equipment DA 50
But now I want to extract some statistics from this, and I'm using a Pivot. Specifically, I want to see:
the relative size of each category in the portfolio
the relative size of each subcategory in the portfolio
the relative size of each company in their subcategory
The first two I get done:
For instance, I can see that:
category financials has a relative size of 30% in the portfolio
subcategory diagnostics has a relative size of 37.97% in the portfolio
What is missing however, is the third column, see mockup below:
I can now see in the last column what the relative size of each company is in its subcategory:
Company CD is 50% of the subcategory banks
Company AB is 300.33% of the subcategory diagnostics
That last column however, is not calculated but added manually to show what output I am trying to get but am unable to.
Does anyone know how to have this last column as part of the Pivot?
Here is a link to the Google Sheet used: Pivot

Currently, there is no % by subtotal option in Google Sheets to show percentages against subtotals for the whole Pivot Table:
As a workaround, you can display the subtotal percentages by filtering the Pivot Table to display only one subcategory, for example:
Add Filter here and select subcategory:
Then select the desired field, for example, banks, and click OK.:
This should display the subtotal percentages like this:
Note: You can delete category field in Rows to make the Pivot Table cleaner.
You can also submit a feature idea request to Google Workspace, please see instructions on this site: Submit ideas for Google Workspace and Cloud Identity

So I found out there are two possible solution using calculated column and another using arrayformula.
Option 1: Use a duplicate pivot table. One issue of getPivotData function is that it will run in the table calculations stage so it will return empty result. So the solution was to have two clones of the pivot table. One will be used to retrieve the data and the other to retrieve the subtotal.
The formula to use would be
=amount/getpivotdata("amount",'Copy of Pivot'!$F$10, "category", category, "subcategory", subcategory)
Where amount, category, subcategory without the double quotes are values (references) filled by Google Sheets. While the double quoted ones are column names.
You also need to set summarize by to custom for it to work.
The only issue I found here is that I couldn't find a way to identify subtotal or total rows, they do have company value equal to the first record in the group. If anyone know how to know if I am rendering a subtotal, please let me know.
The second option is to use one pivot table only but reference original data.
=amount/SUM(QUERY($A$1:$D,"Select D Where A='"&category&"' AND B='"&subcategory&"'",false))
It still shares the same total rows though.
The last option will be the least robust. It would be using arrayformula but I don't have that ready at the moment. It is main issue is that changing the column orders or adding new columns will require to keep the position of the refenced columns and move the arrayformula columns. Not to mention, handling the empty cells in a category since data is grouped.

Related

Google Sheets: Many to Many Table Join

I am trying to create a sheet to determine the amount of overlapping hours for employees.
I have one table with timeclock data for the Employees.
Table 1
And another with timeclock data for their Support Staff.
Table 2
This is the desired output. Each row from table A has all the date matches from table B. From here I would compute the number of overlapping hours in the final column and then roll that up into another sheet.
Table 3, Desired Output
(apologies for image links, I can't post inline images yet)
Sample sheet here Please let me know if you have any ideas for me!
I know its a combination of QUERY, ARRAYFORMULA, FILTER and more but I just can't find the right combo.
Here's a way of doing this type of join using only built-in functions:
=arrayformula(lambda(employee,support,
lambda(datecomp,
lambda(rows,
{vlookup(index(rows,,1),{row(employee),employee},sequence(1,columns(employee),2),false),
vlookup(index(rows,,2),{row(support),support},sequence(1,columns(support),2),false)})(
split(filter(datecomp,datecomp<>""),"|")))(
flatten(if(index(employee,,1)=transpose(index(support,,1)),row(employee)&"|"&transpose(row(support)),))))(
Employee!A1:D6,Support!A1:E5))
There's a lot going on here, but the basic idea is that we are comparing the date columns of each table against each other in a 2D IF array, and where the dates match we are obtaining the row index of each table. After some manipulations we can use these row indexes on each table in two side-by-side VLOOKUPs to obtain the joined table.
DMac,
I wrote myself a QUERY replacement custom function that uses real SQL select syntax.
For your data it looks something like (You need a tab called employee and a tab called support for this to work) :
=gsSQL("select * from employee full join support on employee.date = support.date")
See my test worksheet: (line 164 on gsSqlTest sheet)
https://docs.google.com/spreadsheets/d/1Zmyk7a7u0xvICrxen-c0CdpssrLTkHwYx6XL00Tb1ws/edit?usp=sharing
You need to add one Apps Script file to your sheet to give you the custom function:
https://github.com/demmings/gsSQL/blob/main/dist/gssql.js
For more help using more features see:
https://github.com/demmings/gsSQL
For example, changing the column titles, it would be like:
select employee.name as name .... (rest of your select).

I would like to Automatically sort a Google Sheet highest to lowest, then remove duplicate values

I have a google sheet that contains an inventory list that include manufacturer and model id's. Each line item contains a price and the amount the product is discounted below MSRP. I have the raw information dumping into a spreadsheet daily but I need the sheet to automatically sort the sheet by the make/model/highest discount value and then remove the duplicates (Removing all of the lower make/model/discount values). I could easily do this task manually but I am hoping to find a scaleable way to perform this tasks for many different lists.
To get UNIQUE Id's paste this formula in F2.
=UNIQUE(A2:A)
In G2 Paste this formula and drag down.
=IF(F2="",,SORTN(FILTER($B$2:$D,$A$2:$A=F2),1,,4,0,3,0))
Note: duplicates are highlighted in yellow and green

subtotal with product condition after applying date filter on google sheet

I am struggling to find the right formula for my table. I have a database that I want to filter per date and get the total of each product on a different page.
As I need to prepare these products per day I would like to avoid counting every product by hand.
I have this formula =SUMIF(E40:E52,"Reese - box of 4*",D40:D52)that work without filter.
How can I do this function with filter?
image
My second question is about when an order as more than one product.Like here, we have a box of 4 that contains the product behind.
image
How can I get a count of the total number of each product, applying the filter option as well?
Thanks a lot for your help!

How to do a VLOOKUP in another Google sheet based on a filtered value?

Basically I have a list of cars that I wish to hypothetically sell and captured the data in the Cars tab in this google sheet.
What I am trying to do is capture how many cars are for sale based on the car type and also consumer interest based on the car type. For example, how many "Y" the sports car has and store this total in a new tab called Consumer stats in this google sheet.
I dont know how to do a VLOOKUP so it extracts all the Y for consumer interest based on sports car for example
Any help will really mean a lot!
Thanks
Have you considered a pivot table? This seems like the perfect use case.
In your example sheet, I added 3 versions.
Your original ask, count of consumer interest by car type
Count of consumer interest by car type and model
Type and Model that have both interest and are for sale.
There are multiple tutorials online for how to set up pivot tables.
The pivot table can be more powerful if you used 0 and 1 for N and Y in your interest and for sale. Sheet's pivot tables don't do COUNTIF natively, and adding custom formulas seems like a lot of extra work.

How to reflect multiple cells based on specific criteria

Imagine a list on the left filled with employees going down the spreadsheet and headers across the top based categorized on infractions that an employee might violate. this sheet is connected to another sheet which adds a one every time a form is submitted against the employee adding up for the quarter. So employee john smith has across his row would show a 0 if he never committed this infraction and add a 1 to the column each time he did so a row might look like this. John Smith 0 4 5 0 1
The goal is to show the experts name and infraction with how many times this infraction took place removing the infractions that he did not commit so ideally it would look like John Smith 4 5 1 and the header of each number would show what he did.
The goal is to make it much easier to see who did what essentially. There will be over 100 employees and alot of 0's so optically it would look better to distill in order to quickly identify who did what and how many times.
Any ideas?
V lookups and important ranges based on if this is greater than 0 is tedious and does not exactly pull what we want. Essentially omitting the 0s and just showing what an employee has done rather than what they have not done is the goal. All index and match formulas do not seem to specifically answer this problem
simple Index V lookups and matching formulas have been tried
Not able to reflect all three variables (employee/frequency/infraction) while not showing on a master list the people who did not commit the offense
There's a few ways you could set this up. I would set this up so it
Column A = Employee
Column B = Infraction
Column C = 1
Column D = Date
That way you can do a pivot summary and have the employees, with their infractions below their name and the months/years they occurred. Also you can adjust this table as necessary, such as filter by the employee name or by date or by infraction.
The added benefit is you could create a chart with all of these as filters, like cutoff a date range or pick an employee or infraction and it can show a bar graph of all the infractions by month or something like that.
I would agree that listing your data of infractions line by line (as they happen) and using a pivot table would probably be the easiest.
You could also use the AGGREGATE function to pull from a large database as well. This way you could type in an employees name, and a list of all infractions would pop up next to the name (or wherever you would want it) with as much detail as you would like. This way is more complex, but using both a pivot table and the AGGREGATE function might get you the best of both worlds (you could searching infraction types, dates, employees, employee types, and get all the details in the world if wanted).
Hope this helps!
JW

Resources