Forum Discussion
DAX Script filter last x days
- 1 year ago
Hi Heinrich ,
Apologies for the delayed response. I wanted to let you know that I tested the TODAY() logic, and it worked as expected. I'm wondering if you have been able to resolve the issue on your end. If not, please consider my response below.
1. Ensure that the Calendar table is correctly related to your fact table.
2. The filter logic in your original query has some redundancy. For example, this section:AND( AND( AND('Calendar'[Date] >= TODAY() - 7, 'Calendar'[Date] < TODAY()), 'Calendar'[Date] >= TODAY() - 7 ), 'Calendar'[Date] < TODAY() ) )
can be simplified to:'Calendar'[Date] >= TODAY() - 7 && 'Calendar'[Date] < TODAY()
This ensures cleaner code and improves readability without altering the logic.3. Combine all variable definitions under one DEFINE block:
DEFINE VAR __DS0FilterTable = ... VAR __DS0FilterTable2 = ... VAR __DS0FilterTable3 = ... VAR __DS0Core = ... VAR __DS0PrimaryWindowed = ...
4. Consider defining variables to store common values like TODAY() and TODAY()-7. This can clean up your code and reduce repetition.VAR Last7Days = TODAY() - 7 VAR TodayDate = TODAY() RETURN FILTER( 'Calendar', 'Calendar'[Date] >= Last7Days && 'Calendar'[Date] < TodayDate )
5. The KEEPFILTERS() function is only necessary when you want to preserve the context of filters, but it might be redundant if you’re already in the right context.
6. Ensure your data actually contains records for the last 7 days. If no data exists, your filter will return an empty result.If you have received any error messages or encountered unexpected behavior, please share those details, that can help pinpoint the issue more precisely.
If you have found a solution or used a different approach that works, please share it with the community to help others.
If my response has been helpful, please consider marking it as Accepted Solution to assist others and a Kudos would always be appreciated.
Thank you.
Hi v-veshwara-msft ,
What is the problem that you are having with this code? Can you please give some additional information?
Hello v-veshwara-msft
Hope you are doing well.
The code of MFelix is more elaborate and looking at mine I think I miss something.
Regards
Heinrich
- v-veshwara-msft1 year agoCommunity Support
Hi Heinrich ,
Thank you for the update, and I am glad you were able to filter the data successfully using your version of the DAX code.If your current setup is working as expected and you are getting the correct results, you can confidently continue using your solution.
Both your approach and the one shared earlier by MFelix are valid -- it mainly depends on how simple or complex your report requirements are.If you notice any specific issues like missing records, unexpected totals, or any errors, feel free to share more details or screenshots so we can assist you further.
Also, if any response helped or you found a solution, it would be great if you could accept it as the solution to help others in the community.
Best regards,
Vinay.