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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
URobert
Frequent Visitor

count monthly no of unique customers with valid contracts based on start and end date

Hi all,

 

I am new in power bi and I have a problem with the below issue :

I have a table with customer name, contract number, start date and and date of the contract.

In this table I could have more than one valid contract per customer, or consecutive validity contracts per customer, or some without a valid contract.

1. I want to be able to create a matrix view which is counting unique customers with active contracts on a monthly bases

2. Another matrix with lost customers on a monthly bases

2. Another matrix with gain customers on a monthly bases

 

Input table :

Customer nameContract novalid from new valid to (end of month)
A1/201801.04.2018 31.03.2019
A6/201901.04.2019 31.03.2020
A11/202001.04.2020 31.03.2021
B3/201701.02.2017 31.12.2018
B8/201901.04.2019 31.03.2020
B8/202001.04.2020 31.03.2021
C10/201601.11.2016 31.10.2018
C10/201801.01.2018 28.02.2019
C10/201901.11.2019 31.10.2020
D4/201601.01.2017 31.12.2018
D5/201801.01.2019 31.12.2019
D6/201801.01.2019 31.12.2019
D7/201901.01.2020 31.12.2020
EA1 /201501.04.2016 31.12.2017
EA2 /201501.03.2016 31.12.2017
EA7 /201501.03.2016 31.12.2017
EA4/201701.01.2018 31.12.2018
EA5/201701.01.2018 31.12.2018
EA6/201701.01.2018 31.12.2018
EA20/201801.01.2019 31.12.2020
EA21/201801.01.2019 31.12.2020
EA22/201801.01.2019 31.12.2020
EA30/201801.01.2019 31.12.2020
EA35/201801.01.2019 31.12.2020
EA39/201801.01.2019 31.12.2020

 

result :

 

1. matrix1 : no of customers with active contract (I've created a table only with 5 monts)

 20182019201920192019
matrix 1decjanfebmarchapril
A11111
B1   1
C111  
D11111
EA11111

 

2: no of customers lost

 20182019201920192019
matrix 2decjanfebmarchapril
A     
B 1   
C   1 
D     
EA     

 

3. no of new customers 

 

 20182019201920192019
matrix 3decjanfebmarchapril
A     
B    1
C     
D     
EA     

 

Thank you.

1 ACCEPTED SOLUTION

hi  @URobert 

Ok, I have the adjust all these formula:

no of new customers = 
var _premonthvalue=CALCULATE(DISTINCTCOUNT('Table'[Customer name]),FILTER('Table','Table'[valid from]<=MAXX(DATEADD('Date'[Date],-1,MONTH),'Date'[Date])&&'Table'[new valid to]>=MAXX(DATEADD('Date'[Date],-1,MONTH),'Date'[Date])))
return
IF(([no of customers with active contract]>0&&_premonthvalue=BLANK())||([no of customers with active contract]>_premonthvalue),1)
new customers = 
var _table=SUMMARIZE('Date','Date'[Year],'Date'[Month],"_value",[no of new customers]) return
SUMX(_table,[_value])

 

no of customers lost = 
var _premonthvalue=CALCULATE(DISTINCTCOUNT('Table'[Customer name]),FILTER('Table','Table'[valid from]<=MAXX(DATEADD('Date'[Date],-1,MONTH),'Date'[Date])&&'Table'[new valid to]>=MAXX(DATEADD('Date'[Date],-1,MONTH),'Date'[Date])))
return
IF(([no of customers with active contract]=BLANK()&&_premonthvalue>0)||([no of customers with active contract]<_premonthvalue),1)
lost customers = 
var _table=SUMMARIZE('Date','Date'[Year],'Date'[Month],"_value",[no of customers lost]) return
SUMX(_table,[_value])

 

and here is new sample pbix file, please try it.

 

 

Regards,

Lin

Community Support Team _ Lin
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

7 REPLIES 7
v-lili6-msft
Community Support
Community Support

hi @URobert 

Create three measure as below:

no of customers with active contract = CALCULATE(DISTINCTCOUNT('Table'[Customer name]),FILTER('Table','Table'[valid from]<=MAX('Date'[Date])&&'Table'[new valid to]>=MAX('Date'[Date])))
no of customers lost = 
var _premonthvalue=CALCULATE(DISTINCTCOUNT('Table'[Customer name]),FILTER('Table','Table'[valid from]<=MAXX(DATEADD('Date'[Date],-1,MONTH),'Date'[Date])&&'Table'[new valid to]>=MAXX(DATEADD('Date'[Date],-1,MONTH),'Date'[Date])))
return
IF([no of customers with active contract]=BLANK()&&_premonthvalue>0,_premonthvalue)
no of new customers = 
var _premonthvalue=CALCULATE(DISTINCTCOUNT('Table'[Customer name]),FILTER('Table','Table'[valid from]<=MAXX(DATEADD('Date'[Date],-1,MONTH),'Date'[Date])&&'Table'[new valid to]>=MAXX(DATEADD('Date'[Date],-1,MONTH),'Date'[Date])))
return
IF([no of customers with active contract]>0&&_premonthvalue=BLANK(),[no of customers with active contract])

 

and here is sample pbix file, please try it.

 

By the way, you'd better enable show Items with no data for date field in matrix

https://docs.microsoft.com/en-us/power-bi/create-reports/desktop-show-items-no-data

 

 

Regards,

Lin

Community Support Team _ Lin
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Hi @v-lili6-msft

 

Thank you for your answer.

I coudn't open your file, but I've used your formula from your answer.

 

The solutution for point 1 (count no of customers with active contracts) it is working great.

I have some issued with formulas for "no of customers lost" and "no of new customers" -> if I count them based on the customer name it is ok, but if I want to cumulate them on monthly bases, doesn't show me as totals

 
 

For no of customers lost I want to show as a total on monthly bases, sum of customers which had an active contract last month and doesn't have an active contract this month

For no of new customers I want to show as a total on monthly bases, sum of customers which hadn't an active contract last month but they have an active contract this month.

 

Thank you in advance !

hi @URobert 

For 2no of customers lost and 3no of new customers , Could you please explain it based on the above sample data, I'm a little confused by your description. that will be a great help.

 

Regards,

Lin

Community Support Team _ Lin
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Hi

Thank you for your reply,

I can't upload the pbx file, so I'll try to explan here, in more detail for point 2 and 3.

I've created a table with all the counts (active, new and lost customers).

Total unique count for no of new customers and no of lost customers shoud look like this :

 

- based on year and month :

image.png

- based on year :

image.png

 

Please find below also the data colored :

- yellow -> new customer

- oragne -> lost customer (temporary)

- green -> new customer (gain an old customer)

- blue -> lost customer

image.png

 

Thank you in advance !

hi  @URobert 

Ok, I have the adjust all these formula:

no of new customers = 
var _premonthvalue=CALCULATE(DISTINCTCOUNT('Table'[Customer name]),FILTER('Table','Table'[valid from]<=MAXX(DATEADD('Date'[Date],-1,MONTH),'Date'[Date])&&'Table'[new valid to]>=MAXX(DATEADD('Date'[Date],-1,MONTH),'Date'[Date])))
return
IF(([no of customers with active contract]>0&&_premonthvalue=BLANK())||([no of customers with active contract]>_premonthvalue),1)
new customers = 
var _table=SUMMARIZE('Date','Date'[Year],'Date'[Month],"_value",[no of new customers]) return
SUMX(_table,[_value])

 

no of customers lost = 
var _premonthvalue=CALCULATE(DISTINCTCOUNT('Table'[Customer name]),FILTER('Table','Table'[valid from]<=MAXX(DATEADD('Date'[Date],-1,MONTH),'Date'[Date])&&'Table'[new valid to]>=MAXX(DATEADD('Date'[Date],-1,MONTH),'Date'[Date])))
return
IF(([no of customers with active contract]=BLANK()&&_premonthvalue>0)||([no of customers with active contract]<_premonthvalue),1)
lost customers = 
var _table=SUMMARIZE('Date','Date'[Year],'Date'[Month],"_value",[no of customers lost]) return
SUMX(_table,[_value])

 

and here is new sample pbix file, please try it.

 

 

Regards,

Lin

Community Support Team _ Lin
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Hi 

 

Thank you very much for your solution !!!

I was also able to download your file :).

 

The only issue that I have is that the new valid to date is the first day of next month and is counting the customer as lost in the new month and is realy lost in the previos month.

 

Thank you again for your big help !

amitchandak
Super User
Super User

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.