Forum Discussion

pchecinsk's avatar
pchecinsk
Regular Visitor
1 year ago
Solved

Value for latest date, changing in time

Hi,

I have a problem with creating correct DAX formula that will sum [value] for latest [date], for each [id]. BUT I also have a classic date table, and want to show the sum related to different dates.

Something like snapshot of [value] for latest [date], but in different moments of time, for example as of end of month.

 

Sample input data below:

tblStatus

iddatevalue
121.02.202424400
107.03.202424400
104.04.202424400
102.05.202424400
105.06.202424400
108.07.202430000
107.08.202432000
104.09.202432000
102.10.202432000
221.02.20241500
220.03.20241500
219.04.20241500
221.05.20241500
208.07.20241500
205.08.20243000
205.09.20243000
204.10.20243000
321.02.2024400
328.02.2024400
305.04.2024400
308.05.2024400
306.06.2024590
322.07.2024590
321.08.2024590
323.09.2024590
324.09.2024590
421.02.202423333
404.03.202423333
403.04.202423333
406.05.202423333
403.06.202423333
427.06.202423333
402.07.202423333
410.07.202423333
411.07.202423333
415.07.202423333
424.07.202423333
425.07.202423333
413.08.202423333
414.08.202423333
415.08.202423333
429.08.202423333
402.09.202423333
403.09.202423333
404.09.202423333
411.09.202423333
412.09.202423333
408.10.202423333
521.02.20240
505.03.2024500
502.04.2024500
530.04.2024500
528.05.2024500
513.06.2024500
526.06.20241000
524.07.20241000
521.08.20241000
518.09.20241000
523.09.20241000
621.02.202412000
621.03.202412000
624.04.202412000
606.06.202412000
616.07.202412000
617.07.202415000
627.08.202415000
628.08.202415000
624.09.202415000
721.02.20248000
722.03.20248000
723.04.20248000
720.05.20248000
725.06.20248000
715.07.20249000
712.08.20249000
709.09.20249000
723.09.20249000
708.10.20249000

 

tblCalendar

 

date
31.01.2024
29.02.2024
31.03.2024
30.04.2024
31.05.2024
30.06.2024
31.07.2024
31.08.2024
30.09.2024
31.10.2024
30.11.2024
31.12.2024

 

Required output (pivot table):

e.g. for [id]=1:

date (from tblCalendar)value (from tblStatus)
31.05.2024 (end of May)24400
30.06.2024 (end of June)24400
31.07.2024 (end of July)30000
31.08.2024 (end of August)32000

 

 

  • Anonymous's avatar
    Anonymous
    1 year ago

    Hi pchecinsk 

    Please try the following Dax:


    Measure = 
    VAR _calendar_month = MONTH(SELECTEDVALUE(tblCalendar[FormattedDate]))
    
    RETURN
    SUMX(FILTER('tblStatus',MONTH('tblStatus'[FormattedDate])=_calendar_month),'tblStatus'[value])


    Then click "Show items with no data":




    Result:

     

     

    Sample pbix file attached.

     

     

     

     

     

    Best Regards,

    Jayleny

     

    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

     


     

  • pchecinsk - this was not made clear in your requirements. This will populate the prevoius months value where there is not a value for an id.

     

    VAR _end_month =
        MAX ( 'tblCalendar'[date] )
    VAR _start_month =
        EOMONTH ( _end_month, -1 ) + 1
    VAR _val =
        SUMX (
            FILTER (
                'tblStatus',
                'tblStatus'[date] >= _start_month
                    && 'tblStatus'[date] <= _end_month
            ),
            'tblStatus'[value]
        )
    VAR _prev_month_end =
        EOMONTH ( _end_month, -1 )
    VAR _prev_month_start =
        EOMONTH ( _end_month, -2 ) + 1
    VAR _Prev_val =
        SUMX (
            FILTER (
                'tblStatus',
                'tblStatus'[date] >= _prev_month_start
                    && 'tblStatus'[date] <= _prev_month_end
            ),
            'tblStatus'[value]
        )
    RETURN
        IF ( NOT ISBLANK ( _val ), _val, _Prev_val )

     

    If this works for you, please accept it as the solution, it helps with visibility for others with the same issue.

     

7 Replies

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

    pchecinsk Try:

    Measure = 
      VAR __ID = MAX( 'tblStatus'[id] )
      VAR __Date = MAX( 'tblCalendar'[date] )
      VAR __Table = FILTER( 'tblStatus', [id] = __ID && [date] <= __Date )
      VAR __MaxDate = MAXX( __Table, [date] )
      VAR __Result = MAXX( FILTER( __Table, [date] = __MaxDate ), [value] )
    RETURN
      __Result
    • pchecinsk's avatar
      pchecinsk
      Regular Visitor

      Hi Greg, thanks for your quick response! In general, I find your solution helpful, thanks for insight!

      However, the formula returns [value] only for max [id] - I need to have sum for all [id] of [value] that are relevant for given [Calendar date]

      • mark_endicott's avatar
        mark_endicott
        Icon for Super User rankSuper User

        pchecinsk - This version will show a total for all id's and is filterable by ID. It will show you the value for the whole month at the end of the month. 

         

        VAR _end_month =
            MAX ( 'tblCalendar'[date] )
        VAR _start_month =
            EOMONTH ( _end_month, -1 ) + 1
        RETURN
            SUMX (
                FILTER (
                    'tblStatus',
                    'tblStatus'[date] >= _start_month
                        && 'tblStatus'[date] <= _end_month
                ),
                'tblStatus'[value]
            )

         

        Below are some screenshots to show unfiltered, and then filtered by ID.

         

         

         

         

        If this works for you, please accept it as the solution, it helps with visibility for others with the same issue.

         

  • Anonymous's avatar
    Anonymous
    Not applicable

    Thank you for your prompt reply Greg_Deckler !

    Hi pchecinsk 

    First, you need to change the dates in your two tables to a date form that can be recognized.

    FormattedDate = 
     VAR _ConvertedDate = DATE(
        VALUE(RIGHT([date], 4)), 
        VALUE(MID([date], 4, 2)), 
        VALUE(LEFT([date], 2))
    )
    RETURN
    FORMAT(_ConvertedDate, "MM/DD/YYYY")


     
    Then try the following measure:

    Measure = 
    VAR _calendar_month = MONTH(SELECTEDVALUE(tblCalendar[FormattedDate]))
    VAR _select_id = SELECTEDVALUE(tblStatus[id])
    
    RETURN
    SUMX(FILTER('tblStatus',MONTH('tblStatus'[FormattedDate])=_calendar_month && 'tblStatus'[id] = _select_id),'tblStatus'[value])

     

     

    Result:

     

     

     

     

     

     

     

    Best Regards,

    Jayleny

     

    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.