Forum Discussion
Switch and calculate
Hi everyone,
I am trying to calculate percentage total no of transactions occured across filters.
My data is as follows,
| Hour | Weekname | Count_loading_col | Total_loading | Total teus | Location_name | Column |
| 41 | 20WK24 | 0 | 0 | 90 | 0 | |
| 4 | 20WK24 | 80 | ND | |||
| 20WK24 | a | ND | ||||
| 8 | 20WK24 | 0 | 0 | 90 | 0 | |
| 71 | 20WK24 | 0 | 0 | 80 | b | 0 |
| 3 | 20WK24 | 22 | 1 | 90 | 1 | |
| 21 | 20WK25 | 0 | 0 | 90 | 0 | |
| 13 | 20WK25 | 0 | 0 | 88 | 0 | |
| 20WK25 | ND | |||||
| 4 | 20WK25 | c | ND | |||
| 41 | 20WK25 | 0 | 0 | d | 0 | |
| 7 | 20WK25 | 0 | 0 | 0 | ||
| 20WK25 | ND | |||||
| 47 | 20WK25 | 0 | 0 | 80 | b | 0 |
| 11 | 20WK25 | 0 | 0 | 80 | 0 | |
| 23 | 20WK25 | 0 | 0 | e | 0 | |
| 9 | 20WK25 | 0 | 0 | b | 0 | |
| 15 | 20WK25 | 0 | 0 | a | 0 | |
| 8 | 20WK25 | 0 | 0 | a | 0 | |
| 2 | 20WK25 | 22 | 1 | 1 | ||
| 5 | 20WK25 | 0 | 0 | 0 | ||
| 20WK25 | ND | |||||
| 50 | 20WK25 | 0 | 0 | 90 | 0 | |
| 16 | 20WK25 | 0 | 0 | a | 0 | |
| 6 | 20WK25 | 0 | 0 | c | 0 | |
| 6 | 20WK25 | 0 | 0 | a | 0 |
I want to calculate as follows,
For a week across different locations what is percentage.
1. First need to filter across location and weekname.
2. Check for overall count for that week and location. (Ex. b location has 10 counts in week 20WK25).
3. Have a condition with Hour <4, check for that particular condition in that week. (Ex. b location has passed 4 counts in week 20WK25).
4. Calculate percentage which step 3/step2.
How can I go about this?
Switch and calculate in measure is not working, if I do in calculated columns how can I go about it?
Thanks
Hi, Anonymous
Based on your description, I created data to reproduce your scenario. The pbix file is attached in the end.
Table:
You may create a measure as below.
Percenatage = var _table = ALLSELECTED('Table') var tab = SUMMARIZE( 'Table', 'Table'[Weekname], "Result", var _weekname = [Weekname] var newtab = SUMMARIZE( _table, 'Table'[Hour], 'Table'[Weekname], 'Table'[Location_name], "flag", IF( 'Table'[Hour]>4&&[Location_name]<>"", 1,0 ) ) return DIVIDE( CALCULATE( DISTINCTCOUNT('Table'[Location_name]), FILTER( newtab, 'Table'[Weekname]=_weekname&& [flag]=1 ) ), COUNTROWS( FILTER( newtab, [Weekname]=_weekname&& [Location_name]<>"" ) ) ) ) return SUMX( tab, [Result] )The overall count for '20WK25' with a location is 10. The distinct count location name with criteria of hour>4 for '20WK25' is 5. Here is the result.
If I misunderstand your thoughts, please show us your expected result.
Best Regards
Allan
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
4 Replies
- amitchandak
Super User
Anonymous , not very clear.
do you wow comparision ?
- AnonymousNot applicable
amitchandak, Thanks for your reply, I am not looking for that comparison, I am looking for calculation.
I want to calculate percentage across location and across weeks. Ex. 20Wk25 location a achieved 30% like that by using DAX.
My steps are
1. Calculate Overall count across week and location(Ex. location b has 10 counts in week 25)
2. Calculate count with criteria of hour>4. (Ex. location b has passed 5 transactions(counts) in week 25)
3. calculate percentage(Step2/Step1) for each location and week. My DAX should be dynamic so that I dont need to check for weeks everytime.
So I tried for started doing step 3,
count = SWITCH(TRUE(),'TATLOADING'[Hour]<4 ||'TATLOADING'[Hour]=0,1,'TATLOADING'[Hour]>=4,0)This condition says if Hour>4, put it as 0 else if hour<4 and =0 put as 1.Hereafter how can go about and calculate overall count and percentage Step1 and Step3?Thanks.
- v-alq-msft
Community Support
Hi, Anonymous
Based on your description, I created data to reproduce your scenario. The pbix file is attached in the end.
Table:
You may create a measure as below.
Percenatage = var _table = ALLSELECTED('Table') var tab = SUMMARIZE( 'Table', 'Table'[Weekname], "Result", var _weekname = [Weekname] var newtab = SUMMARIZE( _table, 'Table'[Hour], 'Table'[Weekname], 'Table'[Location_name], "flag", IF( 'Table'[Hour]>4&&[Location_name]<>"", 1,0 ) ) return DIVIDE( CALCULATE( DISTINCTCOUNT('Table'[Location_name]), FILTER( newtab, 'Table'[Weekname]=_weekname&& [flag]=1 ) ), COUNTROWS( FILTER( newtab, [Weekname]=_weekname&& [Location_name]<>"" ) ) ) ) return SUMX( tab, [Result] )The overall count for '20WK25' with a location is 10. The distinct count location name with criteria of hour>4 for '20WK25' is 5. Here is the result.
If I misunderstand your thoughts, please show us your expected result.
Best Regards
Allan
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
- AnonymousNot applicable
Thanks v-alq-msft . It was very helpful.
Done following calculations,
Step 1: created calculated column for hour.
Hour flag = SWITCH(TRUE(),'TATLOADING'[Hour]= BLANK(),0,'TATLOADING'[Hour]<4,1,'TATLOADING'[Hour]>=4,0)Step 2: created calculated column for location and hour.Location and Hour flag = SWITCH(TRUE(),'TATLOADING'[Hour flag]=0 && TATLOADING[Location_name]="",0,'TATLOADING'[Hour flag]=1 && TATLOADING[Location_name]="",0,'TATLOADING'[Hour flag]=0 && TATLOADING[Location_name]<>"",0,'TATLOADING'[Hour flag]=1 && TATLOADING[Location_name]<>"",1)Step 3:Calculated percentage by using step 1 and step 2.percentage = DIVIDE(SUM(TATLOADING[Location and Hour flag]),COUNTROWS(FILTER(TATLOADING,TATLOADING[Location_name]<>"")))Thanks.