March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Early bird discount ends December 31.
Register NowBe one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now
Hello,
I have a contracts table that looks like the below:
CustomerID | ContractNumber | StartDate | SettleDate |
1 | 34 | 2018-02-23 | 2023-02-10 |
1 | 89 | 2023-02-06 | NULL |
2 | 12 | 2023-01-31 | 2023-06-15 |
2 | 56 | 2020-09-21 | 2022-11-22 |
2 | 76 | 2021-05-13 | 2023-02-01 |
I want to determine retention using the below rule:
If the customer starts a new contract 6 months before their previous contract is settled, or 3 months after then they are Retained. I have tried the below calculated column but it does not work accurately:
CSC Rentention Ind =
VAR CurrentContractStartDate = 'New Contracts'[StartDate]
VAR CurrentCustomerID = 'New Contracts'[CustomerID]
VAR SixMonthsBeforeEndDate =
CALCULATE (
MIN ( 'New Contracts'[SettleDate] ),
FILTER (
ALL ( 'New Contracts' ),
'New Contracts'[CustomerID] = CurrentCustomerID
)
) - 6 * 30
VAR ThreeMonthsAfterEndDate =
CALCULATE (
MIN ( 'New Contracts'[SettleDate] ),
FILTER (
ALL ( 'New Contracts' ),
'New Contracts'[CustomerID] = CurrentCustomerID
)
) + 3 * 30
RETURN
IF (
CurrentContractStartDate >= SixMonthsBeforeEndDate
&& CurrentContractStartDate <= ThreeMonthsAfterEndDate,
"Yes",
"No"
)
TIA
Solved! Go to Solution.
Hi,
Try these calculated column formulas
Previous settlement date = CALCULATE(MAX(Data[SettleDate]),FILTER(Data,Data[CustomerID]=EARLIER(Data[CustomerID])&&Data[StartDate]<EARLIER(Data[StartDate])))
Test = or(and(Data[StartDate]>=EDATE([Previous settlement date],-6),Data[StartDate]<=[Previous settlement date]),and(Data[StartDate]<=EDATE(Data[Previous settlement date],3),Data[StartDate]>=Data[Previous settlement date]))
Hope this helps.
Hi,
Based on the data that you have shown, show the expected result.
I am expecting the result to be like the below:
CustomerID | ContractNumber | StartDate | SettleDate | Retention Ind |
1 | 34 | 2018-02-23 | 2023-02-10 | No |
1 | 89 | 2023-02-06 | null | Yes |
2 | 12 | 2020-09-21 | 2022-11-22 | No |
2 | 56 | 2021-05-13 | 2023-02-01 | No |
2 | 76 | 2023-01-31 | 2023-06-15 | Yes |
Hi,
Try these calculated column formulas
Previous settlement date = CALCULATE(MAX(Data[SettleDate]),FILTER(Data,Data[CustomerID]=EARLIER(Data[CustomerID])&&Data[StartDate]<EARLIER(Data[StartDate])))
Test = or(and(Data[StartDate]>=EDATE([Previous settlement date],-6),Data[StartDate]<=[Previous settlement date]),and(Data[StartDate]<=EDATE(Data[Previous settlement date],3),Data[StartDate]>=Data[Previous settlement date]))
Hope this helps.
what's your primary key? CustomerID plus ContractNumber ?
Correct. But the retention is calculated per CustomerID
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("TY3BDcAgDAN3yRtLsSkUZkHsv0ZTkBCfKGdflDGMliw/MeRscEF5gfIPdJtpS61fudeAVSkWalcO7xA3xDEhHanUnRNewPuJ80hvPTmReaCCxeb8AA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [CustomerID = _t, ContractNumber = _t, StartDate = _t, SettleDate = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"StartDate", type date}, {"SettleDate", type date}}),
#"Added Custom" = Table.AddColumn(#"Changed Type", "PrevSettle", each List.Max(Table.SelectRows(#"Changed Type",(k)=>k[CustomerID]=[CustomerID] and k[ContractNumber]<[ContractNumber])[SettleDate])),
#"Added Custom1" = Table.AddColumn(#"Added Custom", "Retention", each [StartDate]>=Date.AddMonths([PrevSettle],-6) and [StartDate]<=Date.AddMonths([PrevSettle],3))
in
#"Added Custom1"
How to use this code: Create a new Blank Query. Click on "Advanced Editor". Replace the code in the window with the code provided here. Click "Done".
If the customer starts a new contract 6 months before their previous contract is settled, or 3 months after then they are Retained.
The "6 months before" part should not be needed. What if they renewed 7 months before the expiry?
Your question is missing the "when?" component. Do you want to show the retention status as of today? By month?
Hi. 7 months before is not considered as retention - strictly 6 months before and 3 months after. I want to show status by month.
March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!
Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.
Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.
User | Count |
---|---|
122 | |
89 | |
78 | |
67 | |
52 |
User | Count |
---|---|
199 | |
140 | |
96 | |
77 | |
68 |