The ultimate Fabric, Power BI, SQL, and AI community-led learning event. Save €200 with code FABCOMM.
Get registeredEnhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends September 15. Request your voucher.
Hello,
I have a table named Sales with two columns: InvoiceDate and Customer. I want to retrieve a list of customers who have not placed orders within a selected number of days, compared to the same number of days prior.
For instance, if today is March 20th, I want to identify customers who placed orders between March 11th and March 20th, and then compare this with their order history between March 1st and March 10th. Customers who ordered between March 1st and March 10th but not between March 11th and March 20th should be included in the list.
To achieve this, I created a parameter named "Days" with a data type of Whole Number and a range of values from 1 to 30.
I created a table using the following DAX formula to retrieve the list of customers:
```DAX
Customers Not Ordering in Last X Days =
VAR XDays = SELECTEDVALUE(Days[XDays], 30) // Default to 30 if no value is selected
VAR PeriodStart = TODAY() - XDays
VAR CustomersWithOrders = CALCULATETABLE(
DISTINCT(Sales[Customer]),
Sales[InvoiceDate] >= PeriodStart // Changed the equality sign to greater than or equal to
)
VAR PreviousDays = PeriodStart - XDays
VAR AllCustomers = CALCULATETABLE(
DISTINCT(Sales[Customer]),
Sales[InvoiceDate] < PeriodStart,
Sales[InvoiceDate] >= PreviousDays // Changed the equality sign to less than
)
VAR CustomersNotOrdering = EXCEPT(AllCustomers, CustomersWithOrders)
RETURN
CustomersNotOrdering
```
I'm using the parameter as a slicer, but when I select values using the slicer, the formula does not seem to work. However, when I hardcode the value of `XDays`, the formula works fine. Even `VAR XDays = SELECTEDVALUE(Days[XDays], 30)` only works with the default value.
I appreciate any assistance with resolving this issue. Thank you.
Solved! Go to Solution.
@sujit_Shirke
Please find the file attached.
https://drive.google.com/file/d/1dEKygfkYa-omPEodTfcsuu1Agj0mW9Rw/view?usp=share_link
If the post helps please give a thumbs up
If it solves your issue, please accept it as the solution to help the other members find it more quickly.
Tharun
Hi @sujit_Shirke ,
Get the XDays as return of your dax. then you can see what it feeds to the function.
Thanks
Dinesh
Its return which i selected in Slicer
Whole Number
Can you share the pbix file with sample data?
Its not allowing to send sample pbix
@sujit_Shirke
In your file you created a caculated table, A calcualted table is static and it cannot capture the seclections made in the slicer even if you use selectedvalue function. Incase if you want to count the number of customers according to the selections in the slicer then create a measure and use countrows function.
Customers Not Ordering in Last X Days =
VAR XDays = SELECTEDVALUE(Days[XDays], 30) // Default to 30 if no value is selected
VAR PeriodStart = TODAY() - XDays
VAR CustomersWithOrders = CALCULATETABLE(
DISTINCT(Sales[Customer]),
Sales[InvoiceDate] >= PeriodStart // Changed the equality sign to greater than or equal to
)
VAR PreviousDays = PeriodStart - XDays
VAR AllCustomers = CALCULATETABLE(
DISTINCT(Sales[Customer]),
Sales[InvoiceDate] < PeriodStart,
Sales[InvoiceDate] >= PreviousDays // Changed the equality sign to less than
)
VAR CustomersNotOrdering = EXCEPT(AllCustomers, CustomersWithOrders)
RETURN
countrows( CustomersNotOrdering)
Or if you want to see the customer list according to then one of the patter that you can use is:
Customers Not Ordering in Last X Days =
VAR XDays = SELECTEDVALUE(Xday[Xday], 30) // Default to 30 if no value is selected
VAR PeriodStart = TODAY() - XDays
VAR CustomersWithOrders = CALCULATETABLE(
DISTINCT(Sales[Customer]),
Sales[InvoiceDate] >= PeriodStart // Changed the equality sign to greater than or equal to
)
VAR PreviousDays = PeriodStart - XDays
VAR AllCustomers = CALCULATETABLE(
DISTINCT(Sales[Customer]),
Sales[InvoiceDate] < PeriodStart,
Sales[InvoiceDate] >= PreviousDays // Changed the equality sign to less than
)
VAR CustomersNotOrdering = EXCEPT(AllCustomers, CustomersWithOrders)
RETURN
IF(SELECTEDVALUE(Sales[Customer] ) in CustomersNotOrdering, 1, 0)
and put filters to remove 0.
If the post helps please give a thumbs up
If it solves your issue, please accept it as the solution to help the other members find it more quickly.
Tharun
no luck
@sujit_Shirke
Please find the file attached.
https://drive.google.com/file/d/1dEKygfkYa-omPEodTfcsuu1Agj0mW9Rw/view?usp=share_link
If the post helps please give a thumbs up
If it solves your issue, please accept it as the solution to help the other members find it more quickly.
Tharun