Combining object key value pairs based on object key - arrays

I have an array of key value pairs. A date and a quantity, but many have the same date, and I need to combine the quantities for each specific date. I've tried a million different for loops and pushing to an array in every way I can think of using vanilla JS, then moved to lodash and still no avail. This is what I currently have.
`data = [
[February 3rd 2020: 1]
[February 4th 2020: 1]
[February 3rd 2020: 3]
[February 5th 2020: 2]
]`
I can get an array of unique dates, and an array of unique values, but I'm stuck when it comes to getting the values combined in any form so that the quantities for each date can be manipulated into simple arithmetic... sum, average...
Appreciate it.

Related

SUM of 2 columns from VLOOKUP in Google Sheets when the search key is not in both columns

I have 2 columns of File # data, representing different weeks in a payroll cycle. I also have 2 columns of Regular Hours data. I am using VLOOKUP and SUM to add the Regular Hours together to receive hours for the pay period.
=SUM(VLOOKUP($AI2,RICS_TimeClocks!O$2:S,4, FALSE) , VLOOKUP($AI2,RICS_TimeClocks!T$2:X,4, FALSE))
I have the File #s and Names flattened into one column each with
=UNIQUE(FLATTEN(
The issue I have now though, is that there are employees that only worked on one of the weeks, resulting in a
"Did not find value '____' in VLOOKUP evaluation"...
Any suggestions so that the formula can function when there is information in only one of 2 data columns? Example, File # 43021 only works in the second week, and File # 43034 only works in the first week, but I still want to be able to compute and display their total hours.
...or a better way to match and add the information into another flattened column of information?
try IFNA set to zero:
=SUM(IFNA(VLOOKUP($AI2, RICS_TimeClocks!O$2:S, 4, 0), 0),
IFNA(VLOOKUP($AI2, RICS_TimeClocks!T$2:X, 4, 0), 0))

I need to provide a tabulated output of this month, month+1, month +2, and month +3 derived from numerous tables within the one sheet

The History:
I have a data set that refreshes every Monday morning adding last week's values to a growing tally until there is 52 weeks in the data set (9 separate cohorts), across 38 different departments.
I have built a power query to filter the department and compiled tables for each cohort, limiting the data to the last 17 weeks, and using excel forecast modelling then setup each table to forecast 16 weeks ahead.
Because the week beginning (WB) dates keep changing IO cant hard code the result table to cells within each cohort table.
My result table needs to show current month, month +1, month +2, and month +3 forecast values as per the highest date closest to or equal to EOM and I need this to be automated, hence a formula.
PS added complexity is that the table has date/value adjacent in (last 17 weeks) and columns separated in future 16 weeks of data in each table. Structure is exactly the same across all the 9 cohort forecast tables.
My Question:
Am I best to use a nested EOM formula, or VLOOKUP(MAX) based on the cohort_forecast_table image link below?
Because the current month needs to be current I have created a cell using =NOW().
I then complete a VLOOKUP within each cell in the master table that references the references the data in each sub-table usin MAX and EOMONTH for current month, then month+1, month+2, month+3, etc.
In a simplified broken down solution:
Date array = 'D3:D35'
Volume array = 'E3:E35'
End of current month formula cell B3: =MAX(($D$3:$D$35<EOMONTH(D1,0))*D3:D35)
Call for result in cell C3:
'=VLOOKUP(B3,Dates:Volumes,2,FALSE)'
I think this will work for me and thank you all...

Issue with a multiple criteria INDEX MATCH formula

so I used this array formula with INDEX MATCH:
{=INDEX(ENTRIES!$F$4:$F$28;MATCH(C4&F4&G4;ENTRIES!$C$4:$C$28&ENTRIES!$G$4:$G$28&ENTRIES!$H$4:$H$28;0))}
Here is the thing, I was trying to display the price of the "entries" sheet on the "sales" sheet, the problem comes up when there are different prices for one "Code" or product over time. I tried to solve it with an Index Match formula (above) that matches the price of the code (product) with the month and the year but it doesn't assign the price or any value on the months between the updates of the price. see picture
example: for month 6 it should assign the price of month 5 because there is not any update or change. and the same for month 9 it should be the same e of the month 8 for that product. How can I do that?
Looks to me like it's throwing those errors because it won't be able to find these months. In all cases these months are missing, at least with your data, you could tell the formula to pick the maximum row from the data that is below or equal to your search month using MAX()
Furthermore, matching multiple criteria through concatenating cells and columns can get tricky once numeric values/dates are involves and could throw back wrong/unexpected results. Try something along these lines instead:
MATCH(1,((Criteria1)*(Criteria2)*(Criteria3))...
So the whole thing would look like:
=INDEX(ENTRIES!$F$1:$F$28;MAX(((ENTRIES!$C$4:$C$28=C4)*(ENTRIES!$G$4:$G$28<=G4)*(ENTRIES!$H$4:$H$28=H4)*ROW(ENTRIES!$F$4:$F$28))))
Entered through CtrlShiftEnter
#JVDV answer was helpful, but it didn't work for me because it gives me a higher value instead of the latest price for the last month known not the next one.
Anyway looking at your formula, I finally came up with this:
{=IFERROR((INDEX(ENTRIES!$F$4:$F$28;MATCH(C8&J8&K8;ENTRIES!$C$4:$C$28&ENTRIES!$G$4:$G$28&ENTRIES!$H$4:$H$28;0)));INDEX(ENTRIES!$F$4:$F$28;MATCH(1;(ENTRIES!$C$4:$C$28=C8)*(ENTRIES!$G$4:$G$28=J8-1)*(ENTRIES!$H$4:$H$28=K8);0)))}
The first part is my original formula but now when it throws an error applies the second INDEX which finds the price of the month before the month with no price in the data. Of course, it's isn't perfect either because I have to "update" the price at least every two months.
I tried another way with a <= sign but it didn't work either.

couchdb map / reduce multiple keys filtering by date

I have a view setup with a map reduce. Right now this code works great:
function(doc) {
if (doc.type == 'test'){
if(doc.trash != 1){
for (var id in doc.items) {
emit([id,doc.items[id].name], 1);
}
}
}
}
function(keys,prices){
return (keys, sum(prices));
}
I get a return and when using the group parameter, it condenses everything just fine.
My issue/question, I want to add a third key.... DATE, so I may only reduce records from certain dates. So for example:
function(doc) {
if (doc.type == 'test'){
if(doc.trash != 1){
for (var id in doc.items) {
emit([date,id,doc.items[id].name], 1);
}
}
}
}
My issue is that since date is at the beginning of the array, the reduce groups by date, id etc. I know I use group_level and say just take the first key from the array or the first 2 keys, but that doesn't help either because afaik, group_level goes from left to right in the array. I could put the date on the end of the emit array, but that doesn't help either because I need to have values at the beginning of my startkey and endkey to search on.
Here is an example of the output of data:
{"key":["2012-03-13","356752b8a5f6871f3","Apple"],"value":1},
{"key":["2012-03-20","123752b8a76986857","Pear"],"value":1},
{"key":["2012-04-12","3013531de05871194","Grapefruit"],"value":1},
{"key":["2012-04-12","356752b8a5f6871f3","Apple"],"value":1},
I want APPLE to be added up in one row, here it's adding up apples by date first. I was able to successfully just add up all the apples if I remove DATE as the first key in the array, but then I can't search by date range.
Any ideas on how to accomplish this?
If I correctly understand what you want to do, then you'd want to put the date as the first element of your array, and use group_level as well as start_key and end_key.
Eg. startkey=[1, "someid"] endkey=[1,"someid",{}] group_level=2
Will get you all items from date 1 (obviously choose your own format here), with id "someid" and any name. It seems funny that you emit id's before names, and without having more information about what you're actually trying to accomplish, it's hard to advise your general data model. If ID is a "type" id meaning that many items share the same ID then this makes sense. If ID is a unique per item ID, then it does not. In that case, you'd want to emit "name" before ID...
Edit 1
As per your comment, to do a range of dates you do this:
startkey=[1] endkey=[5,{}] group_level=2
You will get everything from date 1 to date 5 grouped by id ie. apples, oranges etc. I use this exact technique in a very large scale production application. I actually formatted the dates as an easily human readable integers of the format yyyymmdd, so 20140624 would sort to the top. If I want everything from the start of the month till now grouped by my group ids, I call
startkey=[20140601] endkey=[20140624,{}] group_level=2
It works perfectly and as far as I can tell that's what you're looking to do. I also have a third key layer "detail" which allows me to provide a deeper level of grouping for items that need it. I can then call
startkey=[20140601, "someid"] endkey=[20140624, "someid",{}] group_level=3
To drill to the detail level for a particular id, or just use the previous query with group_level=3 if I want the details for every id. I'm certain you can make this work - I've solved this exact problem in a production application using the techniques described.
Edit 2
If you want to group all apples regardless of date, then you'll need to let apples be the first element in the key. You can then get all apples over all time as a single row in the view result using group_level=1, and Apples over a date range using group_level=2. The difference here is that you'll only be able to do the group_level=2 query on a single item type at a time. If you want the best of both worlds, you unfortunately just need to make 2 views. That's just how key ordering works... If you need fast response times for both types of queries, all item types over a date range, and all of a particular item not grouped by date, I believe 2 views is the only way to achieve that.
Note
Another thing to note is about your reduce function. Wherever possible it is highly recommended that you use the built in reduce functions. They're implemented in erlang and are highly optimized compared to custom javascript reduce functions.
In your case, just replace your reduce function with this
_sum
Easy hey?
If you post more info about your application, data model etc. then I'd be happy to help out more with your database design.

SSRS MDX for Data from previous two weeks

I am trying to get the data fromt the present calender week to the previous two weeks. If I specify the date range as parameters in the MDX query, the table gets filled up properly.
SELECT NON EMPTY {[Measures].[planned_cumulative],[Measures].[Last Forecast CW] } ON COLUMNS,
NON EMPTY { ([Project].[Projekt-Task].[Task].ALLMEMBERS* { [Date].[Date - CW].[Week].&[2014]&[201420] : [Date].[Date - CW].[Week].&[2014]&[201422]})} ON ROWS FROM [DWH]
But if I try and use the lag function, I get an error. Here is the MDX query.
SELECT NON EMPTY {[Measures].[planned_cumulative],[Measures].[Last Forecast CW] } ON COLUMNS,
NON EMPTY { ([Project].[Projekt-Task].[Task].ALLMEMBERS* [Date].[DATECW - CW].[Week].CurrentMember.Lag(2) ) } ON ROWS
FROM [DWH]
Your first query (quite rightly) identifies a range of two weeks, by using two members from the dimension separated by a colon i.e. [Week X] : [Week Y].
Your second query uses the LAG function, which will return a member on your dimension, not a range of members.
Try this:
SELECT NON EMPTY {[Measures].[planned_cumulative],[Measures].[Last Forecast CW] } ON COLUMNS,
NON EMPTY {[Project].[Projekt-Task].[Task].ALLMEMBERS * TAIL([Date].[Date - CW].[Week],3) } ON ROWS
FROM [DWH]
Also, your first query refers to [Date].[Date - CW].[Week] whereas your second query uses [Date].[DATECW - CW].[Week]. I have gone with the first option as you say that query works.
Let me know if that helps.
Ash
EDIT: Apologies for that. I have amended the query to use the TAIL function, which will give you a number of tuples from a set starting at the end and working backwards. So with TAIL ({Set Of Weeks},3) this should give you the current week and the previous two weeks. I have tested a similar query on my cube and it seems produce the expected results.
Hope that solves it for you.
Ash

Resources