Forum Discussion

Johnny78968798's avatar
Johnny78968798
Frequent Visitor
1 year ago
Solved

Calculate difference between multiple values in same column based on week of column

Im trying to create a new column (Price Delta) that calculates the difference between price of the 5 different Brands (R, D, G, C, P) and the "Other Avg" for each week.

 

Example for week of 1/1/24:

Brand R price (16.6) minus Other Avg price (14.8) = Price Delta (1.8)

Brand D price (14.4) minus Other Avg price (14.8) = Price Delta (-0.4)

etc... 

 

This should done for each week based on that weekly "Other Avg" value

 

Here is the data set I'm referring to and the desired column in red with desired values:

 

YearWeekWeek OfBrandPricePrice Delta
202411/1/24Brand R16.61.8
202411/1/24Brand D14.4-0.4
202411/1/24Brand G13.4-1.4
202411/1/24Brand C17.52.7
202411/1/24Brand P16.61.8
202411/1/24Other Avg14.8 
202421/8/24Brand R17.53.2
202421/8/24Brand D13.8-0.5
202421/8/24Brand G13.3-1.0
202421/8/24Brand C17.33.0
202421/8/24Brand P16.92.6
202421/8/24Other Avg14.3 

 

Thanks in advance!

  • Hi Johnny78968798 ,

    You can produce your required output by writing a calculated column like below:

     

    Price Delta Calculated = 
    VAR CurrentWeek = TableName[Week]
    VAR OtherAvgPrice =
        CALCULATE(
            MAX(TableName[Price]),
            FILTER(
                TableName,
                TableName[Week] = CurrentWeek && TableName[Brand] = "Other Avg"
            )
        )
    RETURN
     [Price] - IF(
            TableName[Brand] <> "Other Avg",
            OtherAvgPrice,
            BLANK()
        )
    

     

     

    I have attached an example pbix file.

     

    Best regards,

4 Replies

  • Johnny78968798 , 

    Create a new column to calculate the "Price Delta". You can do this by using the CALCULATE and FILTER functions in DAX to get the "Other Avg" price for each week and then subtract it from the price of each brand.

    Here is the DAX formula to create the "Price Delta" column:

    Price Delta =
    VAR CurrentWeek = 'Table'[Week]
    VAR OtherAvgPrice =
    CALCULATE(
    MAX('Table'[Price]),
    FILTER(
    'Table',
    'Table'[Week] = CurrentWeek && 'Table'[Brand] = "Other Avg"
    )

     

    • MNedix's avatar
      MNedix
      Solution Sage

      Hey bhanu_gautam 

      You didn't paste the end of the measure (missed the Return and actual calculation):

       

      Price Delta Forum = 
      VAR CurrentWeek = 'Table'[Week]
      VAR OtherAvgPrice =
          CALCULATE(
              MAX('Table'[Price]),
              FILTER('Table', 'Table'[Week] = CurrentWeek && 'Table'[Brand] = "Other Avg")
          )
      RETURN
      OtherAvgPrice-'Table'[Price]

       

      Cheers,

  • Hi Johnny78968798 ,

    You can produce your required output by writing a calculated column like below:

     

    Price Delta Calculated = 
    VAR CurrentWeek = TableName[Week]
    VAR OtherAvgPrice =
        CALCULATE(
            MAX(TableName[Price]),
            FILTER(
                TableName,
                TableName[Week] = CurrentWeek && TableName[Brand] = "Other Avg"
            )
        )
    RETURN
     [Price] - IF(
            TableName[Brand] <> "Other Avg",
            OtherAvgPrice,
            BLANK()
        )
    

     

     

    I have attached an example pbix file.

     

    Best regards,