Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

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.

Reply
AZAnalyst
Regular Visitor

Best Approach to Two Dax Challenges?

Greetings,

 

I have a table of municipal water consumption records. I'm trying to create a monthly average of gallons used by residents. I need to filter the table by Rate Class = "Residential." Looks easy enough, but my DAX calcs seem wrong. Here's the DAX:

 

Avg Monthly Vol Residential:=CALCULATE(AVERAGE(ConsumptionData2017[Gal (000s)]),FILTER(ConsumptionData2017,ConsumptionData2017[Rate Class]="Residential"))

 

It's seems wrong because each Resident has twelve records, one for each month. The above formula is returning the average based on "all" the Residential rows which number 206,652. So, if we take Total Residential Vol which is 1,531,545,000 divided by 206,652 equals a Monthly Average of 7,411. (Monthly because all monthly rows are included.) However, if I take the Total Residential Volume of 1,532,545,000 and divide it by the number of unique Residential IDs which is 16,930 (produced by Residential Locations:=CALCULATE(DISTINCTCOUNT(ConsumptionData2017[Location ID]),FILTER(ConsumptionData2017, ConsumptionData2017[Rate Class]="Residential")), then divide that by 12 for a monthly average of 7,538.

 

It seems I need to combine the above two statements but I don't know how. Thoughts for a solution?

 

The second question is more complicated. I want to create a pivot table of residents who consume between 1-5%, 6-10%, 11-20%, etc above the above calculated average. My thoughts are that I should create a disconnected table with the bands I want, then write DAX to compare the average value for each customer to the values in the band. I simply want to count the number of residents that fall into each band and display the values on a pivot table like this as an example:

Month 1-5%    6-10%    11-20%
Jan      4,096    2,600      870

 

Help? Thank you.

2 ACCEPTED SOLUTIONS
Ashish_Mathur
Super User
Super User

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?


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

View solution in original post

Anonymous
Not applicable

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.

View solution in original post

10 REPLIES 10

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.