Forum Discussion

gunjan80's avatar
gunjan80
Microsoft Employee
6 years ago
Solved

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

 

 
  • gunjan80's avatar
    gunjan80
    6 years ago

    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

  • 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-missed-these-Gems-Daily/td-p/1247593

     

    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-msft's avatar
    v-zhenbw-msft
    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))

     

     

     

    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.

  • gunjan80's avatar
    gunjan80
    Microsoft 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.

     

    • v-zhenbw-msft's avatar
      v-zhenbw-msft
      Community 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.

      • gunjan80's avatar
        gunjan80
        Microsoft 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.