'Query to get top 5 across months
(Google Sheets) I have a set of transaction data that grows as the year goes on. I want to pull out the top 5 vendors based on grand total, and present the vendor in the first column, sum of Jan's transactions in the 2nd column, Feb in 3rd, etc. A vendor may have more than one transaction per month, so I need to sum them before sorting and deciding who are the top 5.
I was trying to kludge it together by making columns 1 (Vendor) and 2 (Jan) populate with this: =query('Data'!A:K,"Select D, Sum(K) Where A >= date '"&TEXT(DATEVALUE("1/1/2022"),"yyyy-mm-dd")&"' AND A<= date '"&TEXT(DATEVALUE("1/31/2022"),"yyyy-mm-dd")&"' Group by D Order by Sum(K) Desc Limit 5",0)
And column 3 (Feb) populate with this: =query('Data'!A:K,"Select K Where A >= date '"&TEXT(DATEVALUE("2/1/2022"),"yyyy-mm-dd")&"' AND A<= date '"&TEXT(DATEVALUE("2/28/2022"),"yyyy-mm-dd")&"' Order by K Desc Limit 5",0)
D-Vendor, K-Amount, A-Date
But I cant seem to get this to work (because doing the Feb set formula seems to want me to present the vendor column again.) And I don't blame it. It's definitely not ideal. Plus, I think I'm having problems with the top 5 order based on January being different when other months data is added. Is there a better way?
I feel like a great alternative would be to query off the pivot table I built, but I cant seem to figure out how to do that. (or if there was a way to show top 5, group all the others in pivot table easily..).
Edit: adding link to a google sheet that's kind if what I'm looking for but with fake data https://docs.google.com/spreadsheets/d/13tYeCgA4mkrtkeMVGqRlQprESNlFh9NXc6MlkYbpwho/edit#gid=0
Edit2: Expanded with a Department field. I can seem to get it doing a sortn, but that's not very dynamic and wont hold up without surgery next month.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
