cancel
Showing results for
Did you mean:

Find everything you need to get certified on Fabric—skills challenges, live sessions, exam prep, role guidance, and more. Get started

Anonymous
Not applicable

## 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

1 ACCEPTED SOLUTION
Helper I

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 4
Helper I

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
Not applicable

Thank you very much for explaining this thoroughly!

Community Support

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.

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
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.

 Date rDate weekOfYear rWeek fiscalWeek fiscalWeekName thisFiscalWeek 1-Feb-24 -21 5 -3 1 W01 4 2-Feb-24 -20 5 -3 1 W01 4 3-Feb-24 -19 5 -3 1 W01 4 4-Feb-24 -18 5 -3 1 W01 4 5-Feb-24 -17 6 -2 2 W02 4 6-Feb-24 -16 6 -2 2 W02 4 7-Feb-24 -15 6 -2 2 W02 4 8-Feb-24 -14 6 -2 2 W02 4 9-Feb-24 -13 6 -2 2 W02 4 10-Feb-24 -12 6 -2 2 W02 4 11-Feb-24 -11 6 -2 2 W02 4 12-Feb-24 -10 7 -1 3 W03 4 13-Feb-24 -9 7 -1 3 W03 4 14-Feb-24 -8 7 -1 3 W03 4 15-Feb-24 -7 7 -1 3 W03 4 16-Feb-24 -6 7 -1 3 W03 4 17-Feb-24 -5 7 -1 3 W03 4 18-Feb-24 -4 7 -1 3 W03 4 19-Feb-24 -3 8 0 4 W04 4 20-Feb-24 -2 8 0 4 W04 4 21-Feb-24 -1 8 0 4 W04 4 22-Feb-24 0 8 0 4 W04 4 23-Feb-24 1 8 0 4 W04 4 24-Feb-24 2 8 0 4 W04 4 25-Feb-24 3 8 0 4 W04 4 26-Feb-24 4 9 1 5 W05 4 27-Feb-24 5 9 1 5 W05 4 28-Feb-24 6 9 1 5 W05 4 29-Feb-24 7 9 1 5 W05 4

Thank you again

Announcements

#### Europe’s largest Microsoft Fabric Community Conference

Join the community in Stockholm for expert Microsoft Fabric learning including a very exciting keynote from Arun Ulag, Corporate Vice President, Azure Data.

#### Power BI Monthly Update - August 2024

Check out the August 2024 Power BI update to learn about new features.

#### Microsoft Fabric & AI Learning Hackathon

Learn from experts, get hands-on experience, and win awesome prizes.

#### Fabric Community Update - September 2024

Find out what's new and trending in the Fabric Community.

Top Solution Authors
Top Kudoed Authors