Create SQL flag for entries based on changing value across dates - sql

I have a table with application data where some applications turned into loans of either A or B type. My goal is, for each new application, to pull previous entries from the same Primary ID that turned into a loan and flag whether that ID has had either A, B, or both loan types. I'm struggling most with the time sensitivity element since I would have to pull loans from a subquery based on Funded Date referencing the Created Date from each row of the original table (or at least, that's the route I've focused on so far).
Here's what the original table looks like for one ID:
Primary ID
Application ID
Created Date
Funded Date
Loan Type
1
1
1/1/19
1/5/19
A
1
2
2/3/19
N/A
N/A
1
3
2/10/19
2/15/19
B
1
4
2/25/19
N/A
N/A
Essentially I'd want to add flags to each row that conveys this info:
Primary ID
Application ID
Created Date
Funded Date
Loan Type
Loan History
1
1
1/1/19
1/5/19
A
N/A
1
2
2/3/19
N/A
N/A
A
1
3
2/10/19
2/15/19
B
A
1
4
2/25/19
N/A
N/A
A, B
Thanks in advance

Related

In UniQuery, how do you get the count of unique values found while doing a BREAK.ON

I know I can get the counts for how many individual entries are in each unique groups of records with the following.
LIST CUSTOMER BREAK-ON CITY TOTAL EVAL "1" COL.HDG "Customer Count" TOTAL CUR_BALANCE BY CITY
And I end up with something like this.
Cust...... City...... Customer Count Currently Owes
6 Arvada 1 4.54
********** -------------- --------------
Arvada 1 4.54
190 Boulder 1 0.00
1 Boulder 1 13.65
********** -------------- --------------
Boulder 2 13.65
...
============== ==============
TOTAL 29 85.28
29 records listed
Which becomes this, after we suppress the details and focus on the groups themselves.
City...... Customer Count Currently Owes
Arvada 1 4.54
Boulder 2 13.65
Chicago 3 4.50
Denver 6 0.00
...
============== ==============
TOTAL 29 85.28
29 records listed
But can I get a count of how many unique grouping are in the same report? Something like this.
City...... Customer Count Currently Owes City Count
Arvada 1 4.54 1
Boulder 2 13.65 1
Chicago 3 4.50 1
Denver 6 0.00 1
...
============== ============== ==========
TOTAL 29 85.28 17
29 records listed
Essentially, I want the unique value count integrated into the other report so that I don't have to create an extra report just for something so simple.
SELECT CUSTOMER SAVING UNIQUE CITY
17 records selected to list 0.
I swear that this should be easier. I see various # variables in the documentation that hint at the possibility of doing this easily but I have never been about to get one of them to work.
If your data is structured in such a way that your id is what you would be grouping by and the data you want is stored in Value delimited field and you don't want to include or exclude anything you can use something like the following.
In UniVerse using the CUSTOMER table in the demo HS.SALES account installed on many systems, you can do this. The CUSTID is the the record #ID and Attribute 13 is where there PRICE is stored in a Value delimited array.
LIST CUSTOMER BREAK-ON CUSTID TOTAL EVAL "DCOUNT(#RECORD<13>,#VM)" TOTAL PRICE AS P.PRICE BY CUSTID DET.SUP
Which outputs this.
DCOUNT(#RECORD<13>,#
Customer ID VM)................. P.PRICE
1 1 $4,200
2 3 $19,500
3 1 $4,250
4 1 $16,500
5 2 $3,800
6 0 $0
7 2 $5,480
8 2 $12,900
9 0 $0
10 3 $10,390
11 0 $0
12 0 $0
==================== =======
15 $77,020
That is a little juice for a lot of squeeze, but I hope you find it useful.
Good Luck!
Since the system variable #NB is set only on the total lines, this will allow your counter to calculate the number of TOTAL lines, which occur per unique city, excluding the grand total.
LIST CUSTOMER BREAK-ON CITY TOTAL EVAL "IF #NB < 127 THEN 1 ELSE 0" COL.HDG "Customer Count" TOTAL CUR_BALANCE BY CITY
I don't have a system to try this on, but this is my understanding of the variable.

Need to Slice data based on Count of Boolean Flags (Same Dimension, No Hierarchy)

I need to slice the data based on Multiple Flags on the Transactional record. The Flags are on the same dimension but not in the same hierarchy.
Below is the sample transnational data and corresponding fact table that we have generated. I need to write MDX query in order to generate report as whoen in the Expected Report below.
Here PrintMail, App Notifications, SMS and Email are various types of communication channels through with the message was delivered. Some of the messages are delivered through multiple channels.
Transactional Table
Date MessageCount PrintMail AppNotification SMS Email
1/1/2019 1 1 1
1/1/2019 1 1 1
1/1/2019 1 1 1
2/1/2019 1 1 1
2/1/2019 1 1
Fact Table
Date MessageCount PrintMail AppNotification SMS Email
1/1/2019 3 2 2 1 1
2/1/2019 2 1 2 0 0
Expected Report
All 1/1/2019 2/1/2019
Message Count 5 3 2
PrintMail 3 2 1
AppNotification 2 1 1
SMS 3 2 1
Email 1 1 0
My question is: how can I show various channels are rows here? I am not sure if this will require a new dimension and if yes, how would that look like.
By the looks of your Fact table the diffrent channels are your facts. If that is the case then you dont need a new measure. All you need to do is in your MDX query put the measures on row axis and the date on column axis. The Query should look like
Select {[DimDate].[Date].members} on columns,
{[Measures].[MessageCount],[Measures].[PrintMail],
[Measures].[AppNotification],[Measures].[SMS],[Measures].[Email]}
on rows
from [YourCube]

Table with arbitrary number of columns for rows

I want to store the amount of the groceries that I have at home and show me dishes, that I can cook based on that.
I want to be precise as possible, that's why I want to differentiate e.g. between frozen and fresh berries or low fat and normal milk. But I've problems modeling this. I have the following tables:
Products Type Amounts
id | name id | name id | Products.id | Type.id | amount
---|---------- ---|------ ---|-------------|---------|-------
1 | milk 1 | frozen 1 | 1 | 2 | 1l
2 | strawberry 2 | low fat 2 | 1 | 3 | 0.5l
3 | organic 3 | 2 | 1 | 500g
4 | fresh 4 | 2 | 4 | 250g
So far I've no problem, but how would I store a product that has two or more types (e.g. low fat, organic milk)?
Things I could do:
create organic milk and low fat organic milk as separate products and drop the Type table
Remove the Type.id foreign key and put all types for a product as JSON or as CSV in a new types column of Amounts
Limit to n types per product and add n Type{1..n}.id columns to Amounts - set a column to NULL if product has less than n types
But before I do this, I would love to know if there are better solutions.
Distinguish between a "product", which is generic, and an "item" which is a specific instance of the product in the inventory.
Then "tag" the items with properties. This is essentially an entity-attribute-value relationship. Rows might be:
item product id tag
A 1 [milk] organic
A 1 low-fat
B 1 organic
The idea is to separate the notion of the "product", which is generic, and "item" which is a specific instance that might have additional tags.
Then an "amounts" table:
item Amount Unit
A 1 liter
The tags could of course be in another table, to ensure consistency within and across products. For instance, you might ask: What can I make with the organic items (products) that I have on hand?
Product <-> Type is a many to many relationship. Every product can have many types and there can be many products that are of a certain type. To model this in a relational database, you need a so called mapping table. That table would have two columns: product_id and type_id. Then you insert a row for every relation between a product and a type.
You can find an example in this post.

SSAS Attribute relationship behavior in Hierarchy

I am using SQL server 2012 with Visual Studio 2015.
(While I am an excellent SSRS developer I am only just getting into SSAS)
I basically want to use the hierarchy as it neatens up the Excel Front end more than anything. At the same time though I want the attributes to behave in the same manner, when in the browser on their own, as they would if they are not in the hierarchy.
I have a customer dimension, for example, where the PK is an int called ID with a customer ID and Delivery Postcode
The attribute relation ship is set to;
ID to Customer ID
ID to Delivery Post code
The full hierarchy displays like this;
CustomerID Delivery PostCode Order Quantity
1 FY5 3TG 1
2 CH2 1NF 1
3 SK22 4DT 1
4 L20 6LT 1
5 FY6 8BX 1
6 WA8 7XP 1
7 L18 3JN 1
8 L12 3HB 1
9 M28 0SX 1
10 BB7 9AZ 1
10 ZZ99 9ZZ 1
9 ZZ99 9ZZ 1
If you just pull the delivery post code, from the hierarchy, through it returns;
Delivery PostCode Order Quantity
FY5 3TG 1
CH2 1NF 1
SK22 4DT 1
L20 6LT 1
FY6 8BX 1
WA8 7XP 1
L18 3JN 1
L12 3HB 1
M28 0SX 1
BB7 9AZ 1
ZZ99 9ZZ 1
ZZ99 9ZZ 1
I would expect the post codes at the bottom to roll up, eg postcode ZZ99 9ZZ, (like it would in a normal excel pivot for example), like the below. It is also worth noting that I have left the attributes visible outside the hierarchy. If use that delivery Postcode attribute it also returns the below;
Delivery PostCode Order Quantity
FY5 3TG 1
CH2 1NF 1
SK22 4DT 1
L20 6LT 1
FY6 8BX 1
WA8 7XP 1
L18 3JN 1
L12 3HB 1
M28 0SX 1
BB7 9AZ 1
ZZ99 9ZZ 2
I don't understand why the same attribute is acting differently inside and outside the hierarchy. My only conclusion, which is probably wrong(!), is that the hierarchy itself is forcing the Delivery Postcode attribute to act differently.
Any help would be great.
I basically want to use the hierarchy as it neatens up the Excel Front end more than anything. At the same time though I want the attributes to behave in the same manner, when in the browser on their own, as they would if they are not in the hierarchy.

PowerPivot DAX - Dynamic Ranking Per Group (Min Per Group)

I am searching for a method to utilize within Microsoft PowerPivot 2010 that will allow me to perform dynamic ranking that will automatically update the associated rank value based on filters and slicer values that are applied.
Thusfar, all examples I have seen utilize the Calculate() DAX function that overrides existing filters within the PowerPivot table via the All() function which causes predefined filters that users may apply to be disregarded.
To illustrate my requirements, please reference the example below:
(Source Data within PowerPivot Window:)
-------------------------------------------------------------------------------------
Claim Number | Claimant Number | Transaction Date | Dollar Amount
------------------------------------------------------------------------------------
ABCD123456 4 1/1/2012 $145.23
ABCD123456 4 8/1/2012 $205.12
ABCD123456 4 9/1/2012 $390.74
VDSF123455 2 3/5/2012 $10.12
VDSF123455 2 9/5/2012 $50.12
VDSF123455 2 12/9/2012 $210.45
------------------------------------------------------------------------------------------
Now, I would like to have the capability of ONLY displaying the dollar amount for each claim number and claimant number based on the FIRST transaction date. I would essentially desire to only show dollars tied to the first transaction tied to each claim# and claimant number group.
My thought process was to create a dynamic rank for each [claim number + claimant number] group similiar to the "partition by - Row_Number()" function. In this way, each [claim number + claimant number] group containing a rank value of 1 would represent the FIRST transaction tied to this value.
However, I would need this rank value to change if a user applies a filter against the data within the resulting PivotTable.
This can be illustrated below based on the sample data defined above:
----------------------------------------------------------------------------------------------
Claim Number | Claimant Number | Transaction Date | Dollar Amount | Rank |
---------------------------------------------------------------------------------------------
ABCD123456 4 1/1/2012 $145.23 1
ABCD123456 4 8/1/2012 $205.12 2
ABCD123456 4 9/1/2012 $390.74 3
VDSF123455 2 3/5/2012 $10.12 1
VDSF123455 2 9/5/2012 $50.12 2
VDSF123455 2 12/9/2012 $210.45 3
------------------------------------------------------------------------------------------------
Now, if a user applies a filter via a slicer or via the filter dropdown within a PivotTable or within the PowerPivot table itself excluding valuation dates < 2/1/2012, the rank value should automatically update itself and be reflected as shown below:
(Filters applied)
=============================================================================
Claim Number | Claimant Number | Transaction Date | Dollar Amount | Rank |
============================================================================
ABCD123456 4 8/1/2012 $205.12 1
ABCD123456 4 9/1/2012 $390.74 2
VDSF123455 2 3/5/2012 $10.12 1
VDSF123455 2 9/5/2012 $50.12 2
VDSF123455 2 12/9/2012 $210.45 3
===========================================================================
As you can see, the rank value was automatically updated for the first claim group based on one of the rows tied to claim number ABCD123456 being hidden, based on the user applied filter.
I would then like to create an additional measure within the PowerPivot table that would display the dollar amount ONLY tied to the transaction containing a rank value of 1 as shown below:
(Filters applied, New Measure Added)
===============================================================================================
Claim Number | Claimant Number | Transaction Date | Dollar Amount | Rank | Opening Dollar Amt|
================================================================================================
ABCD123456 4 8/1/2012 $205.12 1 $205.12
ABCD123456 4 9/1/2012 $390.74 2
VDSF123455 2 3/5/2012 $10.12 1 $50.12
VDSF123455 2 9/5/2012 $50.12 2
VDSF123455 2 12/9/2012 $210.45 3
===============================================================================================
A corresponding Pivot Table could then be created referencing this data so that the "Opening Dollar Amt" measure would only represent dollars tied to the FIRST transaction per group as outlined above.
I would greatly appreciate if someone can outline how this could be accomplished.
I thought the dynamic ranking approach might be a good idea for this, but if anyone has a better idea to achieve my end result/goal which is to simply obtain the dollar amount tied to the first transaction "VISIBLE" (based on any user applied filters) PER GROUP I would be open ears to whatever approach you may have.
Although this is a well written question that you've obviously put time into formulating, you should read this about cross posting in forums. This is a clear duplication of something you've posted on MSDN at exactly the same time. I've answered in both seeing as its a decent question.
Firstly I created a basic measure [Amount] to sum the dollar amount column. I then used that within RANKX() to create the following:
[Rank] = RANKX(
FILTER(
ALLSELECTED(Table1),Table1[Claimant Number]=max(Table1[Claimant Number])
),
[Amount],
,1)
The key is the table that the [Amount] measure is iterated over - ALLSELECTED() just brings the stuff in the current filter context into play and the expression within the FILTER() restricts the table to the current claim number.
After that it was a simple task to return the [Amount] based on whether or not the [Rank] was 1:
[Opening Balance] = if([Rank]=1,[Amount],BLANK())
Hope this makes sense, I posted my workings on SkyDrive if they help.
Jacob