Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.
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.
Thank you for you help in advance
Solved! Go to Solution.
Hi @mirzo
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:
Hi @mirzo
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:
Thank you very much for explaining this thoroughly!
Hi, @mirzo
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.
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
Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City
Check out the April 2024 Power BI update to learn about new features.
User | Count |
---|---|
107 | |
98 | |
83 | |
76 | |
65 |
User | Count |
---|---|
120 | |
112 | |
94 | |
84 | |
75 |