cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
RRSSDW
Frequent Visitor

Average Stacked

Very likely this point has been discussed already, but could not find it. Averages stacked by categories are traps for people who do not know how to manage with DAX. May someone help on it? 

The measure should be an average given by a DISTINCTCOUNT from Column1 divided by the number of days in range in the table, which I am getting by the DISTINCTCOUNT from a Date field

MyAvg = DISTINCTCOUNT(Column1_StuffToCount) / DISTINCTCOUNT(Column2_DateFromTable)      

Y axis has the measure, MyAvg

X axis is made on Column3_ CategoryToXaxis; 

bars (MyAvg) are stacked by using Column4_ToStackBars

Slicers give the users the ability to select the time period

Of course, the visual works fine when Count is presented. The Count is properly stacked. Data is correct. The problem comes when we want to present the average per category, because PBI calculates independent averages, per every “cell” that is in the chart, while I need the average be based on total days for the bar.

Assume we select 100 days from the slicer and assume that there was only one item in a day in slot “01:00” for category dark blue, PBI calculates the Avg = 1 (1 item / one day, although extraction period from slicer is 100 days), while the real average for that "cell" is 0.01. How can we force the measure in providing a correct Stacked Average? Tableau gives an easy set to define if calculation is computed per “cell” or per “pane” or per table.

In PBI Have tried the following, but in all cases the count of days is performed per every stack (per every cell in the chart) while I need the count be performed per bar

MyAvg =

VAR SelectedDays = CALCULATE( DISTINCTCOUNT(Tbl1[MyDate]), ALL(Tbl1[Date]) )

RETURN DISTINCTCOUNT(Tbl1[StuffToCount]) / SelectedDays

 

MyAvg =

VAR SelectedDays = CALCULATE( DISTINCTCOUNT(Tbl1[MyDate]), ALLSELECTED(Tbl1[Date]),

RETURN DISTINCTCOUNT(Tbl1 [StuffToCount]) / SelectedDays

 

MyAvg =

VAR SelectedDays = CALCULATE( DISTINCTCOUNT(Tbl1[StartDate]),

                            FILTER(ALL(Tbl1 [StartDate]), Tbl1 [StartDate] >= MIN(Tbl1 [StartDate]) ),

                            FILTER(ALL(Tbl1 [StartDate]), Tbl1 [StartDate] >= MAX(Tbl1 [StartDate]) )  )  

RETURN DISTINCTCOUNT(Tbl1 [StuffToCount]) / SelectedDays

 

Average Stacked.PNG

 

thanks

1 ACCEPTED SOLUTION
RRSSDW
Frequent Visitor

Alex, thanks. It moved me on the right path. I got the concept. ALLSELECTED has to indicate the field that has not to be broken down (as per name of the function). I have a case where the count depends by category on the X axis (which is the typical case in plotting stacked averages). The Average by category on X axis presented stacked by a further category has use the a count based on ALLSELECTED(filed that stacks the bars). Thanks for your help

Average Stacked2.PNG  

View solution in original post

2 REPLIES 2
RRSSDW
Frequent Visitor

Alex, thanks. It moved me on the right path. I got the concept. ALLSELECTED has to indicate the field that has not to be broken down (as per name of the function). I have a case where the count depends by category on the X axis (which is the typical case in plotting stacked averages). The Average by category on X axis presented stacked by a further category has use the a count based on ALLSELECTED(filed that stacks the bars). Thanks for your help

Average Stacked2.PNG  

AlexisOlson
Super User
Super User

Does it help if you use just ALLSELECTED() without any table or column arguments?

Helpful resources

Announcements
May 23 2022 epsiode 5 without aka link.jpg

The Power BI Community Show

Welcome to the Power BI Community Show! Jeroen ter Heerdt talks about the importance of Data Modeling.

charticulator_carousel_with_text (1).png

Charticulator Design Challenge

Put your data visualization and design skills to the test! This exciting challenge is happening now through May 31st!

Power BI Dev Camp Session 22 with aka link 768x460.jpg

Check it out!

Mark your calendars and join us on Thursday, May 26 at 11a PDT for a great session with Ted Pattison!

Power BI Release May 2022 768x460.png

Check it out!

Click here to read more about the May 2022 updates!