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
lnz
Frequent Visitor

Caluclate COUNT with grouping and dynamic date range

Hi, guys!

I hope you can help me with following problem:

 

I have a table Sales with next structure:

 

[Column] - Description

-----------------------
[SaleID] - ID of sale
[SaleDate] - Date of sale
[ContractID] - ID of contract. On one contract may be sereval sales
[ContractDate] - Date when contract was signed
[ClientName] - Cleint Name. One cleint can have several contracrats
[Maneger] - Manger Name. One manager can have several clients. Eeach cleint correspond only to one manager
[HasNewContractInPeriod] - The flag that informs of the fact of concluding the new contract for this client, under which took place at least one sale in a period of 90 days before the date of current sale.

 

The problem is to calulate the [HasNewContractInPeriod] column.

 

I suppose it should be something like (that just a prototype of query):

SELECT ContractID, COUNT(SaleID) .... FROM Sales WHERE ContractDate BETWEEN ((SaleDate - 90, SaleDate) AND GroupBY ClientName HAVING COUNT(SaleID) > 1

1 ACCEPTED SOLUTION

@v-shex-msft, thanks for reply!

 

I got your idea, but it's not my way.

 

Finaly I decide to solve this task with SQL. Here is my code:

 

UPDATE "Sales" AS s1 
SET "maxClientContractDate" = (SELECT "mDate"
	FROM (SELECT "ContractID", MAX("ContractDate") AS "mDate"
		FROM "sales" 
		WHERE "SaleDate" < s1."SaleDate"
		AND "ClientName" =  s1."ClientName" 
		GROUP BY "ContractID" 
		HAVING COUNT(*) > 0) AS cs
	WHERE
        "mDate" < p1."SaleDate",
        ORDER BY "mDate" DESC LIMIT 1;

UPDATE "Sales" AS s1
SET "hasNewContractInPeriod" = 'TRUE' 
     WHERE "SaleDate" - "maxClientContractDate" > 90;

 

 

View solution in original post

2 REPLIES 2
v-shex-msft
Community Support
Community Support

Hi @lnz,

 

You can use below formula to achieve your goal.

 

Calculate column:

 

HasNewContractInPeriod = if(COUNTAX(FILTER(ALL(Sheet1),Sheet1[ContractID]=EARLIER(Sheet1[ContractID])&&Sheet1[ContractDate]>=DATEADD(Sheet1[SaleDate],-90,DAY)&&Sheet1[ContractDate]<=Sheet1[SaleDate]),[SaleID])>0,TRUE(),FALSE())

 

Regards,

Xiaoxin Sheng

Community Support Team _ Xiaoxin
If this post helps, please consider accept as solution to help other members find it more quickly.

@v-shex-msft, thanks for reply!

 

I got your idea, but it's not my way.

 

Finaly I decide to solve this task with SQL. Here is my code:

 

UPDATE "Sales" AS s1 
SET "maxClientContractDate" = (SELECT "mDate"
	FROM (SELECT "ContractID", MAX("ContractDate") AS "mDate"
		FROM "sales" 
		WHERE "SaleDate" < s1."SaleDate"
		AND "ClientName" =  s1."ClientName" 
		GROUP BY "ContractID" 
		HAVING COUNT(*) > 0) AS cs
	WHERE
        "mDate" < p1."SaleDate",
        ORDER BY "mDate" DESC LIMIT 1;

UPDATE "Sales" AS s1
SET "hasNewContractInPeriod" = 'TRUE' 
     WHERE "SaleDate" - "maxClientContractDate" > 90;

 

 

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.