Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Be 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

Reply
azeezj
Frequent Visitor

Retention percentage calculation in Dax

Hello,

 

I have a contracts table that looks like the below:

CustomerIDContractNumberStartDateSettleDate
1342018-02-232023-02-10
1892023-02-06NULL
2122023-01-312023-06-15
2562020-09-212022-11-22
2762021-05-132023-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

 

1 ACCEPTED 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.

Untitled.png


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

View solution in original post

8 REPLIES 8
Ashish_Mathur
Super User
Super User

Hi,

Based on the data that you have shown, show the expected result.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

I am expecting the result to be like the below:

 

CustomerIDContractNumberStartDateSettleDateRetention Ind
1342018-02-232023-02-10No
1892023-02-06nullYes

2

122020-09-212022-11-22No

2

562021-05-132023-02-01No

2

762023-01-312023-06-15Yes

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.

Untitled.png


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

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".

lbendlin
Super User
Super User

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.

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

Dec Fabric Community Survey

We want your feedback!

Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.