Forum Discussion

Namoh's avatar
Namoh
Icon for Post Partisan rankPost Partisan
6 years ago
Solved

Adding WHERE MAX statement into DAX measure TOTALYTD

Hi.

 

I’ve got a DAX measure which calculates the TOTALYTD: YTD_Unplanned_Downtine_Target = IF( SUM(Targets[Target Unplanned downtime (hrs)]) = BLANK(); BLANK ();TOTALYTD(SUM(Targets[Target Unplanned downtime (hrs)]);'CALENDAR'[Date];ALL('CALENDAR');"3/31"))

 

This works fine. But when I put this in a table it will show all figures for the whole year, see picture 1.

 

I would like to only show the results uptill the current month, so I can compare the targets with the actuals, without having to use/change the filter each month.

 

I’ve made a measure which looks up the latest month, see picture 2: Latest Month = MAX(kpiexport[Month])

 

 

How to merge both measures into one measure so that the TOTALYTD, for the future months, wont show in my table?

I assume I have to combine the MAX with somekind of WHERE statement?

If another method is possible, I’m also interested in hearing this.

 

  • Ok, so to be honest, I don't know what I did to make it work, but it works!

     

    I was fooling around with some fields (Calendar-Date vs Targets-Begin van de maand), I was adding a Filter onto measure _5_ stating show all that's Not Empty, I clicked in the table in the right top corner on Zoom Out and Expand 1 level in Hierarchie, etc and now it shows:

    1. just the values of the beginning of the months

    2. it summarizes after each month

    3. it stops after the current month

     

    Thanks for your help.

     

     

     

     

26 Replies

  • Namoh ,  refer if these can help

    example

    YTD QTY forced= 
    var _max = today()
    return
    calculate(Sum('order'[Qty]),DATESYTD('Date'[Date]),'Date'[Date]<=_max)
    //calculate(TOTALYTD(Sum('order'[Qty]),'Date'[Date]),filter('Date','Date'[Date]<=_max))
    
    LYTD QTY forced= 
    var _max = date(year(today())-1,month(today()),day(today()))
    return
    CALCULATE(Sum('order'[Qty]),DATESYTD(dateadd('Date'[Date],-1,year)),'Date'[Date]<=_max)
    //TOTALYTD(Sum('order'[Qty]),dateadd('Date'[Date],-1,year),'Date'[Date]<=_max)
    
    
    YTD QTY forced= 
    var _max = maxx('order',[Order date])
    return
    calculate(Sum('order'[Qty]),DATESYTD('Date'[Date]),'Date'[Date]<=_max)
    //calculate(TOTALYTD(Sum('order'[Qty]),'Date'[Date]),filter('Date','Date'[Date]<=_max))
    
    LYTD QTY forced= 
    var _max1 =maxx('order',[Order date])
    var _max = date(year(_max1)-1,month(_max1),day(_max1))
    return
    CALCULATE(Sum('order'[Qty]),DATESYTD(dateadd('Date'[Date],-1,year)),'Date'[Date]<=_max)
    //TOTALYTD(Sum('order'[Qty]),dateadd('Date'[Date],-1,year),'Date'[Date]<=_max)
    

     

    use date table

    https://www.youtube.com/watch?v=Qt0TM-4H09U&list=PLPaNVDMhUXGaaqV92SBD5X2hk3TMNlHhb&index=5

  • AntrikshSharma's avatar
    AntrikshSharma
    Icon for Community Champion rankCommunity Champion

    Namoh  If the goal is to create a running total then you can use this:

     

    Create a separate measure for SUM ( Targets[Target Unplanned downtime (hrs)] )

    Total Downtime = SUM ( Targets[Target Unplanned downtime (hrs)] )
    YTD_Unplanned_Downtine_Target =
    VAR MaxVisibleDate =
        MAX ( Dates[Date] )
    VAR MaxYear =
        YEAR ( MaxVisibleDate )
    VAR DatesLessThanMaxDate =
        FILTER (
            ALL ( Dates ),
            Dates[Date] <= MaxVisibleDate
                && Dates[Calendar Year Number] = MaxYear
        )
    VAR Downtime =
        CALCULATE ( [Total Downtime], DatesLessThanMaxDate )
    VAR Result =
        IF ( [Total Downtime] <> BLANK (), Downtime )
    RETURN
        Result
    

     

    • Namoh's avatar
      Namoh
      Icon for Post Partisan rankPost Partisan

      I've added 2 columns.

       

      One with: _1_Total Downtime = SUM ( Targets[Target Unplanned downtime (hrs)] )

       

      The other one with:  _2_YTD_Unplanned_Downtine_Target =
      VAR MaxVisibleDate =
      MAX ( Calendar[Date] )
      VAR MaxYear =
      YEAR ( MaxVisibleDate )
      VAR DatesLessThanMaxDate =
      FILTER (
      ALL ( Calendar );
      Calendar[Date] <= MaxVisibleDate
      && Calendar[Year] = MaxYear
      )
      VAR Downtime =
      CALCULATE ( [_1_Total Downtime]; DatesLessThanMaxDate )
      VAR Result =
      IF ( [_1_Total Downtime] <> BLANK (); Downtime )
      RETURN
      Result

       

      but I get an error message. Column _1_Total_Downtime not found or can't be used in this expression.

      • AntrikshSharma's avatar
        AntrikshSharma
        Icon for Community Champion rankCommunity Champion

        Namoh  Nope, you have to create measures, you are doing running total in a visual right? 🙂

         

  • v-zhenbw-msft's avatar
    v-zhenbw-msft
    Icon for Community Support rankCommunity Support

    Hi Namoh ,

     

    Do you want to calculate the total from the beginning of the year to the maximum date?

     

    If yes, maybe you can try this measure.

     

    Measure = 
    var _lastest_date = MAX('Table (2)'[Date])
    return
    CALCULATE(SUM('Table'[value]),FILTER(ALLSELECTED('Table'),'Table'[Date]<=_lastest_date))

     

    In the sample, the latest date is 2020/7/1, so the total calculates the value of 2020/1/1 – 2020/7/1.

     

     

    If it doesn’t meet your requirement, could you please provide a mockup sample based on fake data or describe the fields of each tables and the relations between tables simply?

    It will be helpful if you can show us the exact expected result based on the tables.

     

    Please upload your files to OneDrive For Business and share the link here. Please don't contain any Confidential Information or Real data in your reply.

     

    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.

    • Namoh's avatar
      Namoh
      Icon for Post Partisan rankPost Partisan

      Hi, this is almost what I'm looking for.

      But instead of calculating from the beginning of the year, I need to calculated from the beginning of our fiscale year, which starts at 01-apr. 

      • Namoh's avatar
        Namoh
        Icon for Post Partisan rankPost Partisan

        I noticed that my table showed incorrect dates (it showed jan-2020, while this should have been jan-2021).

        I corrected my data, used the above formula and now it cuts of nicely at the current month.

         

        But now it isn't summarizing anymore, as you can see in the picture below.

        Both columns show exactly the same numbers, while the 2nd column should be a summarisation. like the table.

         

        Where in the formula am I missing something?

         

        Anyone who can help with the last missing part?

         

        Formula/measure column 1: 

        _1_Total Downtime M = SUM ( Targets[Target Unplanned downtime (hrs)] )
         
        Formula/measure column 2:  
        _2_YTD_Unplanned_Downtine_Target M =
        VAR MaxVisibleDate =
        MAX ( kpiexport[Month] )
        VAR MaxYear =
        YEAR ( MaxVisibleDate )
        VAR DatesLessThanMaxDate =
        FILTER (
        ALL ( Calendar );
        Calendar[Date] <= MaxVisibleDate
        && Calendar[Year] = MaxYear
        )
        VAR Downtime =
        CALCULATE ( [_1_Total Downtime M]; DatesLessThanMaxDate )
        VAR Result =
        IF ( [_1_Total Downtime M] <> BLANK (); Downtime )
        RETURN
        Result

         

         _1_Total_Downtime_M_2_YTD_Total_Downtime_Target_M
        woensdag 1 april 20209595
        vrijdag 1 mei 2020104199
        maandag 1 juni 2020101300
        woensdag 1 juli 202089389
        zaterdag 1 augustus 202020 
        dinsdag 1 september 202082 
        donderdag 1 oktober 2020105 
        zondag 1 november 202096