Forum Discussion

twister8889's avatar
twister8889
Icon for Helper V rankHelper V
6 years ago
Solved

N values MIN/MAX EDITOR QUERY

Hi guys,

 

I have some data like the table below, I need to find the N min/max values per period to calculate the average, for example, in the first row:

 

Average Min:

I find the Values from months 02-2020 and 04-2020 (because in this case its the minors values, I need to find 2 months until 06-2020), after that, I calculate the average from this.

 

Average Max

I find the Values from months 05-2020 and 06-2020 (because in this case its the bigger values,), after that, I calculate the average from this.

 

If I have more months I need to verify more months, but in this case, I would like to know, how can I get the N (in this case 2 min/max) values from the column values considering one period in this case until 06-2020? (But this period can be changed dynamically )

 

MonthValueAverage MinAverage Max
01-20200.2443520.0772470.672917
02-20200.1050020.0772470.672917
03-20200.2117670.0772470.672917
04-20200.0494920.0772470.672917
05-20200.273010.0772470.672917
06-20201.0728240.0772470.672917
  • Hi twister8889 

    here is my solution. Use the following measures:

     

    Sum of Value = SUM('Table'[Value])
    
    Highest = 
    CALCULATE (
        AVERAGEX (
            TOPN ( 'TopN'[TopN Value], 'Table', [Sum of Value], DESC ),
            'Table'[Value]
        ),
        ALL ( 'Table' )
    )
    
    Lowest = 
    CALCULATE (
        AVERAGEX (
            TOPN ( 'TopN'[TopN Value], 'Table', [Sum of Value], ASC ),
            'Table'[Value]
        ),
        ALL ( 'Table' )
    )

     

    You can dowmload the pbix file here.

    Regards FrankAT

     

7 Replies

  • Hi twister8889 ,

     

    Create the following 3 measures:

     

    Value_Sum = SUM('Table'[Value])
    
    Lowest = 
    AVERAGEX (
        FILTER (
            SUMMARIZE (
                ALLSELECTED ( 'Table' ),
                'Table'[Month],
                'Table'[Value],
                "@Ranking", RANKX ( ALLSELECTED ( 'Table' ), [value_Sum],, ASC, SKIP )
            ),
            [@Ranking] <= 2
        ),
        [value_Sum]
    )
    
    
    HIGHEST = 
    AVERAGEX (
        FILTER (
            SUMMARIZE (
                ALLSELECTED ( 'Table' ),
                'Table'[Month],
                'Table'[Value],
                "@Ranking", RANKX ( ALLSELECTED ( 'Table' ), [value_Sum],, DESC, SKIP )
            ),
            [@Ranking] <= 2
        ),
        [value_Sum]
    )

     

    If you want the [@Ranking]<= 2 can also be dinamic wiht an WHAT IF analysis.

     

    Check PBIX file attach.

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

    Hi twister8889 

    here is my solution. Use the following measures:

     

    Sum of Value = SUM('Table'[Value])
    
    Highest = 
    CALCULATE (
        AVERAGEX (
            TOPN ( 'TopN'[TopN Value], 'Table', [Sum of Value], DESC ),
            'Table'[Value]
        ),
        ALL ( 'Table' )
    )
    
    Lowest = 
    CALCULATE (
        AVERAGEX (
            TOPN ( 'TopN'[TopN Value], 'Table', [Sum of Value], ASC ),
            'Table'[Value]
        ),
        ALL ( 'Table' )
    )

     

    You can dowmload the pbix file here.

    Regards FrankAT

     

    • twister8889's avatar
      twister8889
      Icon for Helper V rankHelper V

      thank you for your answer @FrankAT

       

      I have one scenario complement: My period to find the average numbers (lowest, highest) is 7 months. So in my next example, when month 8 be load, I need to find my average (lowest, highest) to the months 2,3,4,5,6,7,8. When month 9 be load I need to find my average to the months 3,4,5,6,7,8,9. So my range of data is changing with the increase of months. After that of month 7, the average numbers (lowest, highest) can't repeat, only until July I can repeat the values to the previous month (like your print), how to improve to consider that the month 8 and months bigger, the data (lowest, highest) will not be repeated? The final table is like this:

       

      MonthLowHIgh

      01/2020

      0,1220,530
      02/20200,1220,530
      03/20200,1220,530
      04/20200,1220,530
      05/20200,1220,530
      06/20200,1220,530
      07/20200,1220,530
      08/20200,0900,400
      09/20200,1300,545

       

      Thank you so much.

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

        Hi twister8889 

        why shouldn't the data of month 8 and months bigger be repeated?

        Regards FrankAT