Forum Discussion
Filter using last date not working
I have a Sales table. I am trying to get the sale amount as per the last sale quarter. I've tried, using 'LastDate' function. It works independantly but it does not work with the filter combination. I want to show the visualization as a matrix. The below Dax statement works if I mention "Q2". But it does not work if I try to use lastdate or Max(Quarter). I cannot keep changing Q2 to Q3/Q4 every time there is additional sales. Please help. Thanks.
Last Sale Amount = CALCULATE(sum(Table1[Sales]), FILTER(ALLEXCEPT(Table1, 'Table1'[Customer Name]), 'Table1'[Quarter] = "Q2" ))
| Customer Name | Sales | Product | Quarter | Date |
| C1 | 100 | abc | Q1 | 3/31/2020 |
| C2 | 200 | mmm | Q1 | 3/31/2020 |
| C3 | 300 | ggg | Q1 | 3/31/2020 |
| C4 | 400 | abc | Q1 | 3/31/2020 |
| C5 | 500 | ggg | Q1 | 3/31/2020 |
| C1 | 150 | mmm | Q2 | 6/30/2020 |
| C2 | 250 | ddd | Q2 | 6/30/2020 |
| C3 | 350 | ggg | Q2 | 6/30/2020 |
| C4 | 450 | abc | Q2 | 6/30/2020 |
| C5 | 550 | hhh | Q2 | 6/30/2020 |
I fixed this. Here is the DAX.
Last Sale Amount2 = CALCULATE(sum(Table1[Sales]), All('Date'[Quarter]), CALCULATETABLE(LASTDATE(Table1[Date]),ALLEXCEPT(Table1, 'Table1'[Customer Name]) ))
9 Replies
- amitchandakSuper User
gunjan80 , If have data saved on the last date of qtr . You can join with date table and use time intelligence to get the data of this qtr vs last qtr.
Now if data on different dates of qtr and you wnat data of only last date use closing balance quarter
https://docs.microsoft.com/en-us/dax/closingbalancequarter-function-dax
QTD
QTD Sales = CALCULATE(SUM(Sales[Sales Amount]),DATESQTD(('Date'[Date]))) Last QTD Sales = CALCULATE(SUM(Sales[Sales Amount]),DATESQTD(dateadd('Date'[Date],-1,QUARTER)))Power BI — QTD Questions — Time Intelligence 2–5
https://medium.com/@amitchandak.1978/power-bi-qtd-questions-time-intelligence-2-5-d842063da839Closing Balance
To get the best of the time intelligence function. Make sure you have a date calendar and it has been marked as the date in model view. Also, join it with the date column of your fact/s. Refer :
https://radacad.com/creating-calendar-table-in-power-bi-using-dax-functions
https://www.archerpoint.com/blog/Posts/creating-date-table-power-bi
https://www.sqlbi.com/articles/creating-a-simple-date-table-in-dax/
See if my webinar on Time Intelligence can help: https://community.powerbi.com/t5/Webinars-and-Video-Gallery/PowerBI-Time-Intelligence-Calendar-WTD-YTD-LYTD-Week-Over-Week/m-p/1051626#M184
Appreciate your Kudos. - v-zhenbw-msftCommunity Support
Hi gunjan80 ,
If you want to display Last Sale Amount in a card visual, you can try this measure.
Last Sale Amount card = var _last_date = MAX(Table1[Date]) var _lastQ = CALCULATE(MAX(Table1[Quarter]),FILTER(Table1,Table1[Date]=_last_date)) return CALCULATE(sum(Table1[Sales]), FILTER(Table1,Table1[Quarter]=_lastQ))If you want to display Last Sale Amount in a table visual, you can try this measure.
last Sale table = var _last_date = CALCULATE(MAX(Table1[Date]),ALLSELECTED(Table1)) var _lastQ = CALCULATE(MAX(Table1[Quarter]),FILTER(ALLSELECTED(Table1),Table1[Date]=_last_date)) return CALCULATE(sum(Table1[Sales]), FILTER(ALLSELECTED(Table1),Table1[Quarter]=_lastQ))If it doesn’t meet your requirement, could you please show the exact expected result based on the table that you have shared?
Best regards,
Community Support Team _ zhenbw
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
BTW, pbix as attached.
- gunjan80Microsoft Employee
I need this data for forecasting purpose. Forecast calculations will be based on 'Current Sale Amt". Hence for all future quarters I need to use the data of the present quarter (present quarter could be Q3 or Q2 or Q1 or Q4). Below is the type of output I need in the form of a matrix. It works perfectly well with the below DAX, but the problem with it is after every quarter I will have to manually change the quarter from Q2 to Q3 / Q3 to Q4 etc. I tried using last date and Max functions but they dont work in this case.
Current Sale Amt = CALCULATE(sum(Table1[Sales]), FILTER(ALLEXCEPT(Table1, 'Table1'[Customer Name]), 'Table1'[Quarter] = "Q2" ))
C1 Q1 Q2 Q3 Q4 Actual Sale 100 150 Current Sale Amt 150 150 150 150 C2 Q1 Q2 Q3 Q4 Actual Sale 200 250 Current Sale Amt 250 250 250 250 C3 Q1 Q2 Q3 Q4 Actual Sale 300 350 Current Sale Amt 350 350 350 350 Thanks.
- v-zhenbw-msftCommunity Support
Hi gunjan80 ,
We can create a date table and a measure to meet your requirement.
1. Create a whole year date table, add a quarter column and there is no relationship.
Date = CALENDAR("2020/1/1","2020/12/31")Quarter = "Q" &""& QUARTER('Date'[Date])2. Then create a measure and use the Date[Quarter] to create a matrix table.
Current Sale Amt = VAR _x = CALCULATE ( SUM ( Table1[Sales] ), FILTER ( ALLEXCEPT ( Table1, 'Table1'[Customer Name] ), 'Table1'[Quarter] = MAX ( 'Date'[Quarter] ) ) ) VAR _y = CALCULATE ( SUM ( Table1[Sales] ), FILTER ( ALLEXCEPT ( Table1, Table1[Customer Name] ), Table1[Quarter] = MAX ( Table1[Quarter] ) ) ) RETURN IF ( ISBLANK ( _x ), _y, _x )If you have any question, please kindly ask here and we will try to resolve it.
Best regards,
Community Support Team _ zhenbw
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
BTW, pbix as attached.
- gunjan80Microsoft Employee
It worked perfectly when the date table was not connected. But unfortunatly I need the date table to be connected. My original model has lots of tables along with a date table. And I need the date table to be connected for various other calculations. The line highlighted in yelow is what I need. Thanks a lot for all the time and effort you've taken.