Summing up data from tables Ruby on Rails - ruby-on-rails

What I have is a website where I add collected data of every single shift in a factory's production lines. I add data like (Quantity in tonnes). What I want is to be able to have the data of for instance; the morning, late and night shift of the (Quantity in tonnes) which are in the Shift table and are present and visible in the Shift Index view all combined and added, and added in another page which is the Days Index page (Day contains the shifts, one day has 3 shifts), so I could see the 3 shifts' data summed up together into the data combined to see as the total output of the day.
For example, in the "Quantity in tonnes", I would like 7 + 10 + 12 (These are the inputs I already have and I have added through a form to the shifts index) to be summed up, and appear in the Days index page automatically without me interfering as "29" in the Quantity of tonnes columns in it.
How is that possible to do? I can't seem to figure out how to write the code for it so that it would loop over all the inputs and constantly give me the summed out outputs.
Let me know if you need to see any parts of my code and if there is anymore info I could add for you to understand.

Have a look at the groupdate gem, it allows you to group by day, week, hour of the day, etc.
Some code from your end would help, but here's an example use, if I wanted to get revenue for past 3 weeks:
time_range = 90.days.ago..Time.zone.now
total = Sales.where('status > 2').group_by_week(:date_scheduled, Time.zone, time_range).sum(:price)

Related

Combining a dynamic total by state in Tableau

I am using a US shootings database where the event is specified by 3 columns, state event occured, date, total casualties. I want to make a dashboard in Tableau that has dynamic sum and sorting where if the year column was a page that I could click through, the graphic would reflect the top ten states of sum(casualties) of that specified range. So my data ranges from 1924-2022, and if I started the page at 1980, it would graph the top 10 states with the sum of totals between 1924-1980. The next page could potentially be a different top 10 of states and would reflect the current top 10 states as the sum(casualties) from 1924-1981.
I hope this makes sense. I apologize if it does not as I am just starting out. I did attempt to sort the data in python by making a column for each year, and you could move horizontally along a state to see it's totals change as each year goes by. Would it be best to add these year columns as a group and sort by top 10 and year that way?
Edit:
Attempting to click through the year filter and dynamically sort the graph by top 10 states with total shootings from 1924-current year
Can you use SQL to query the database? If so, you can insert a date parameter in the query that replaces the use of pages to calculate the sum based on the earliest datapoint to the year you have selected and then just click show parameter. When you create the parameter make sure you set step size to one year and switch the parameter to slider. You can also create a state parameter that is based off that column of data and then use it to filter the data in SQL. However, you may need to use a calculated field or SQL case statements if you want to change the granularity from all the states to one selected state.
I hope this helps you. If you need more clarification, please comment and I will try to provide an example.

Repeat cell values and increment them afterwards

I'm trying to set up a structure in a sheet that allows for a date value to be incrementally (monthly) increased from a given start date. However, I also want each month to be repeated x times before it moves on to the next month, which should then also be repeated x times before moving on, and so on, until a given end date.
I've created this sheet to exemplify my desired output.
In the Example tab, I have a list of names (Sales reps) which, in this case, correspond to the "x" I mentioned previously. This means that I would like each date value to be repeated the same number of times as there are sales reps, before moving on to the next month and repeating it again the same number of times as there are sales reps. This also means that, if I increase the number of sales reps at some point, that should also be reflected in the number of times that a month is repeated. Also, the start and end dates should match the values in D3 and D5.
In columns D and E in the Example tab, I just wrote the kind of behaviour I'm looking for in static values. Can anyone help me fill in the Desired solution tab with a formula that automatically achieves that? That would be much appreciated 🙏🏻😃
EDIT: THINGS TRIED SO FAR
I created a new tab called Undesired solution where I show a hacky workaround that I tried, but that I would like to keep away from. The main reason for that is that, if the number of sales rep was to change:
I would have to manually update the first few cells of column F
I would have to manually update the formula used from G5 onwards
Here's how it looks filled with an array formula. I'm using counta() to get the number of names and month() to get the difference between start and end date in months, then applying sequence with int and mod to get the index of the required date and name, finally applying edate and vlookup to get the actual date and name.
=ArrayFormula({edate(D3,int(sequence(COUNTA(B3:B)*(month(D5)-month(D3)+1),1,0)/COUNTA(B3:B))),
vlookup(mod(sequence(COUNTA(B3:B)*(month(D5)-month(D3)+1),1,0),COUNTA(B3:B)),{sequence(rows(B3:B),1,0),B3:B},2)})

Sum in ARRAYFORMULA based on possible cell values of two rows, then subtract another sum based on two different date values in two different columns

I really couldn't put the title into words very well. I will link a template spreadsheet below.
I've been working on a formula for hours now however I keep hitting dead ends. I'm unable to effectively do what I believe should be feasible. I'd give my attempts however I believe it would be of zero help, instead I'll explain my desired outcome.
I have a page with my employees, the E column isn't populated right now as I'd like to create a formula (ARRAYFORMULA so I don't have to paste a formula into each cell) to calculate the output based on a few conditions and values.
Vacation days are calculated as follows. The CEO gets 5, managers get 3 and assistants get 1. Extra vacation days based on points employees receive, 30 points or above is 5, 20 points or above is 3 and 10 or above point is 1.
Calculating the amount of vacation days employees have earned wasn't the hard part for me, it was having the formula subtract days based on how many vacation days have been used in the past 30 days.
We log vacations on the vacation page. The formula on the employees page needs to calculate how many vacation days each employee has used in the past 30 days only and subtract that from the total earned vacation days that employee has earned.
I'd like for the formula to use TODAY() to calculate 30 days in the past however for the sake of this example I'll use the date 06/09/2021 instead for continuity.
Sorry if I haven't explained this well or I'm asking too much in one go, I figured all the context is required.
Example sheet

Algorithm for tracking changes in value over time

I am writing a rails app that deals with product inventory. I would like to include the following features, and am struggling with developing an efficient algorithm:
View stock history (how many were in stock on each date)
Quantity removed from warehouse, and quantity added to warehouse over specific periods of time
Amount of time the product was out of stock in any given period
My questions are as follows:
What is the best way of tracking changes? In addition to my Products
table, should I create another table called
HistoricProductQuantities, and insert a new record each time there
is a change in the quantity?
What number should I track? The historic stock quantity (i.e. 50 in
stock on this day, 24 in stock on that day), or the CHANGE in stock
quantity i.e. -5 (5 sold) or 15 (15 added to inventory)? Or do I
track both in separate tables?
Thanks for your help.
First of all I recommend implementing Date Dimensions on your application, as it seems like you will be doing a lot of Time related calculations. Search on Google for date dimensions as it's beyond the scope of your questions. That said, I believe it will be of great benefit for your app to implement and use date dimensions.
As far as your direct questions go:
What is the best way of tracking changes? In addition to my Products table, should I create another table called HistoricProductQuantities, and insert a new record each time there is a change in the quantity?
Yes you could do this, I would probably call it HistoricProductSnapshot and keep track of the product activity in there on daily basis. With this information as well as time dimensions you could do calculations such as "how many of Product X Did we have 5 days ago or a month ago etc etc."
What number should I track? The historic stock quantity (i.e. 50 in stock on this day, 24 in stock on that day), or the CHANGE in stock quantity i.e. -5 (5 sold) or 15 (15 added to inventory)? Or do I track both in separate tables?
I do not have experience writing inventory control software but I believe with the Snapshot table I mentioned on the question above you would only have to keep track of quantities per day. The Change in product counts could then be calculated from your snapshot table. You could for example have a function that will output the product amount in a given time range as an array. Example: From March 1 to March 7 these were the stock amounts for Product Y [45,40,39,27,22,45,44].
Hope that helps. As I said I am not a product inventory guy but I have worked with Point of Sales Systems and the procedure above should give you a could enough start for what you are trying to do.
This gem could be usefull for tracking changes in models https://github.com/collectiveidea/audited
Keep the data raw. I would personally create a new data entry every day, displaying how much items you have in stock per day. Or you can make the interval much shorter, such as every 12 hours.
For our particular use case:
We had a table called Days, which had a many to many relationship with products, and each "relationship" will have a value called quantity (to keep track of quantity of product per day). Additionally per relationship, we had another value for the relationship with transactions (a one to many relationship) that has the entries for the time of transaction and remaining stocks.
I would personally advise you to use the quantity of stock as the raw data, as it will enable you to gather the data such as how much items were removed during a certain transaction, when the item was out of stock and when it became in stock, all through the data. When you have data in which you need to perform statistical calculations on, it's best to store this data as raw values (quantity of the item).

Delphi Rbuilder: Calculate value across multiple pages of data not just first and last page

I am using Rbuilder within a application constructed with Delphi. I have a report already built that displays a list of items but then at the bottom I have some subtotal fields as well as a total field. The subtotals and totals are defined as variables which then total up the cost of the individual items.
Unfortunately both the subtotals and totals only give me calculations for items on the first and last pages of data. Lets say there are 5 pages of data that prints out. Page one the totals are accurate.
Page two totals are accurate. Page 3 totals include ONLY the totals from page 1 and page 3. Page 4 total includes page 1 and page 4 and so on. I have been trying to play around with timing settings as well as moving my code calculating the total to different operations (ongettext, onprint, oncalc, etc)
Has anybody ever run into this?
Ok, so I kept working at this and eventually found the problem.
At the report level I changed the report from TwoPass to OnePass. That ended up giving me very close to what I wanted. I ended up having to write some more code to get exactly what I wanted but changing the number of passes worked.
I was trying to display a running total page by page. And as I changed pages it would update the value.
Onepass worked.

Resources