Forum Discussion
slatka11
Helper I
3 years agoConditional average by month
I am looking to write a DAX to calculate the average skid count by month but the average should take the sum of the skids in an order group if there is one. Below is the sample data.
For example, the February average should calculate as 10 in the sample data because there is a group, and January should calculate as 10.83 ((21+3+21+4+2+14)/6).
| ORDER_NUMBER | ORDER_GROUP_ID | SKID_COUNT | SHIP_DATE |
| 801676 | 21 | 1/1/2022 | |
| 801762 | 3 | 1/1/2022 | |
| 801772 | 21 | 1/1/2022 | |
| 801782 | 4 | 1/1/2022 | |
| 801836 | 2 | 1/1/2022 | |
| 801697 | 801697 | 4 | 1/1/2022 |
| 801736 | 801697 | 5 | 1/1/2022 |
| 801780 | 801697 | 5 | 1/1/2022 |
| 801787 | 801787 | 4 | 2/2/2022 |
| 801799 | 801787 | 4 | 2/2/2022 |
| 801815 | 801787 | 2 | 2/2/2022 |
Here's a measure expression that shows one way to do it.
Avg Skid_Count = //var NoGroupSum = CALCULATE(SUM(T6[SKID_COUNT]), ISBLANK(T6[ORDER_GROUP_ID])) VAR NoGroupCount = CALCULATE ( COUNT ( T6[SKID_COUNT] ), ISBLANK ( T6[ORDER_GROUP_ID] ) ) VAR OrderGroupCount = DISTINCTCOUNTNOBLANK ( T6[ORDER_GROUP_ID] ) VAR TotalSkidCount = SUM ( T6[SKID_COUNT] ) RETURN DIVIDE ( TotalSkidCount, NoGroupCount + OrderGroupCount )Pat
2 Replies
- ppm1
Solution Sage
Here's a measure expression that shows one way to do it.
Avg Skid_Count = //var NoGroupSum = CALCULATE(SUM(T6[SKID_COUNT]), ISBLANK(T6[ORDER_GROUP_ID])) VAR NoGroupCount = CALCULATE ( COUNT ( T6[SKID_COUNT] ), ISBLANK ( T6[ORDER_GROUP_ID] ) ) VAR OrderGroupCount = DISTINCTCOUNTNOBLANK ( T6[ORDER_GROUP_ID] ) VAR TotalSkidCount = SUM ( T6[SKID_COUNT] ) RETURN DIVIDE ( TotalSkidCount, NoGroupCount + OrderGroupCount )Pat
- slatka11
Helper I
Thank you Pat, this works!