Forum Discussion
Calculated column based on multiple conditions using DAX
- 3 years ago
Hi, CBXS
First, you need to add an index column to Power Query.
Column = Var _N1=CALCULATE(COUNT('Table'[Index]),ALLEXCEPT('Table','Table'[Person],'Table'[Classification],'Table'[Weeknumber],'Table'[Day_in_week])) Var _N2=CALCULATE(MIN('Table'[Index]),ALLEXCEPT('Table','Table'[Person],'Table'[Classification],'Table'[Weeknumber],'Table'[Day_in_week])) Var _N3=CALCULATE(COUNT('Table'[Index]),ALLEXCEPT('Table','Table'[Person],'Table'[Weeknumber],'Table'[Day_in_week])) Var _Rank=RANKX(FILTER('Table',[Person]=EARLIER('Table'[Person])&&[Weeknumber]=EARLIER('Table'[Weeknumber])),[Day_in_week],,ASC) Return IF(_N1>1&&[Index]=_N2,1,IF(_N3>1&&[Classification]="A",1,IF(_Rank<=2&&_N3=1,1,BLANK())))One week more A = Var _RankA=IF([Classification]="A",RANKX(FILTER('Table',[Person]=EARLIER('Table'[Person])&&[Weeknumber]=EARLIER('Table'[Weeknumber])&&[Classification]="A"),[Day_in_week],,ASC),BLANK()) Return IF(_RankA<=2&&_RankA<>BLANK()&&[Column]=BLANK(),1,BLANK())Decision = SWITCH(TRUE(), [Column]=1,1, [One week more A]=1,1, 0)Is this the result you expect?
Best Regards,
Community Support Team _Charlotte
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Hi, CBXS
You can try the following methods.
One week first two days =
Var _Rank=RANKX(FILTER('Table',[Person]=EARLIER('Table'[Person])&&[Weeknumber]=EARLIER('Table'[Weeknumber])),[Day_in_week_for_event],,ASC)
Return
IF(_Rank<=2,1,0)One day more events =
Var _Countevent=CALCULATE(COUNT('Table'[Day_in_week_for_event]),FILTER('Table',[Person]=EARLIER('Table'[Person])&&[Day_in_week_for_event]=EARLIER([Day_in_week_for_event])&&[Weeknumber]=EARLIER('Table'[Weeknumber])))
Return
IF(_Countevent>1&&[Classification]="A",1,IF(_Countevent=1,[One week first two days],0))Decision =
Var _CountA=CALCULATE(COUNT('Table'[Classification]),FILTER('Table',[Person]=EARLIER('Table'[Person])&&[Weeknumber]=EARLIER('Table'[Weeknumber])&&[Classification]="A"))
Var _RankA=IF([Classification]<>"A",BLANK(),RANKX(FILTER('Table',[Classification]="A"&&[Person]=EARLIER('Table'[Person])&&[Weeknumber]=EARLIER('Table'[Weeknumber])),[Day_in_week_for_event],,ASC))
return
IF(_CountA>=1&&_RankA<=2&&_RankA<>BLANK(),1,[One day more events])
Is this the result you expect?
Best Regards,
Community Support Team _Charlotte
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Almost perfect!
It doesn't look like the "One day two events" takes the weeknumber into account, as the Person= Erick row should be a 0 in the "One day two events" column, since it is the only row with weeknumber=3.
Additionally, i forgot to mention another condition. If there are multiple events for a person on the same day in the same week, the Decision should be 1 in only one of those rows. Your code does this when the two events have different classification, but not if the classification is the same. It doesn't matter which row that gets Decision = 1.
See the desired result in the last two rows with Person = Erick
| Person | Weeknumber | Day_in_week | Classification | Decision |
| Bob | 1 | 1 | B | 0 |
| Bob | 1 | 1 | A | 1 |
| Jenny | 2 | 1 | B | 1 |
| Jenny | 2 | 2 | B | 1 |
| Jenny | 2 | 3 | A | 1 |
| Jenny | 2 | 4 | A | 1 |
| Jenny | 2 | 5 | A | 0 |
| Ken | 2 | 2 | A | 1 |
| Ken | 2 | 4 | B | 1 |
| Ken | 2 | 5 | B | 0 |
| Erick | 3 | 2 | B | 1 |
| Erick | 3 | 2 | B | 0 |
Thank you so much for helping me with my problem!
- v-zhangti3 years agoCommunity Support
Hi, CBXS
First, you need to add an index column to Power Query.
Column = Var _N1=CALCULATE(COUNT('Table'[Index]),ALLEXCEPT('Table','Table'[Person],'Table'[Classification],'Table'[Weeknumber],'Table'[Day_in_week])) Var _N2=CALCULATE(MIN('Table'[Index]),ALLEXCEPT('Table','Table'[Person],'Table'[Classification],'Table'[Weeknumber],'Table'[Day_in_week])) Var _N3=CALCULATE(COUNT('Table'[Index]),ALLEXCEPT('Table','Table'[Person],'Table'[Weeknumber],'Table'[Day_in_week])) Var _Rank=RANKX(FILTER('Table',[Person]=EARLIER('Table'[Person])&&[Weeknumber]=EARLIER('Table'[Weeknumber])),[Day_in_week],,ASC) Return IF(_N1>1&&[Index]=_N2,1,IF(_N3>1&&[Classification]="A",1,IF(_Rank<=2&&_N3=1,1,BLANK())))One week more A = Var _RankA=IF([Classification]="A",RANKX(FILTER('Table',[Person]=EARLIER('Table'[Person])&&[Weeknumber]=EARLIER('Table'[Weeknumber])&&[Classification]="A"),[Day_in_week],,ASC),BLANK()) Return IF(_RankA<=2&&_RankA<>BLANK()&&[Column]=BLANK(),1,BLANK())Decision = SWITCH(TRUE(), [Column]=1,1, [One week more A]=1,1, 0)Is this the result you expect?
Best Regards,
Community Support Team _Charlotte
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
- CBXS3 years agoFrequent Visitor
It works as intended for the examples shown above.
Although there are still different combinations where the Decision column isn't the desired result:
A person should only have two Decision = 1 for a given week if the classification is the same through the week, see the desired result below (Chris and Derp):
Person Weeknumber Day_in_week Classification Decision Bob 1 1 B 0 Bob 1 1 A 1 Jenny 2 1 B 1 Jenny 2 2 B 1 Jenny 2 3 A 1 Jenny 2 4 A 1 Jenny 2 5 A 0 Ken 2 2 A 1 Ken 2 4 B 1 Ken 2 5 B 0 Erick 3 2 B 1 Erick 3 2 B 0 Jenny 2 6 A 0 Jenny 2 5 B 0 Chris 6 1 A 1 Chris 6 1 A 0 Chris 6 5 A 1 Chris 6 5 A 0 Chris 6 6 A 0 Chris 6 6 A 0 Chris 6 7 A 0 Chris 6 7 A 0 Derp 7 1 B 1 Derp 7 1 B 0 Derp 7 5 B 1 Derp 7 5 B 0 Derp 7 6 B 0 Derp 7 6 B 0 Derp 7 7 B 0 Derp 7 7 B 0 I attempted to edit your code to achieve the desired result, but I am not experienced enough in DAX, unfortunately.