Sum values of each parcels by months - arrays

I'm stucked here. Someone please help meeee.
I need sum some values, grouped by month. But I don't have this values on DB. Just the start date, end date, value of the parcels and number of months.
Ex.
Expense
Date of buy: 2022-01-15
Value: 500.00
Parceled: 10x
Each parcel: 50.00
End of parcels: 2022-11-15
Instead of inserting 10 lines in DB with each installment of each month. I would like to get the purchase date until the end of the installments and show in a graph the amount to be paid each month.
If I've another expense with the same value at month 03, for example. On month 04 my value by month of parcels will be 100.00, until the end of this new expense.
here is what i made:
$parceled_expenses = Expense::whereYear('end_parcels', '>=', $thisYear)
->whereMonth('end_parcels', '>=', $thisMonth)
->select(
DB::raw('sum(parcel_vl) as total'),
DB::raw('month(end_parcels) as month')
)
->groupBy('month');
This return (value of parcels => month):
items: array:3 [
126 => 5
75 => 6
50 => 12
With this data on my DB:
date_pc value parcels parcel_vl end_parcelsĀ 
15/03/2022 500.00 10 50.00 15/01/2023
16/04/2022 150.22 2 75.11 16/06/2022
16/02/2022 150.00 3 50.00 16/05/2022
02/03/2022 153.11 2 76.56 02/05/2022
09/03/2022 125.00 2 62.50 09/05/2022
Thanks Advance!

Related

Creating a date index for data restructure

I've got a dataset like in the example below (with thousands of rows for each participant):
participant-ID date duration
-------------------------------------------
id-1 01.01.2020 35
id-1 01.01.2020 15
id-1 01.01.2020 3
id-1 02.01.2020 120
id-1 02.01.2020 5
id-2 20.01.2020 13
id-2 20.01.2020 10
id-2 21.01.2020 450
id-2 21.01.2020 1
Some explanation of the data: For each participant we've collected data for several weeks everyday (with different start and ending dates for each participant) measuring the duration time of how long they've used their smartphones.
First, I would like to restructure such that the duration sum is calculated for each participant and each individual date (day 1, day 2, ...). It should look like that:
participant-ID date day duration_sum
-----------------------------------------------------------
id-1 01.01.2020 1 53
id-1 02.01.2020 2 125
id-2 20.01.2020 1 23
id-2 21.01.2020 2 451
Second, I would like to create another restructured dataset, such that I've got one row for each participant and the duration time sums for each individual day (day 1, day 2, day 3, ...). The dates are irrelevant in this case.
participant-ID duration_sum_day1 duration_sum_day2 ...
----------------------------------------------------------------------
id-1 53 125 ...
id-2 23 451 ...
I'm thankful for any advice!
This needs a few steps. First I will recreate your example data to demonstrate on:
data list list/ID (a10) date (edate10) duration (f6).
begin data
id-1 01.01.2020 35
id-1 01.01.2020 15
id-1 01.01.2020 3
id-1 02.01.2020 120
id-1 02.01.2020 5
id-2 20.01.2020 13
id-2 20.01.2020 10
id-2 21.01.2020 450
id-2 21.01.2020 1
end data.
Now to work:
* first step: summing durations per day (per ID).
dataset declare aggdata.
aggregate out=aggdata /break ID date /durationSum=sum(duration).
dataset activate aggdata.
* second step - creating a day index.
aggregate out=* mode=add /break ID/fstDay=min(date).
compute day=datediff(date, fstDay, "days")+1.
format day(f).
third step - restructure.
casestovars /id=ID /index=day /sep="_"/drop date.

SQL: Windows function

I'm stuck with a new problem
I have two tables:
BudgetNumber Name Remaing(h) EndDate
BU145122 Jhon 5 2018/11/15
BU145123 Jhon 20 2018/12/01
BU145124 Lewis 10 2018/11/15
BU145125 Lewis 50 2018/12/15
Actuals:
Actuals(h) Name DATE
5 Jhon 2018/11/01
6 Jhon 2018/11/10
5 Jhon 2018/11/25
5 Lewis 2018/12/10
So in my final table, I'd like something like:
Name Remaining(h) Date Budget
Jhon 5 2018/11/01 BU145122
Jhon 14 2018/11/01 BU145123
Jhon 9 2018/11/25 BU145123
Lewis 45 2018/12/10 BU145125
So if the date of my budget is lower than my actual date, my budget is lost
If it's not the case, I substract my Actuals from my remaing(Table B)
I can get a table with a windows table but I'm stuck with the date
Is there another way to do that than a loop?
Thanks for your help

Difference between weeks in MDX

How I can calculate difference between each week and 2 weeks ago, for a given measure in MDX?
WEEK MEASURE NEW_MEASURE
---- ------- -----------
1 10 NULL
2 5 NULL
3 20 10
4 10 5
5 40 20
Below Members work, but only without CASE statement so I have to calculate it separately:
MEMBER [Measures].[12 Week temp]
AS
([Date].[Week Year].CurrentMember, [Measures].[Total Orders]) -
([Date].[Week Year].lag(13), [Measures].[Total Orders])
MEMBER [Measures].[12 Week]
AS
CASE WHEN [Measures].[12 Week temp] = [Measures].[Total Orders] THEN 0 ELSE [Measures].[12 Week temp] END

Displaying differences with PowerPivot

Based on the following Data:
Location Salesperson Category SalesValue
North Bill Bikes 10
South Bill Bikes 90
South Bill Clothes 250
North Bill Accessories 20
South Bill Accessories 20
South Bob Bikes 200
South Bob Clothess 400
North Bob Accesories 40
I have the following Sales PivotTable in Excel 2016
Bill Bob
Bikes 100 200
Clothes 10 160
Accessories 40 40
I would now like to diplay the difference between Bill and Bob and, importantly, be able to sort the table by difference. I have tried adding the Sales a second time and displaying it as a difference to "Bill". This gives me the correct values but sorts according to the underlying sales value and not the computed difference.
Bill Bob Difference
Bikes 100 200 100
Clothes 10 160 150
Accessories 40 40 0
I am fairly sure I need to use some form of DAX calculation but am having difficulty finding out exactly how. Can anyone give me a pointer?
Create a measure for that calculation:
If Bill and Bob are columns in your table.
Difference = ABS(TableName[Bill] - TableName[Bob])
If Bill and Bob are measures:
Difference = ABS([Bill] - [Bob])
UPDATE: Expression to only calculate difference between Bob and Bill.
Create a measure (in this case DifferenceBillAndBob) and use the following expression.
DifferenceBillAndBob =
ABS (
SUMX ( FILTER ( Sales, Sales[SalesPerson] = "Bob" ), [SalesValue] )
- SUMX ( FILTER ( Sales, Sales[SalesPerson] = "Bill" ), [SalesValue] )
)
It is not tested but should work.
Let me know if this helps.

SSRS. How to group in a group?

I have SSRS report like below with Boolean parameter to show 12h view or 24h view. To fit report into single screen the 24h report need to group by every 2hr.
07:00 08:00 09:00 10:00 11:00 12:00 13:00 14:00 ...
Line 1 25 30 24 26 25 25 30 30 ...
08:00 10:00 12:00 14:00 ...
Line 1 55 50 50 60 ...
The query for the dataset is:
SELECT LineID
,Hour
,HourValue
,Target
FROM vwData
ORDER BY LineID, CASE WHEN [Hour] > 6 THEN - 1 ELSE [Hour] END
How can I achieve this?
This declares your bit variable (which should be true when they want the 24 hour view - false when 12 hour)
DECLARE #24Hour bit = 0
SELECT CASE WHEN #24Hour = 0
THEN Hour
ELSE Hour + (Hour % 2)
END AS [HourGroup]
,SUM(Target) AS [TargetTotal]
FROM vwData
GROUP BY CASE WHEN #24Hour = 0
THEN Hour
ELSE Hour + (Hour % 2)
END
If they want the 24 hour view, we make hour = hour + hour % 2. (7 = 8, 8=8, 9=10, etc., etc.). If you had a more complex query, I would suggest reading up on cross apply, but this is so simple I think this will suffice. The grouping by makes sure to aggregate the REAL 7 and REAL 8 hour records (which will both be returned as "8", if using the 24 hour view). If you don't group your results, you will get two 8 oclock records - one with the REAL 7 hour total and one with the REAL 8 hour total.
EDIT:
Since you didn't include the schema of your DB, I'm guessing that 'Target' is the value being summated, but it could just as easily be 'HourValue'. Furthermore, I have no idea why you would need LineID, so I omitted it from my answer. But you can easily modify that if it's inaccurate. In the future, you should provide some sample data and your database schema so that others aren't forced to make assumptions or guess.
You could add a calculated field with a value given by something like this: `Fields!Hour.Value + Fields!Hour.Value Mod 2' and then group on that field, using a parameter to choose the Group By field in the report (Your new field or the actual hour value).

Resources