Forum Discussion

RichardT_78's avatar
RichardT_78
Helper I
3 years ago

adding row values

How do you calculate the last 3 row values into a messure?

 

E,g 

 

value  New Messure 

10             31

11             30

10             22

9               16

3                7 

4                4

 

 

4 Replies

  • HotChilli's avatar
    HotChilli
    Community Champion

    You filter the data to find the last 3 values based on some ordering (either an index or a date field) and then sum the values.
    If the ordering is regular, it can be simpler e.g. index of 1,2,3,4 but otherwise you might have to use TOPN

    Do you have an ordering?

  • I can use the date 

    I used the following code i found online which looks to SUM the filtered values, which would be around 24,366 but i dont understand the last part of the code which references the date and + 3 

     

    if i look at the last 4 values the sum will always be the same unless data is missing due to it being repetitive

     

    CIN Total =
    CALCULATE (
        SUM ( 'Events'[EventId] ),
        FILTER (
            ALL ( 'Events' ),
            'Events'[EventId]
                && 'Events'[End Date/Time] >= SELECTEDVALUE ( 'Events'[End Date/Time])
                && 'Events'[End Date/Time]
                    <= SELECTEDVALUE ( 'Events'[End Date/Time] ) + 3
        )
    )
     
    Any Help would be much appricated 
  • HotChilli's avatar
    HotChilli
    Community Champion

    Test it separately.

    The FILTER statement creates a table so Hit New Table in powerbi and paste in that part of the statement.  You can use a variable instead of SELECTEDVALUE ( 'Events'[End Date/Time]) - hardcode the value to be a datetime you have in the table.

    You can then see what that part of the code does.

     

    • RichardT_78's avatar
      RichardT_78
      Helper I

      Thank you, 

      Yes i can see that beacuse the date and time stamp are identical they are both assinged the same value so i need to order the data. this is a problem to other data in the set. 

       

      My code works for this small example but only because of the unique index

       

       

      I used this code to order the data that was filtered

      index_move = RANKX(ALLSELECTED(CIN_Table),CALCULATE(SUM(CIN_Table[End Date/Time])),,ASC,Dense)
       
      But it then looks like this:
       

      So i would need to either differenate the 1,1,2,2 to 1,2,3,4 or another option to count in groups of 4 ?