Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
2 years ago
Solved

How to create a calculated column with same value in every row with a calculated value

Hi,

 

I am trying to create a calculated column using DAX Calculate function.

I want all my rows to have the same value that was calculated using CALCULATE function.

I want all rows of thisFiscalWeek column to be populated with the outcome of CALCULATE function.

When I use this code it is only populating one row. I want that value to be in every row.

thisFiscalWeek = CALCULATE(MAX('Date Hierarchy'[fiscalWeek]), 'Date Hierarchy'[rDate] = 0)


Thank you for you help in advance



  • Hi Anonymous 

     

    DAX works a little differently in Calculated Columns because of Row Context. When using DAX in a measure, you have to be more aware of it's Filter Context. Row Context allows you to reference a 'naked' column in a row without an aggregating funtion, because DAX knows that row and column you mean.

     

    When used in a Calculated Column the Calculate function has an interesting behaviour, it 'converts' the value of each row into a Filter Context for the calculation. In your case, for the first row this creates a filter context of rDate = -2, dayOfWeek = 0, ...etc. So to get the same value for every row, you need to first clear the relevant filter context in the Calculate function.

     

    Something like:  

    thisFiscalWeek = CALCULATE(MAX('Date Hierarchy'[fiscalWeek]), ALL('Date Hierarchy'), 'Date Hierarchy'[rDate] = 0)

     

    Generally (depending on your use of the column) it is usually better to use a Measure instead of the Calculated Column.

     

    These references might be useful:

    Context in DAX Formulas - Microsoft Support

    Row context in DAX - SQLBI

4 Replies

  • Hi Anonymous 

     

    DAX works a little differently in Calculated Columns because of Row Context. When using DAX in a measure, you have to be more aware of it's Filter Context. Row Context allows you to reference a 'naked' column in a row without an aggregating funtion, because DAX knows that row and column you mean.

     

    When used in a Calculated Column the Calculate function has an interesting behaviour, it 'converts' the value of each row into a Filter Context for the calculation. In your case, for the first row this creates a filter context of rDate = -2, dayOfWeek = 0, ...etc. So to get the same value for every row, you need to first clear the relevant filter context in the Calculate function.

     

    Something like:  

    thisFiscalWeek = CALCULATE(MAX('Date Hierarchy'[fiscalWeek]), ALL('Date Hierarchy'), 'Date Hierarchy'[rDate] = 0)

     

    Generally (depending on your use of the column) it is usually better to use a Measure instead of the Calculated Column.

     

    These references might be useful:

    Context in DAX Formulas - Microsoft Support

    Row context in DAX - SQLBI

    • Anonymous's avatar
      Anonymous
      Not applicable

      Thank you very much for explaining this thoroughly!

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi, Anonymous 

    In your calculated column, only one 4 is displayed, and the other ones are blank. The reason is: rdate=0 has only one item. You can improve this DAX expression using the IF function. I captured some of the data in your screenshot:

    thisFiscalWeek = IF('Date Hierarchy'[rDate]=0,CALCULATE(MAX('Date Hierarchy'[fiscalWeek])),
    CALCULATE(MAX('Date Hierarchy'[fiscalWeek]),'Date Hierarchy'[rDate]))

    The results are as follows:

    If the above DAX expression helps you, that would be great.

     

     

     

    How to Get Your Question Answered Quickly

    If it does not help, please provide more details with your desired output and pbix file without privacy information (or some sample data) .

    Best Regards

    Jianpeng Li

    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

    • Anonymous's avatar
      Anonymous
      Not applicable

      Thank you for your help! I think I made a mistake to explain my goal initially. Below table is what I am trying to acheive. When rDate is 0 (which means Date is today) I want every row of thisFiscal week column to be populated with the same value - with the corresponding fiscalWeek value when rDate is 0.

      In my case below it is fiscalWeek = 4, so thisFiscalWeek column should have value 4 in every cell and dynamically change when Date changes. For example when the Date is Feb 26, 2024 thisFiscalWeek column should have 5 in all rows.

      DaterDateweekOfYearrWeekfiscalWeekfiscalWeekNamethisFiscalWeek
      1-Feb-24-215-31W014
      2-Feb-24-205-31W014
      3-Feb-24-195-31W014
      4-Feb-24-185-31W014
      5-Feb-24-176-22W024
      6-Feb-24-166-22W024
      7-Feb-24-156-22W024
      8-Feb-24-146-22W024
      9-Feb-24-136-22W024
      10-Feb-24-126-22W024
      11-Feb-24-116-22W024
      12-Feb-24-107-13W034
      13-Feb-24-97-13W034
      14-Feb-24-87-13W034
      15-Feb-24-77-13W034
      16-Feb-24-67-13W034
      17-Feb-24-57-13W034
      18-Feb-24-47-13W034
      19-Feb-24-3804W044
      20-Feb-24-2804W044
      21-Feb-24-1804W044
      22-Feb-240804W044
      23-Feb-241804W044
      24-Feb-242804W044
      25-Feb-243804W044
      26-Feb-244915W054
      27-Feb-245915W054
      28-Feb-246915W054
      29-Feb-247915W054


      Thank you again