Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
1 year ago
Solved

Get last 4 values/dates from a table

Hi Experts

I have a requirement to calculate 4 four values average.

 

My data is as such

Namedatevalue
Ammonia

1/1/2023

1.25
Ammonia 1/7/20230.21

Ammonia

1/19/20230.3
Ammonia1/22/20231.1
Ammonia2/1/20233.11
Ammonia2/4/20230.32
Ammonia2/12/20230.91
Ammonia2/23/20231.01
Ammonia2/25/20232.43
Potassium1/7/20230.02
Potassium1/11/20230.68
Potassium1/19/20230.81
Potassium2/5/20231.66
Potassium2/23/20231.25

I need to display 2 tables like this. One table with summary and bottom table with only last 4 values for each Name column

NameTimeperiodAverage
Ammonia2/4/2023 - 2/25/20231.2
Potassium1/11/2023 - 2/23/20231.1
Namedatevalueavg
Ammonia2/4/20230.321.2
Ammonia2/12/20230.911.2
Ammonia2/23/20231.011.2
Ammonia2/25/20232.431.2
Potassium1/11/20230.681.1
Potassium1/19/20230.811.1
Potassium2/5/20231.661.1

Potassium

2/23/20231.251.1
  • Hi Anonymous 

     

    You can create a measure that virtually ranks the dates for each Name and filter that to ranks <=4

    Latest 4 Value Avg = 
    AVERAGEX (
        FILTER (
            SUMMARIZECOLUMNS (
                Data[Name],
                Data[date],
                "@rank", RANKX ( ALL ( Data[date] ), CALCULATE ( AVERAGE ( Data[date] ) ),, DESC, DENSE ),
                "@value", CALCULATE ( SUM ( Data[value] ) )
            ),
            [@rank] <= 4
        ),
        [@value]
    )
    

     

    And then another measure referencing the first one but returns a value only for those with ranks <= 4.

    Name Avg = 
    IF (
        NOT ( ISBLANK ( [Latest 4 Value Avg] ) ),
        CALCULATE ( [Latest 4 Value Avg], ALLEXCEPT ( Data, Data[Name] ) )
    )
    

     

    Please see the attached sample pbix.

4 Replies

  • Hi Anonymous 

     

    You can create a measure that virtually ranks the dates for each Name and filter that to ranks <=4

    Latest 4 Value Avg = 
    AVERAGEX (
        FILTER (
            SUMMARIZECOLUMNS (
                Data[Name],
                Data[date],
                "@rank", RANKX ( ALL ( Data[date] ), CALCULATE ( AVERAGE ( Data[date] ) ),, DESC, DENSE ),
                "@value", CALCULATE ( SUM ( Data[value] ) )
            ),
            [@rank] <= 4
        ),
        [@value]
    )
    

     

    And then another measure referencing the first one but returns a value only for those with ranks <= 4.

    Name Avg = 
    IF (
        NOT ( ISBLANK ( [Latest 4 Value Avg] ) ),
        CALCULATE ( [Latest 4 Value Avg], ALLEXCEPT ( Data, Data[Name] ) )
    )
    

     

    Please see the attached sample pbix.

    • Anonymous's avatar
      Anonymous
      Not applicable

      Thanks for your proposed solution.  For the summarized table (table1) solution is working as expected.

       

    • Anonymous's avatar
      Anonymous
      Not applicable

      danextian, please also let me know to limit table only to display  rows for  last 4 dates. Because I want to display in my 2nd table with only last4 dates. With the above measure "Latest 4 Value Avg" not giving me blanks in my case so I'm not able to filter blank values from table

       

      • danextian's avatar
        danextian
        Super User

        You can see in my example that Latest 4 Value Avg is showing blanks. Please share a sanitized copy of your pbix so I can inspect why.