Forum Discussion
Best Approach to Two Dax Challenges?
- 8 years ago
Hi,
Let's solve the first one. Try this
=(CALCULATE(SUM(ConsumptionData2017[Gal (000s)]),ConsumptionData2017[Rate Class]="Residential")/CALCULATE(DISTINCTCOUNT(ConsumptionData2017[Location ID]),ConsumptionData2017[Rate Class]="Residential"))/12
Does this work?
- Anonymous8 years ago
Looks like you're missing a DIVIDE() statement in the first parameter of your CALCULATE() ...
Test Dax := CALCULATE ( DIVIDE ( AVERAGE ( ConsumptionData2017[Gal (000s)] ), DISTINCTCOUNT ( ConsumptionData2017[Location ID] ) ), ConsumptionData2017[Rate Class] = "Residential" )PS...go to www.daxformatter.com to get the nicely formatted code that I've been providing. It's a HUGE help, especially with more complex DAX.
Regarding wanting to see the monthly average in the grand total, you may want to use the following pattern:
IF(
HASONEVALUE( Month ),
[Subtotal measure for 1 month],
[Different measure for calculating in the grand total]
)
There's some good documentation on this already. Check PowerPivotPro's website.
This should solve the first part of your problem:
Avg Monthly Vol Residential :=
CALCULATE (
DIVIDE (
SUM ( ConsumptionData2017[Gal (000s)] ),
DISTINCTCOUNT ( ConsumptionData2017[Location ID] )
),
ConsumptionData2017[Rate Class] = "Residential"
)Notice that you're first modifying the filter context to [Rate Class] = "Residential", then you're adding up all of the [Gal 000s] and dividing by the distinct count of locations.
Also, be careful when using FILTER(). In each of the instances you added it, it's not needed (and will likely slow down your queries). FILTER() is only needed when you're trying to compare a column to a calculated amount (usually a measure). If you're trying to compare a column to a fixed value, just add that statement directly to CALCULATE() as I have shown above.
The reason is FILTER() is an iterative function, and you're telling DAX to iterate over all 1.2 million rows to search to see if they should be included.
Now, onto the 2nd part:
I don't think you need a disconnected slicer table. You can build 3 separate measures and add them to a table visual along with Month as you've shown.
I think something like this would work:
# Customers 1-5% Over Average =
VAR AverageUsage = [Avg Monthly Vol Residential]
RETURN
SUMX (
VALUES ( ConsumptionData2017[Location ID] ),
IF (
AND (
CALCULATE ( AVERAGE ( ConsumptionData2017[Gal (000s)] ) )
>= AverageUsage * 1.01,
CALCULATE ( AVERAGE ( ConsumptionData2017[Gal (000s)] ) )
<= AverageUsage * 1.05
),
1,
0
)
)Essentially, this is iterating over a table of the unique Location IDs. With each Location ID, it's calculating the average for that customer. If that average is between 1-5% over the total average (using the measure name that I defined earlier), then give that row a 1 (otherwise a 0). Then just add up all of the 1s and 0s to get the number of customers.You can repeat this same template and just replace the 1.01 and 1.05 with the other values you need.
If this measure behaves a little slow (as it very well could, since we're calculating an average twice for each of the 17k customers), you can try this. Please note that I don't have DAX Studio open at the moment, so I'm unable to test if this syntax is correct (I normally don't declare variables in the middle of code, but that's required in this case).
This MIGHT work, but you may throw an error:
# Customers 1-5% Over Average =
VAR AverageUsage = [Avg Monthly Vol Residential]
RETURN
SUMX (
VALUES ( ConsumptionData2017[Location ID] ),
IF (
VAR IndividualAverage =
CALCULATE ( AVERAGE ( ConsumptionData2017[Gal (000s)] ) )
RETURN
AND (
IndividualAverage
>= AverageUsage * 1.01,
IndividualAverage
<= AverageUsage * 1.05
),
1,
0
)
)Let us know if these solve the issue for you!
Hi Chris,
I tried implementing the DAX formula for the second problem, and it returns only zeros for every band I created. My intial filter context is a pivot table with Months on the rows.
I am using DAX in PowerPivot so the only change I made was "=" to ":=". What is the function of the pound sign at the beginning of your first statement? Thanks!