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.
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
Solved! Go to 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;
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
@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;
Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City
Check out the April 2024 Power BI update to learn about new features.
User | Count |
---|---|
113 | |
99 | |
80 | |
70 | |
60 |
User | Count |
---|---|
149 | |
114 | |
107 | |
89 | |
67 |