Calculating CoreData query between rows - core-data

I have a CoreData Entity - 'columns' of date and distance.
e.g.
Date Dist
--- ---
01/01/2020 20
07/01/2020 70
10/01/2020 60
I'd ideally like to pull out all rows (sorted by date) - with each row calculating how many days between this row and the next, then using the next row's distance value, to calculate how much distance has been covered, divided by the number of days.
e.g.
Date Dist Days Dist Miles per day
--- --- --- --- ---
01/01/2020 20 7 days 70 10 miles per day
07/01/2020 70 3 days 60 20 miles per day
10/01/2020 60 NA NA NA
I can't figure out if I should be doing this in the query, or the code - I'm using SwiftUI.
My current code just pulls out all rows through a fetch request, I've no idea where to go from here.
Any help or advice, or if you can point me in the right direction, would be really appreciated.

Do this via code definitely. It might be cleaner to put more data in your entities rather than try recalculate this all on the fly every time. So each entity knows its own start and end date as well as start and end distance.
When you add a new entity just update the previous one with its end values.
Also you could have a routine to go through everything and recalculate it all and save.

Related

How to split excel records into multiple records using formula

I have data in the following format:
Start date
End date
Start time
End time
Volume
Price
9/12/22
9/12/22
10:00
12:00
50
300
Is there a way using formulas to split that record into hourly data? I was hoping for something that could work automatically without using vba if possible.
This is the output I am looking for:
Start Date
End Date
Time
Volume
Price
9/12/22
9/12/22
10
50
300
9/12/22
9/12/22
11
50
300
This could be done by creating an empty date time series and using sum-ifs. I would like to avoid that if possible as each day only has a few hours of prices/volumes
For ms365, try:
Formula in A5:
=LET(r,A2:F3,REDUCE(HSTACK(A1:B1,"Time",E1:F1),SEQUENCE(ROWS(r)),LAMBDA(a,b,LET(x,INDEX(r,b,1)+INDEX(r,b,3),y,INDEX(r,b,2)+INDEX(r,b,4),VSTACK(a,DROP(REDUCE(0,SEQUENCE(--TEXT(y-x,"[hh]")),LAMBDA(c,d,VSTACK(c,HSTACK(INT(x+TIME(d-{1,0},,)),INDEX(r,b,3)+TIME(d-1,,),INDEX(r,b,{5,6}))))),1))))))
Above is flexible enough to have Start- and End Date to be different.

Excel - Dividing total to a range of cells

I have a issue with my excel project. What I want to do is to divide number of working hours to cells when particular person has a working day. Right now I use QUOTIENT formula with combination with others but the problem is I'm not getting the right split of the total. So set up looks like that
Number of hours = 72
Number of Working days = 7
So I need to divide 72/7 but I need to have the result rounded to full figure (hour). So for example I need day 1 = 10h day 2=12h and day 3 to 7 each = 10h. The QUOTIENT is resulting 10h in every single day giving me result of total 70 not 72.
The problem is that the variables will change when the employee will be switched so for example the next employee will have 94 hours and 11 days. Generally its look like that that I have range of full month so from 1 to 31 and the working days are collected from "working schedule". The idea is to sum up the hours to a month normative working hours. So for example employee has 104 hours and he is working 12 days in working schedule but the monthly norm is 176 so we have 72 hours missing and those hours should be added to those days that he is working.
Example.
You can use MOD function to calculate the remainder.
so day 1 and 3 to 7 should have =quotient(72,7)
and day 2 should have =quotient(72,7)+mod(72,7)
I suppose from your question that you want the remaining hours to be added to day 2.

How to count appearences along a timeline with criteria

I have a table with 3 columns: expense, client_id, date.
The date has record of clients expense for half a year for each day.
Some clients spend many each day, but some not.
The expendure ranging from zero to few hundreds.
I want to group/count active client, which spend over 50 dollars each day, and calculate how much clients was active between 0-30 days, 30-60 days, 60-90 days, 90-120 days, 120-150 days, 150+.
I mean if client spend at least 50 dollars each day along 40 days, i add him to 30-60 days column.
client id appears only once each day.
expense
client_id
date
20
1
01/01/2000
60
2
01/01/2020
70
3
01/02/2020
the result should be like that
0-30 days
30-60 days
60-90 days
90-120 days
9
3
12
20
the values are count of active clients
Thank you a lot
There might be better solutions with less helper columns, but this is what I can offer at the moment.
Preparation
I generated some random data in columns A to C for 10 clients, one month and an expense limit of 100. You will have to adjust the expense threshold and the day ranges to your needs.
Helper columns and formulas
criteria_met checks whether the expense is higher or equal the given threshold (here 10). Formula in D2 is =IF(A2>=10,TRUE,FALSE).
is_consecutive checks whether the client had an entry on the previous day. Formula in E2 is =IF(COUNTIFS(B$1:B1,B2,C$1:C1,C2-1)>0,TRUE,FALSE).
consecutive_group assigns a number for each group of consecutive days, on which the client had an expenditure above or equal the threshold. Formula in F2 is =IF(AND(D2=TRUE,E2=TRUE),MAXIFS(F$1:F1,B$1:B1,B2),MAXIFS(F$1:F1,B$1:B1,B2)+1). Whenever the combination of criteria_met and is_consecutive is FALSE, the group number is increased by one.
days_per_group counts the number of days per client_id and consecutive_group. Formula in G2 is =COUNTIFS(B$1:B2,B2,F$1:F2,F2).
max_per_group makes sure that only the max number of consecutive days is considered per consecutive group. Formula in H2 is =IF(G2=MAXIFS(G:G,B:B,B2,F:F,F2),G2,0).
Result table
labels creates the headline. Formula in J3 is =J2&"-"&K2-1&" days"
values counts how often a number between the given thresholds occured. Formula in J4 is =COUNTIFS($H:$H,">="&I$2,$H:$H,"<"&J$2).
Requirements
Each client can only have one entry per day
The source list (A:C) has to be sorted by date
Please let me know, if you need more information.
how to count values in K4 ?
=COUNTIFS($H:$H,">="&J$2,$H:$H,"<"&K$2)

Powerpivot DAX sum based on row value

I am trying to create a running total based on 4 criteria but can't seem to get it to work - sorry very new to both powerpivot and DAX.
I have a Query that has a total of 17 columns - 13 actual data and 4 calculated.
I want to get a cumulative total based on 4 criteria.
I haven't even been able to get 1 criteria working so far:
CALCULATE(
SUM(Tonnes),
Filter(Query,[Deliveryid]=[Deliveryid])
)
Basically want to sum all the tonnes for each deliveryid - this is simple in a pivot, but i want to eventually do this for all dates before the date value in the row and for each commodity and delivery address etc.
Any help would be appreciated.
Regarding your specific requirement to sum all the tonnes for each Deliveryid, you're close. Try this: =CALCULATE(SUMX(Table1,[Tonnes]),FILTER(Table1,[Deliveryid]=EARLIER([Deliveryid]))). It will sum the Tonnes for each Deliveryid.
Regarding your follow-on desire to sum all the tonnes for each Deliveryid for the dates preceding the current date, try this: =CALCULATE(SUMX(Table1,[Tonnes]),FILTER(FILTER(Table1,[Deliveryid]=EARLIER([Deliveryid])),[Date]<EARLIER([Date]))).
The table below might help show what these do (I used very long column titles to hopefully make this more understandable):
TotalTonnesForDeliveryid (made with the first code item above) simply totals tonnes by Deliveryid. In the table below, you see the TotalTonnesForDeliveryid for Deliveryid 1 is 91 tonnes ... 38 (from 1/1/2017) + 23 (from 1/3/2017) + 30 (from 1/6/2017).
TotalTonnesForDeliveryBeforeCurrentDate (made with the second code item above) totals tonnes by Deliveryid only for the dates preceding the current date. The reason TotalTonnesForDeliveryBeforeCurrentDate shows blanks for first occurences of Deliveryid is because there was no prior delivery. If more than one delivery has been made for a particular Deliveryid, each following occurence of the Deliveryid shows an accumulation of the previous dates' tonnes. So for Deliveryid 1: the initial delivery date (1/1/2017) shows blank TotalTonnesForDeliveryidBeforeCurrentDate (because there was no previous delivery); the second delivery date (1/3/2017) shows 38 tonnes (which is what was delivered on 1/1/2017); and the third delivery date (1/6/2017) shows 61 tonnes (which is the 38 from the first delivery + the 23 from the second delivery).

Calculating days since an event in Excel

I am working with a weather data set. I am particularly interested in two columns which are cumulative precipitation and a date. My question is a simple one, though I am struggling to figure out the solution. Essentially I am wanting to determine days since precipitation. An example of the data is as follows:
WEATHER DATA
Pr Date
40 8/8/2013
40 8/8/2013
40 8/9/2013
40 8/9/2013
41 8/10/2013
41 8/10/2013
In this example, if I know the last day it rained was 8/7, then 8/8 would have a value of 1 (days since precipitation), 8/9 would be 2, and 8/10 would go back to 0. I have multiple dates because of hourly recordings (I trimmed it down for this post). I've been trying to figure it out with conditional if|then statements, but I'm thinking VBA may be more appropriate here. Any help or insight would be greatly appreciated.
Assuming cell C2 to be equal 1 (or start where you wish by adjusting the C2 value), the formula below works in the example you provided. Type in C3:
=IF(A3<>A2,0,IF(B3=B2,C2,1+C2))
Drag the formula down. Explanation:
If precipitation from time i+next is different from i it comes back to zero --> there was rain.
If time i+next is equal i, then it compares the date d+next with d.
If they are equal hold the number of days without rain from previous cell.
If they are not, add 1 day* to the value inside previous cell.
*I'm assuming you have consecutive days from the following sentence:
I have multiple dates because of hourly recordings

Resources