The ultimate Fabric, Power BI, SQL, and AI community-led learning event. Save €200 with code FABCOMM.
Get registeredCompete to become Power BI Data Viz World Champion! First round ends August 18th. Get started.
Dear Community,
I am looking for a way to list customers who made 0 orders during current month and the previous 2 months.
The data in the orders table (TableA) includes
I expect to see a list of those customers who did make at least one order in the past (thus in the table) but there is no order in the table recently.
My thinking so far:
Many thanks in advance,
Kroof
Solved! Go to Solution.
Hi @Kroof
You need first to create a new calculated column in TableA of integer data type that ranks the period column as folows:
Period Rank =
RANKX ( TableA, TableA[period],, ASC, DENSE )
Then the calculated column in TableB would be
CountOfOrders =
VAR RelatedTableA =
RELATEDTABLE ( TableA )
VAR LastPeriod =
MAX ( TableA[Period Rank] )
RETURN
COUNTX (
FILTER (
RelatedTableA,
TableA[Period Rank] >= LastPeriod - 2
&& TableA[Period Rank] <= LastPeriod
),
TableA[order_number]
)
Hi @Kroof
Please try
CountOfOrders =
COUNTX (
TOPN (
3,
FILTER ( Table_A, TableA[customerID] = TableB[customerID] ),
Table_A[period]
),
TableA[order_number]
)
Thanks for the reply. This solution counts those periods where there is at least one order, and not the last 3 thus I never get a 0 count. Can we create a list or variable and inject into the FILTER function? I tried this but I get an error.
CountOfOrders =
VAR Last3Periods =
TOPN(
3,
TableA,
TableA[period]
)
return
COUNTX (
FILTER(TableA,
TableA[customerID] = TableB[customerID]
&&
TableA[period] = Last3Periods
)
TableA[order_number]
)
WHat shall I amend?
yes, TableA and TableB is related n:1 based on customerID.
@Kroof
Ok, that should make it even easier. But what is the data type of the YYYY.MM [period] column? Is it date or text?
text
Hi @Kroof
You need first to create a new calculated column in TableA of integer data type that ranks the period column as folows:
Period Rank =
RANKX ( TableA, TableA[period],, ASC, DENSE )
Then the calculated column in TableB would be
CountOfOrders =
VAR RelatedTableA =
RELATEDTABLE ( TableA )
VAR LastPeriod =
MAX ( TableA[Period Rank] )
RETURN
COUNTX (
FILTER (
RelatedTableA,
TableA[Period Rank] >= LastPeriod - 2
&& TableA[Period Rank] <= LastPeriod
),
TableA[order_number]
)
Applied and double checked the results. Works like a charm. Many thanks!
User | Count |
---|---|
24 | |
10 | |
8 | |
7 | |
6 |
User | Count |
---|---|
31 | |
12 | |
10 | |
10 | |
9 |