Pacing Period To Date Calculated Measure on SSAS cube - MDX - ssas

I want to produce a calculated member on our cube using MDX Expression that shows
revenue booked at any point in time up for the current date of a given
Measure
[Measures].[Rev]
Date Dimension
[Date].[Year]
Contract Dimension
[Contract].[Booking Date]
Year-Month
Rev
Pacing Rev
2021-01
10,000
10,000
2021-02
6,000
6,000
2021-03
12,000
12,000
2021-04
7,000
7,000
2021-05
9,000
4,500
2021-06
11,000
3,600
2021-07
13,000
1,000
Essentially I want to be able to determine how much Revenue was booked this time last year.
if today = 17/05/2022
I want to see Pacing Rev for all contracts booked for 2021 from any point in time up until today 17/05/22.
What I mean by 'from any point in time. i.e. a contract could have been booked in 2018, for 2021 (hence why I think I'll need to use the [Contract].[Booking Date] some how

Related

Calculate total manufacturing output over a shift for each location

We currently have a master table stored in our SQL server with the following example information:
Site
Shift Num
Start Time
End Time
Daily Target
A
1
8:00AM
4:00PM
10000
B
1
7:00AM
3:00PM
12000
B
2
4:00PM
2:00AM
7000
C
1
6:00AM
2:00PM
5000
As you can see, there are multiples sites each with their own respective shift start & end times as well as a total daily target for the day.
Another table in the DB is populated by users via the use of a PowerApp. This PowerApp will push output values to the server like so:
Site
Shift Number
Output
Timestamp
A
1
2500
3/15/2022 9:45 AM
A
1
4200
3/15/2022 11:15 AM
A
1
5600
3/15/2022 12:37 PM
A
1
7500
3/15/2022 2:15 PM
This table contains a log of all time-stamped output entries for each site / shift.
What I would like to do is do a daily trend of output vs. target. In order to do so, all output values over a specific shift would have to be aggregated in a SUM function for a given shift grouped by the shift day. The resulting view would need to look like this:
Site
Shift Number
Day
Actual
Target
A
1
3/14
9500
10000
B
1
3/14
13000
12000
A
1
3/15
8000
10000
B
1
3/15
10000
12000
This is easy enough for daytime shifts (group by day and sum the output values). However, if you notice in the master table, Site B / Shift 2 crosses midnight. In this example, I would need to sum values from the previous day 4PM up until 2AM of today. The date grouping would be done by the Shift End Time. Here's an example of the problem area:
Site
Shift Number
Output
Timestamp
B
2
3300
3/15/2022 5:45 PM
B
2
2200
3/15/2022 8:15 PM
B
2
1600
3/16/2022 12:37 AM
B
2
2500
3/16/2022 1:15 AM
I would need these four rows to be aggregated in the view as one row like so:
Site
Shift Number
Day
Actual
Target
B
2
3/16
9600
10000
The values should be listed under March 16th since the end time of the shift occurs then. The values are summated and the target is taken from the daily target master table.
How can I properly calculate these outputs for each shift every day irrespective if it crosses into a new day or not in a view? Or should I go a different route altogether?

Find vacancy (no contract) days in each month for property/unit per year

I need to get number of vacant days in each month in every year
Leasing Contracts
So, my target is to occupy all units along the year.
For that I have to find how many days in each month units are not occupied (There s not contract) and focus on these units to reduce the price or give promotions.
So, I have lease start and lease expiry dates for the units in the first table and I want to know in summary table (second table) number of days in each month which are not belong to any of the leasing period (from/to range) from the first table for each unit.
I need Excel formulas and/or SQL scripts to be applied to find all days without rent for each project/unit in the selected year
Appreciate your support in advance!
Project Name Unit No Lease Start Lease Expiry Rent Per Annum
Building1 Unit1 01-01-2017 31-12-2017 70000
Building1 Unit1 01-01-2018 15-08-2018 60000
Building1 Unit1 01-10-2018 31-12-2018 60000
Building1 Unit2 01-01-2017 31-12-2017 60000
Building1 Unit2 01-03-2018 31-07-2018 60000
Building1 Unit2 01-09-2018 30-09-2018 45000
Vacancies Days multiply by last daily rate renting (rent per annum)/365
Project Name Unit No Jan-18 Feb-18 Aug-18 Sep-18 Oct-18
Building1 Unit1 0 0 16*164 30*164 0
Building1 Unit2 31*164 28*164 31*164 0 31*123
I would use a calendar table.
This you join with your lease table to get whether a particular day is leased or not.
Then you can group by unit and month and do a pivot to get the result you want.

Calculate stock trading position for buy and take profit in ms sql

I'm currently developing a trading diary. There are many positions having multiple buy and take profit transactions. Here's a sample:
The initial position size is 50 #20 USD
One week later, we sell 15 #22 USD
Another 2 weeks later we sell again 15 #23 USD
One week later, a pullback comes into the price so we buy another 25 #21 USD
One week later, we sell again 20 #23 USD
Another 4 weeks later we sell 25 shares #25 USD
One business rule is the following: To get the actual P/L calculation, the "last buy point" must always be used as long as we have shares in place.
In this case, we can deduct 2 x 15 Shares (330 USD + 345 USD) from the initial 50 shares (1k USD). After buying 25 shares (after the pullback), we first have to deduct 20 remaining shares (460 USD) from the initial position (50 shares for 1k USD). For the last sell point of 25 shares (625 USD) we have to use the second buy point of 25 shares #21 USD (525 USD).
Any help is appreciated.
Krgds
Sascha

Report Pending Payments with mixed Monthly & Weekly intervals

We are working with a database that tracks payment plans for patients. Some patients pay monthly, some weekly, and some every other week.
The PaymentsSchedule table fields are Patient, Frequency, NextDueDate, PaymentsRemaining, Amount. Once a payment is processed the NextDueDate and PaymentsRemaining fields are updated so that at any given time there is only one record representing all future payments for a given payment plan.
The report we want to generate would show all payments expected within the next month. Something like this:
Report Date: 3/1/2014
Patient Frequency Next Date Pmts Left Amount
01 Monthly 3/01/2014 5 $100
02 Weekly 3/02/2014 3 $30
03 Weekly 3/02/2014 7 $25
04 Bi-Weekly 3/03/2014 4 $75
02 Weekly 3/09/2014 2 $30
03 Weekly 3/09/2014 6 $25
02 Weekly 3/16/2014 1 $30
03 Weekly 3/16/2014 5 $25
04 Bi-Weekly 3/17/2014 3 $75
03 Weekly 3/23/2014 4 $25
03 Weekly 3/30/2014 3 $25
I could set up 5 different queries for the 5 date possibilities (weekly) of a payment plan for the next 31 days, pull them together in a UNION and then filter out dates that are not in my time horizon but I would like to find a simpler solution.
Your help is much appreciated.
Thanks,
Jim S

Teradata Default List

There is this one table which contains the amounts and states that I need. However, this table contains a year information but I want month. For example, in the table it shows information for Kentucky for 2011..and thats it. For California it shows about 5 different years. But I need it to repeat by month.
So if in 2011 Kentucky had 12 total, then I need a query that shows 12 for January, February, May....repeatedly
Right now I get this output with a dumb query I have:
Kentucky 12 January
California 800 January
This is done easily by grouping by State, Quantity and Month
I want to make sure that no matter what the Quantity is, each State has ALL months
Kentucky 12 January
Kentucky 12 February
Kentucky 12 May
California 800 January
California 800 February
California 800 May
Any idea on how to do this with Teradata SQL?
The overall query would look something like this:
SELECT
state_quantities.state,
state_quantities.quantity,
all_months.month_name
FROM state_quantities
CROSS JOIN (
...
) all_months
What goes between the brackets for all_months depends on what you mean by "all months".
If you mean all months that appear in state_quantities irrespective of state (so if you have Kentucky with January, California with February and Florida with May, you'd only get those three months) you could use something like this:
SELECT
month_name
FROM state_quantities
GROUP BY month_name
If you want all 12 months, you would join to a table containing all 12 months. In the absence of that, you could use sys_calendar.calendar (syntax below might be off):
SELECT
CAST(calendar_date AS DATE FORMAT 'MMM') AS month_name
FROM sys_calendar.calendar
GROUP BY month_name