Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
2 years ago
Solved

Same calculations on multiple columns

 

I am new to Power BI desktop and I am trying to conceptually understand the best way to tackle this problem. So I have a table that looks like this:


IndexFIFO2_1FIFO2_2FIFO2_3FIFO2_4FIFO2_5FIFO2_6FIFO2_7
00.0970.0960.1010.0960.1910.1870.098
10.0960.0940.0990.0940.1880.1840.096
20.0940.0930.0980.0930.1850.1820.095
30.0930.0920.0970.0920.1840.1800.094
40.0930.0910.0960.0910.1820.1790.094
50.0920.0900.0950.0910.1810.1780.093
60.0910.0900.0950.0900.1790.1770.092
70.0910.0890.0940.0900.1780.1760.092
80.0900.0880.0940.0890.1770.1750.091
90.0890.0880.0930.0890.1760.1740.091
100.0890.0870.0920.0880.1750.1730.091
110.0880.0870.0920.0880.1740.1730.090
120.0880.0860.0910.0870.1730.1720.090
130.0880.0860.0910.0870.1720.1710.089
140.0870.0860.0910.0860.1710.1710.089
150.0870.0850.0900.0860.1700.1700.089
160.0630.0630.0660.0630.1250.1260.064
170.0630.0630.0660.0630.1250.1270.064
180.0630.0630.0660.0630.1250.1260.064
190.0630.0630.0660.0630.1250.1260.064
200.0630.0630.0660.0630.1250.1270.064
210.0630.0630.0660.0630.1250.1260.064
220.0630.0630.0660.0630.1250.1260.064
230.0630.0630.0660.0630.1250.1260.064
240.0630.0630.0660.0630.1250.1260.064
250.0630.0630.0660.0630.1250.1260.064
260.0630.0630.0660.0630.1250.1260.064
270.0630.0630.0660.0630.1250.1260.064
280.0630.0630.0660.0630.1250.1260.064
290.0630.0630.0660.0630.1250.1260.064
300.0630.0630.0660.0630.1250.1260.064



I want to perform operations that will done on each one of the seven columns. For example, one of the steps I want to perform is taking the average for rows 15-20 for every columns, so it will AVG1, AVG2,....AVG7. Then I will subtract from the average every value in the corresponding column. So for FIFO2_1, if average is AVG1 then the formula will be AVG - Xi (where i ranges from 0-30).


Next, I will search for the index of the highest value for every column, so it will be MAX1, MAX2,..., MAX7. Then I will take the index and get the average of the last 10 points, this again for every column. So you can see it's a sequential process and I am not sure what will be the best way to preform it. It's the same process for every column.

What I did until now is create a measure for every column and then divide the

AverageBetween400and440 = 
CALCULATE(
    AVERAGE('Table'[FIFO_1_1]),
    'Table'[Index] >= 15 && 'Table'[Index] <= 20
)

transformed_FIFI2_1 = 
VAR X= [AverageBetween400and440]
RETURN CALCULATE(X- SUM(Table[FIFO_1_1]))

My plan is to replicate this for every columns but honestly this is not efficient a all and I don't even know how to perform the next steps. Is there a way to automate the process for all the columns?

  • Anonymous's avatar
    Anonymous
    2 years ago

    Ashish_MathurThanks Ashish, I was actually able to solve the problem by pivoting the table in the following way. And then working with rows instead of columns.

    Index FIFO2 Value

    0FIFO2_10.097
    0FIFO2_20.096
    0FIFO2_30.101
    0FIFO2_40.096
    .........
    30FIFO2_60.126
    30FIFO2_70.064


    Changing the DAX to:

     

    AverageBetween400and440 = 
      CALCULATE(
        AVERAGE('Table'[Value]),
        'Table'[Index] >= 15 && 'Table'[Index] <= 20
      )
    
    
    transformed_FIFI2 =  [AverageBetween400and440] - SUM('Table'[Value])
    
    
    Avg Last 10 Max = 
      var maxV = MAX('Table'[Value])
      var maxI = CALCULATE(MAX('Table'[Index]), 'Table'[Value] = maxV)  
      RETURN
        CALCULATE(
          AVERAGE('Table'[Value]),
          'Table'[Index] > maxI - 10 && 'Table'[Index] <= maxI 
        )

     

2 Replies

  • Hi,

    Based on the table that you have shared, show the expected result very clearly.  Still better in the secondn tab of an Excel workbook, show your desired result using formulas/pivot tables.  Share the download link of the Excel file.

  • Anonymous's avatar
    Anonymous
    Not applicable

    Ashish_MathurThanks Ashish, I was actually able to solve the problem by pivoting the table in the following way. And then working with rows instead of columns.

    Index FIFO2 Value

    0FIFO2_10.097
    0FIFO2_20.096
    0FIFO2_30.101
    0FIFO2_40.096
    .........
    30FIFO2_60.126
    30FIFO2_70.064


    Changing the DAX to:

     

    AverageBetween400and440 = 
      CALCULATE(
        AVERAGE('Table'[Value]),
        'Table'[Index] >= 15 && 'Table'[Index] <= 20
      )
    
    
    transformed_FIFI2 =  [AverageBetween400and440] - SUM('Table'[Value])
    
    
    Avg Last 10 Max = 
      var maxV = MAX('Table'[Value])
      var maxI = CALCULATE(MAX('Table'[Index]), 'Table'[Value] = maxV)  
      RETURN
        CALCULATE(
          AVERAGE('Table'[Value]),
          'Table'[Index] > maxI - 10 && 'Table'[Index] <= maxI 
        )