Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

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.

Reply
gunjan80
Employee
Employee

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 NameSalesProductQuarterDate
C1100abcQ13/31/2020
C2200mmmQ13/31/2020
C3300gggQ13/31/2020
C4400abcQ13/31/2020
C5500gggQ13/31/2020
C1150mmmQ26/30/2020
C2250dddQ26/30/2020
C3350gggQ26/30/2020
C4450abcQ26/30/2020
C5550hhhQ26/30/2020

 

 
1 ACCEPTED SOLUTION

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]) ))
 
 

 
 
 

 

 
 
 
 
 
 
 
 
 
 
 
 

 

 

 

 

 

 

 

 

 

 

 
 

 

 

View solution in original post

9 REPLIES 9
gunjan80
Employee
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" ))

 

C1Q1Q2Q3Q4
Actual Sale100150  
Current Sale Amt150150150150
C2Q1Q2Q3Q4
Actual Sale200250  
Current Sale Amt250250250250
C3Q1Q2Q3Q4
Actual Sale300350  
Current Sale Amt350350350350
     

 

Thanks.

 

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])

 

Fil1.jpg

 

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 )

 

Fil2.jpg

 

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.

 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.

gunjan80_1-1595999013545.png

 

Hi @gunjan80 ,

 

Sorry for that if a relationship is established, only quarters with data will be displayed.

Maybe you can create a new date table and create a relationship with original table to do some calculations.

 

Or you can create four Measures to calculate quarter sales separately.

 

Q1 = 
VAR _maxQ =
    CALCULATE (
        MAX ( Table1[Quarter] ),
        FILTER (
            ALLSELECTED ( Table1 ),
            Table1[Customer Name] = MAX ( Table1[Customer Name] )
        )
    )
RETURN
    IF (
        "Q1" <= _maxQ,
        CALCULATE ( SUM ( Table1[Sales] ), FILTER ( Table1, Table1[Quarter] = "Q1" ) ),
        CALCULATE ( SUM ( Table1[Sales] ), FILTER ( Table1, Table1[Quarter] = _maxQ ) )
    )

 

Q2 = 
VAR _maxQ =
    CALCULATE (
        MAX ( Table1[Quarter] ),
        FILTER (
            ALLSELECTED ( Table1 ),
            Table1[Customer Name] = MAX ( Table1[Customer Name] )
        )
    )
RETURN
    IF (
        "Q2" <= _maxQ,
        CALCULATE ( SUM ( Table1[Sales] ), FILTER ( Table1, Table1[Quarter] = "Q2" ) ),
        CALCULATE ( SUM ( Table1[Sales] ), FILTER ( Table1, Table1[Quarter] = _maxQ ) )
    )

 

Q3 = 
VAR _maxQ =
    CALCULATE (
        MAX ( Table1[Quarter] ),
        FILTER (
            ALLSELECTED ( Table1 ),
            Table1[Customer Name] = MAX ( Table1[Customer Name] )
        )
    )
RETURN
    IF (
        "Q3" <= _maxQ,
        CALCULATE ( SUM ( Table1[Sales] ), FILTER ( Table1, Table1[Quarter] = "Q3" ) ),
        CALCULATE ( SUM ( Table1[Sales] ), FILTER ( Table1, Table1[Quarter] = _maxQ ) )
    )

 

Q4 = 
VAR _maxQ =
    CALCULATE (
        MAX ( Table1[Quarter] ),
        FILTER (
            ALLSELECTED ( Table1 ),
            Table1[Customer Name] = MAX ( Table1[Customer Name] )
        )
    )
RETURN
    IF (
        "Q4" <= _maxQ,
        CALCULATE ( SUM ( Table1[Sales] ), FILTER ( Table1, Table1[Quarter] = "Q4" ) ),
        CALCULATE ( SUM ( Table1[Sales] ), FILTER ( Table1, Table1[Quarter] = _maxQ ) )
    )

 

F1.jpg

 

F2.jpg

 

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.

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]) ))
 
 

 
 
 

 

 
 
 
 
 
 
 
 
 
 
 
 

 

 

 

 

 

 

 

 

 

 

 
 

 

 

Hi @gunjan80 ,

 

Glad you found the solution.

Please mark your reply as an answer, let more people find it more quickly, thanks.

 

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.

I've marked it as a solution. Thankyou for helping me on this.

v-zhenbw-msft
Community Support
Community 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))

 

F1.jpg

 

F2.jpg

 

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))

 

F3.jpg

 

F4.jpg

 

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.

amitchandak
Super User
Super 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-d842063da839

 

Closing Balance

https://community.powerbi.com/t5/Quick-Measures-Gallery/Allocating-Targets-in-Power-BI-Why-You-misse...

 

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-Y...


Appreciate your Kudos.

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.