Forum Discussion
Compare customer change between two dates from account table
- 3 years ago
Hi , ChrisCBR
For the column , we need the static fields. Due to we can not put the measure on the headers . So we need to create two dimention table.
Here are the steps you can refer to :
(1)We need to click "New Table" to create two tables:
Row = VALUES('Table'[RatingGrade])Column = VALUES('Table'[RatingGrade])(2)Then we need to create two measures:
Expouser 1-1 = var _row =SELECTEDVALUE('Row'[RatingGrade]) var _column = SELECTEDVALUE('Column'[RatingGrade]) var _date1 = SELECTEDVALUE('Date1'[Date1]) var _date2 = SELECTEDVALUE('Date2'[Date2]) var _row_t = FILTER( 'Table' , 'Table'[Date] =_date1 && 'Table'[Defaulted]=1) var _column_t = FILTER('Table','Table'[Date] =_date2 && 'Table'[Defaulted]=0) var _row_customerNumber = MAXX(FILTER(_row_t , [RatingGrade] = _row),[CustomerNumber]) var _column_customerNumber = MAXX(FILTER(_column_t , [RatingGrade] = _column),[CustomerNumber]) var _in_customnumber =INTERSECT( SELECTCOLUMNS(_row_t , "CustomerNumber",[CustomerNumber]) , SELECTCOLUMNS(_column_t , "CustomerNumber",[CustomerNumber])) var _row_value =DISTINCT(SELECTCOLUMNS( FILTER(_row_t , [CustomerNumber] in _in_customnumber ) , "RatingGrade",[RatingGrade])) var _column_value = DISTINCT(SELECTCOLUMNS( FILTER(_column_t , [CustomerNumber] in _in_customnumber ) , "RatingGrade",[RatingGrade])) var _value =SUMX( FILTER( _row_t,[RatingGrade] = _row) , [Exposure]) return IF(_row in _row_value && _column in _column_value && _row_customerNumber=_column_customerNumber, _value , BLANK())Expouser 1-2 = var _row =SELECTEDVALUE('Row'[RatingGrade]) var _column = SELECTEDVALUE('Column'[RatingGrade]) var _date1 = SELECTEDVALUE('Date1'[Date1]) var _date2 = SELECTEDVALUE('Date2'[Date2]) var _row_t = FILTER( 'Table' , 'Table'[Date] =_date1 && 'Table'[Defaulted]=1) var _column_t = FILTER('Table','Table'[Date] =_date2 && 'Table'[Defaulted]=0) var _row_customerNumber = MAXX(FILTER(_row_t , [RatingGrade] = _row),[CustomerNumber]) var _column_customerNumber = MAXX(FILTER(_column_t , [RatingGrade] = _column),[CustomerNumber]) var _in_customnumber =INTERSECT( SELECTCOLUMNS(_row_t , "CustomerNumber",[CustomerNumber]) , SELECTCOLUMNS(_column_t , "CustomerNumber",[CustomerNumber])) var _row_value =DISTINCT(SELECTCOLUMNS( FILTER(_row_t , [CustomerNumber] in _in_customnumber ) , "RatingGrade",[RatingGrade])) var _column_value = DISTINCT(SELECTCOLUMNS( FILTER(_column_t , [CustomerNumber] in _in_customnumber ) , "RatingGrade",[RatingGrade])) var _value =SUMX( FILTER( _column_t,[RatingGrade] = _column) , [Exposure]) return IF(_row in _row_value && _column in _column_value && _row_customerNumber=_column_customerNumber, _value , BLANK())(3)Then we put the fields we need on the visual and we will meet your need:
Thank you for your time and sharing, and thank you for your support and understanding of PowerBI!
Best Regards,
Aniya Zhang
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly
Hi , ChrisCBR
Accoding to your table you provides , i have some questions about it and i can noy understand.
(1)"AccountProduct and the exposure come from the individual accounts, Defaulted and RatingGrade info from the customer.", the table you provide means that it come from three tables?
(2)"When date1 (the date for default status = 1) is 30.09.2022 and date2 (the date for default status = 0) is 30.06.2022, the sum of the exposure @date1 (newly in default) would be 9.400.000, exposure from @date2 would be 9.500.000 (coming from customer 3 that newly defaulted between @ date2 and @date1."
According to your description, i cannot calcualte the 9.400.000 and 9.500.000 by your provided data.
If this , can you provide all the test table and with data to us and the relationship between them , then put the end result and the calculation logic so that we can help you better.
Thank you for your time and sharing, and thank you for your support and understanding of PowerBI!
Best Regards,
Aniya Zhang
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly
Hello!
Regarding your questions:
1) I have an account table with the information which customers the accounta are linked to. The information whether a customer defaulted (=went bankrupt) and the customer rating originally came from a customer table, the information is added to the account table through the customer number, and is therefore always the same for each customer row per date. This was done in data preparation way before I load the data into Power BI.
2) The new default between 30.06.2022 and 30.09.2022 is customer 3 (default status =1 at 30.09.2022 and 0 at 30.06.2022).
The accounts that are linked to customer 3 at 30.09.2022 are AccountNr 4,5 and 7 which have an overall exposure of 9.400.000. At 30.06.2022, the accounts 4 and 5 were linked to customer 3 with an overall exposure of 9.500.000.
Customers 1 and 2 are not in default at 30.09.2022. Customer 4 was already in default by 30.06.2022 and is therefore not a new default between 30.06.2022 and 30.09.2022.
I have no other data in my model except for this account table (there are some other tables with other information, but those have nothing to do with the account table). The only other two tables that are linked to the account table are the two date tables with its inactive relationships that I've mentioned in my original post to calculate the new defaults as a customer list.
SQL queries on the example data for these four scenarios would look like the following if that helps:
--1) default between 30.09.2022 and 30.06.2022
SELECT sum(Exposure1) Exposure1
,sum(menge.Exposure2) Exposure2
from (
SELECT a.*,b.Exposure2 FROM (
SELECT CustomerNumber
,Defaulted
,sum(Exposure) Exposure1
from exampledata
where Defaulted=1
and Date='20220930'
GROUP BY CustomerNumber
,Defaulted
)a
INNER JOIN (
SELECT CustomerNumber
,Defaulted
,sum(Exposure) Exposure2
from exampledata
where Defaulted=0
and Date='20220630'
GROUP BY CustomerNumber
,Defaulted
)b on b.CustomerNumber = a.CustomerNumber
)menge
--2) default between 30.09.2022 and 31.03.2022
SELECT sum(Exposure1) Exposure1
,sum(menge.Exposure2) Exposure2
from (
SELECT a.*,b.Exposure2 FROM (
SELECT CustomerNumber
,Defaulted
,sum(Exposure) Exposure1
from exampledata
where Defaulted=1
and Date='20220930'
GROUP BY CustomerNumber
,Defaulted
)a
INNER JOIN (
SELECT CustomerNumber
,Defaulted
,sum(Exposure) Exposure2
from exampledata
where Defaulted=0
and Date='20220331'
GROUP BY CustomerNumber
,Defaulted
)b on b.CustomerNumber = a.CustomerNumber
)menge
--3) default between 30.09.2022 and 31.03.2022 - Loans
SELECT sum(Exposure1) Exposure1
,sum(menge.Exposure2) Exposure2
from (
SELECT a.*,b.Exposure2 FROM (
SELECT CustomerNumber
,Defaulted
,sum(Exposure) Exposure1
from exampledata
where Defaulted=1
and Date='20220930'
and AccountProduct='Loan'
GROUP BY CustomerNumber
,Defaulted
)a
INNER JOIN (
SELECT CustomerNumber
,Defaulted
,sum(Exposure) Exposure2
from exampledata
where Defaulted=0
and Date='20220331'
and AccountProduct='Loan'
GROUP BY CustomerNumber
,Defaulted
)b on b.CustomerNumber = a.CustomerNumber
)menge
--4) cured customer between 31.03.2022 and 30.09.2022
SELECT sum(Exposure1) Exposure1
,sum(menge.Exposure2) Exposure2
from (
SELECT a.*,b.Exposure2 FROM (
SELECT CustomerNumber
,Defaulted
,sum(Exposure) Exposure1
from exampledata
where Defaulted=1
and Date='20220331'
GROUP BY CustomerNumber
,Defaulted
)a
INNER JOIN (
SELECT CustomerNumber
,Defaulted
,sum(Exposure) Exposure2
from exampledata
where Defaulted=0
and Date='20220930'
GROUP BY CustomerNumber
,Defaulted
)b on b.CustomerNumber = a.CustomerNumber
)menge
Thanks in advance for your time!
Chris
- v-yueyunzh-msft3 years ago
Community Support
Hi , ChrisCBR
Thank you for your sql code to me , now i can fully understand your requirement and here is my solution based on it:
(1)My test data is the same as you provided.
(2)We need to create two Date table to use as a slicer. We need to click "New Table" to create two tables:
Date1 = VALUES('Table'[Date])Date2 = VALUES('Table'[Date])(3)We need to create two measures to calculate your exposures:
Exposure1 = var _date1 = MAX('Date1'[Date1]) var _date2 = MAX('Date2'[Date2]) var _t1 = FILTER('Table' , 'Table'[Date] = _date1 && 'Table'[Defaulted] =1 ) var _t1_CustomerNumber = SELECTCOLUMNS(_t1,"CustomerNumber" , [CustomerNumber]) var _t2 = FILTER('Table' , 'Table'[Date]=_date2 && 'Table'[Defaulted]=0) var _t2_CutomerNumber = SELECTCOLUMNS(_t2 , "CustomerNumber",[CustomerNumber]) var _Innerjoin_number =DISTINCT( INTERSECT(_t1_CustomerNumber , _t2_CutomerNumber)) var _t_exposure1 =FILTER(_t1, [CustomerNumber] in _Innerjoin_number) return SUMX(_t_exposure1 , [Exposure])Exposure2 = var _date1 = MAX('Date1'[Date1]) var _date2 = MAX('Date2'[Date2]) var _t1 = FILTER('Table' , 'Table'[Date] = _date1 && 'Table'[Defaulted] =1 ) var _t1_CustomerNumber = SELECTCOLUMNS(_t1,"CustomerNumber" , [CustomerNumber]) var _t2 = FILTER('Table' , 'Table'[Date]=_date2 && 'Table'[Defaulted]=0) var _t2_CutomerNumber = SELECTCOLUMNS(_t2 , "CustomerNumber",[CustomerNumber]) var _Innerjoin_number =DISTINCT( INTERSECT(_t1_CustomerNumber , _t2_CutomerNumber)) var _t_exposure2 =FILTER(_t2, [CustomerNumber] in _Innerjoin_number) return SUMX(_t_exposure2 , [Exposure])(4)Then we can put the 'Date1' and 'Date2' on the two slicer and the two measures on the card visual and we will meet your need :
Thank you for your time and sharing, and thank you for your support and understanding of PowerBI!
Best Regards,
Aniya Zhang
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly
- ChrisCBR3 years ago
Helper I
Thank you very much!
This way all four use cases for my first problem are solved. I have one more problem with my data. I am still not sure how to deal with my categories.
Going back to my original post:
"
However, I also need this information in different visualisations, for instance I want to see the current exposure of all newly defaulted customers as a Card or visualize the new defaults by individual default classes. Without the Customer Nr in my visualisation, my filters do not work.
"
Is there a way to expand this example so I can use for example the rating grade from my example at date 1 on the x-axis (columns), the rating grade at date 2 on the y-axis (rows) and our newly calculated exposure 1 and exposure 2 as the values of a matrix?
Thank you again and best regards
Chris
- v-yueyunzh-msft3 years ago
Community Support
Hi , ChrisCBR
I do not fully understand the "visualize the new defaults by individual default classes.".
Can you tell me what the filed you want to put on the visual and the end result on the visual , such as give a end table you want based on the test data .
Thank you for your time and sharing, and thank you for your support and understanding of PowerBI!
Best Regards,
Aniya Zhang
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly