Solving Gaps and Islands in MDX - ssas

We are replacing an old system with SSAS in SQL Server 2016. An existing report that needs to be replaced in the new system looks like this:
From Date To Date Days Cash Settled Rate (%) Interest
2017-01-01 2017-01-03 3 1000.00 1500.00 3.13 0.39
2017-01-04 2017-01-04 1 1100.00 1500.00 3.13 0.13
2017-01-05 2017-01-05 1 1100.00 1600.00 3.33 0.15
The underlying data would be this:
Date Cash Settled Rate (%) Interest
2017-01-01 1000.00 1500.00 3.13 0.13
2017-01-02 1000.00 1500.00 3.13 0.13
2017-01-03 1000.00 1500.00 3.13 0.13
2017-01-04 1100.00 1500.00 3.13 0.13
2017-01-05 1100.00 1600.00 3.33 0.15
The report basically groups all rows where Cash, Settled, and Rate are the same. I found a similar issue in this question but it is for SQL. Right now I use that for the report but would like to use MDX for better aggregation since the daily values consist of many detail rows.
In a twist to the above it is also possible that Settled, Rate, or all three (plus the derived Interest) are NULL.
I believe this issue is referred to as the Gaps and Islands problem. Has anyone any ideas on how to solve it in MDX?

Related

Transpose a table with multiple ID rows and different assessment dates

I would like to transpose my table to see trends in a data. The data is formatted as such:
UserId is can occur multiple times because of different assessment periods. Let's say a user with ID 1 inccured some charges in January, February, and March. There are currently three rows that contain data from these periods respectively.
I would like to see everything as one row - independently of the number of periods (up to 12 months), for each user ID.
This would enable me to see and compare changes between assessment periods and attributes.
Current format:
UserId AssessmentDate Attribute1 Attribute2 Attribute3
1 2020-01-01 00:00:00.000 -01:00 20.13 123.11 405.00
1 2021-02-01 00:00:00.000 -01:00 1.03 78.93 11.34
1 2021-03-01 00:00:00.000 -01:00 15.03 310.10 23.15
2 2021-02-01 00:00:00.000 -01:00 14.31 41.30 63.20
2 2021-03-01 00:03:45.000 -01:00 0.05 3.50 1.30
Desired format:
UserId LastAssessmentDate Attribute1_M-2 Attribute2_M-1 ... Attribute3_M0
1 2021-03-01 00:00:00.000 -01:00 20.13 123.11 23.15
2 2021-03-01 00:03:45.000 -01:00 NULL 41.30 1.30
Either SQL or Pandas - both work for me. Thanks for the help!

Comparing one date with starting date and ending date with duplicates

i have the below sample of data, and i need to create a function that will take a sales date and compare it with the below dates and returns the discount name & percentage, but as below the discount dates are not unique and some times overlaps, so in case the date falls in two different discounts names it has to return the highest duplicate discount name based on the percentage in case the sale date falls in more than one.
Discount Name Start Date End Date Percentage
0 First 2020-07-24 2020-11-25 0.10
1 First 2020-09-13 2020-10-29 0.10
2 First 2020-12-07 2020-12-10 0.10
3 First 2020-12-28 2021-01-19 0.10
4 First 2020-06-14 2020-06-14 0.10
5 Second 2020-06-16 2020-06-18 0.15
6 Second 2020-06-21 2020-06-22 0.15
7 Second 2020-06-22 2020-06-23 0.15
8 Second 2020-07-07 2020-07-08 0.15
9 Third 2020-06-02 2020-06-12 0.20
10 Third 2020-05-19 2020-06-01 0.20
11 Third 2020-05-06 2020-05-17 0.20
12 Third 2020-04-30 2020-05-03 0.20
Screen Shot of Dataframe
i truly hope that someone can help me on this. thanks
This function should do the trick
def discout_rate(df, sales_date):
return df[(df['Start Date'] <= sales_date) & (df['End Date'] >= sales_date)]['Percentage'].max()
The sales_date should be of type datetime.datetime and the columns Start Date and End Date too.

Count column SSRS

I am looking to create a second column which counts all the columns with a number >= 2.
Total
2.03
2.00
2.00
1.38
1.33
1.32
1.21
Expected result
Total Count column
2.03 3
2.00
2.00
1.38
1.33
1.32
1.21
I have looked at SUMIF and COUNTIF
=Sum(IIf(Fields!Total.Value >= "2" , 1, 0)
But i just get the SSRS cannot create report error.
Can anyone help? thanks

VBA calculate individual hours, from list of multiple employees [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I tried to avoid using VBA for this due to my lack of familiarity, but it looks like this is complex enough to require the Excel native language.
What I have is a CSV with a list of employees hours worked each day, all in separate rows. Employee name, Date, Regular Hours, and OT Hours are in separate columns. The challenge is that for employee Bob, there may be 20 rows for one day. Bob applies a slice of time to different projects all day. Then of course there are multiple days and multiple employees. What I am trying to end up with is a report that shows all of the regular and OT hours (separately) for each employee on a daily basis.
What I can't wrap my head around is how to start the compilation. I am guessing that separating each employee would be the start. Then separating each date, then adding all hours for that date.
I appreciate any assistance.
Emp# Name Date Reg OT
Emp1 Bob 1/1/2016 8.00 0.00
Emp1 Bob 1/4/2016 3.00 0.00
Emp1 Bob 1/4/2016 5.00 0.00
Emp1 Bob 1/5/2016 2.00 0.00
Emp1 Bob 1/5/2016 1.00 0.00
Emp1 Bob 1/5/2016 5.00 0.00
Emp1 Bob 1/6/2016 1.00 0.00
Emp1 Bob 1/6/2016 2.00 0.00
Emp1 Bob 1/6/2016 5.00 0.00
Emp1 Bob 1/7/2016 2.00 0.00
Emp2 Henry 1/1/2016 8.00 0.00
Emp2 Henry 1/4/2016 8.00 0.00
Emp2 Henry 1/5/2016 8.00 0.00
Emp2 Henry 1/6/2016 2.00 0.00
Emp2 Henry 1/6/2016 6.00 0.00
Emp2 Henry 1/7/2016 1.50 0.00
Emp2 Henry 1/7/2016 0.50 0.00
Emp2 Henry 1/7/2016 6.00 0.00
Emp2 Henry 1/8/2016 8.00 0.00
Emp2 Henry 1/11/2016 8.00 0.00
Emp2 Henry 1/12/2016 3.00 0.00
Emp2 Henry 1/12/2016 1.00 0.00
Emp2 Henry 1/12/2016 3.00 0.00
Emp2 Henry 1/12/2016 1.00 0.00
Emp2 Henry 1/13/2016 1.50 0.00
Your description sounds like you want to use a pivot table. They are easy to build - this example literally took me 5 minutes to build, including typing in the data.
As an illustration, you can take data that looks like this ...
and consolidate it in a way that provides a lot of flexibility in looking at it. Such as this:
or this
There are several good, simple tutorials available on building Pivot Tables. A google search turns up plenty.

Simple SQL math operation gives incorrect results

I am running into an issue with a simple SQL math operation of qty * price is returning an incorrect value.
This is SQL server 2005. Compatibility is set to 80 for 2000 SQL server.
Any help on understanding why I am having the problem shown below
For example:
Transaction Table:
id price qty
1 2.77 20.00
1 2.77 25.00
1 2.77 10.00
2 0.10 50.00
2 0.10 80.00
3 0.10 50.00
3 0.10 60.00
SQL
Select id, price, qty, (qty * price) from transact
The actual problem was this and it was my fault :(
Select id, CAST(price AS DECIMAL(5,2)), qty, (qty * price) from transact
Returns the following:
id price qty Total
1 2.77 20.00 55.400000 Correct
1 2.77 25.00 69.250000 Correct
1 2.77 10.00 27.700000 Correct
2 0.10 50.00 4.800000 Should be 5.0000
2 0.10 80.00 7.680000 Should be 8.0000
2 0.10 50.00 5.050000 Should be 5.0000
2 0.10 60.00 6.060000 Should be 6.0000
3 39.00 1.00 39.000000 Correct
3 39.00 2.00 78.000000 Correct
3 39.00 3.00 117.000000 Correct
You price is being rounded somewhere. The select you are running is not showing the actual price.
select round(0.096, 2) price, 0.096 * 50.00 total
Result:
price total
0.10 4.80000