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
KeyData
Frequent Visitor

including blanks in an average with summarize

Hi,

 

I´m trying to compute monthly sales average using summarize, but my formula is excluding blank values so it´s producing wrong results. How can i change the following dax formula to include and trate the blank values like 0s? 

 

SalesAvrg = Averagex(SUMMARIZE(Sales,SalesTime[YearMonth],"MonthlyQtty",sum(Sales[Quantity])),[MonthlyQtty])

 

i mean, if the summarize result in the following arrangement, the average should be 2 ((4+0+4+0)/4) and not 4 ((4+4)/2).

 

YearMonth201701201702201703201704
Qtty4 4 
1 ACCEPTED SOLUTION

Hi,

 

See if this works

 

=STDEVX.P(SUMMARIZE(Calendar,Calendar[YearMonth],"MonthlyQtty",sum(Sales[Quantity])),[MonthlyQtty])


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

View solution in original post

20 REPLIES 20
v-shex-msft
Community Support
Community Support

Hi @KeyData,

 

You can refer to below steps to use query editor to create a summary table.

 

Steps:

 

1. Duplicate original table.
2. Unpivot date columns.
3. Group column with count and sum mode.
4. Add custom column to store average.

 

StepsSteps

 

Full query:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WCiwpqVTSUTIBYhgVqxOt5OjkDGQaQkSNgdhIKTYWAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [YearMonth = _t, #"201701" = _t, #"201702" = _t, #"201703" = _t, #"201704" = _t]),
    #"Unpivoted Columns" = Table.UnpivotOtherColumns(Source, {"YearMonth"}, "Attribute", "Value"),
    #"Replaced Value" = Table.ReplaceValue(#"Unpivoted Columns","","0",Replacer.ReplaceValue,{"Value"}),
    #"Changed Type" = Table.TransformColumnTypes(#"Replaced Value",{{"Value", Int64.Type}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"YearMonth"}, {{"Count", each Table.RowCount(_), type number}, {"Sum", each List.Sum([Value]), type number}}),
    #"Added Custom" = Table.AddColumn(#"Grouped Rows", "Avg", each [Sum]/[Count])
in
    #"Added Custom"

 

Regards,

Xiaoxin Sheng

Community Support Team _ Xiaoxin
If this post helps, please consider accept as solution to help other members find it more quickly.

Hi v-shex-msft

 

Thks for your suggestion, but i need to make a lot of stat calculations (average, stddev, etc) with differents granulometry grades, so i would like to insist in find a solution with summarize.

Ashish_Mathur
Super User
Super User

Hi,

 

Try this

 

=AVERAGEX(SUMMARIZE(Sales,SalesTime[YearMonth],"MonthlyQtty",IF(ISBLANK(SUM(Sales[Quantity])),0,SUM(Sales[Quantity]))),[MonthlyQtty])


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

Thks Ashiss, but it´s still not working. Your formula it´s not taking in account blanks like 0´s, so it´s given the average of just the months with sales.

Hi @KeyData,

 

Share the link from where i can download your file.


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

Thks a lot for your interest Ashish

 

I tried to reproduce the issue in the following link

https://drive.google.com/file/d/1B5lsOIWfYM6Yk1oOpGU429RRbHFO8agX/view

It’s just an example, and for the average purpose, I created two measures

  • MonthlyAvrgSalesQtty1 = divide(sum(Sales[SalesQuantity]),(max('Calendar'[DateKey])-min('Calendar'[DateKey])))*30

          This measure reproduce almost exactly the average that I’m looking for. (it’s not exactly because some months are not of               30 days)

  • MonthlyAvrgSalesQtty2 = AVERAGEX(SUMMARIZE(Sales,Calendar[YearMonth],"MonthlyQtty",IF(ISBLANK(SUM(Sales[SalesQuantity])),0,SUM(Sales[SalesQuantity]))),[MonthlyQtty])

         This is the one I want to include months withouth sales like 0 but is not working.

 

The example is based on Contoso DB.

Hi,

 

Please reduce that file size to a couple of MB's.


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

hi Ashish,

 

Done. The Short example lynk is the following

 

https://drive.google.com/file/d/1_gu410ALDLz1HDkwAdXWknYXciK4fOPw/view?usp=sharing

 

Rgds.

Hi @KeyData,

 

Try this

 

=MonthlyAvrgSalesQtty1 = sum(Sales[SalesQuantity])/DATEDIFF(MIN('Calendar'[DateKey]),MAX('Calendar'[DateKey])+1,MONTH)

 

Hope this helps.


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

Do you have any idea why the summary options do not work?

Hi @KeyData,

 

I am not sure of whom you replied to but did my proposed solution work?

 

 


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

Hi Ashis.

Thanks a lot for your interest. Your solution porpoused of calculate the average like the sum of sales over the count of the months in the data range is correct, but i need a solution with the summarize clause, because another statistical measures are needed.  I should have  posted this discussion for example with devstat function, wich is more difficult to calculate in an alternative way like the v-shex or your last one.

How can we use the next expression includind in the calculation blanks like 0s.?

SalesStDev= STDEVX.P(SUMMARIZE(Sales,SalesTime[YearMonth],"MonthlyQtty",sum(Sales[Quantity])),[MonthlyQtty])

Hi,

 

See if this works

 

=STDEVX.P(SUMMARIZE(Calendar,Calendar[YearMonth],"MonthlyQtty",sum(Sales[Quantity])),[MonthlyQtty])


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

Hello @Ashish_Mathur I am trying to replicate your formula suggestion to include blanks in the average. I have a date table (Dimdate) with [weekinyear] granularity for sales. I also have an invoiced lines table (invoicedlines) with [Cases Invoiced]. I need to calculate the value of invoiced cases INCLUDING weeks there are no invoiced cases. 

 

Item A: Sold 1,792 cases over 5 weeks so the result should be: 1,792/10=179.2 But my current calculation is ignoring the zero weeks. So the result it is returning is 1,792/5 = 358.4 which is NOT CORRECT.

 

Sum of Invoiced CasesWeek in year     179.2       
Row Labels31323334353637383940Grand TotalStraight Invoiced for 10 weeks including blanksnumber of blanksnot counting blanks
Item A 384256256640 256   1792179.25358.4
Item B140140180180804014012018024014401440144
Item C945094121188692737453838587681054265749502912459124.509124.5
Item D 16008001760 1097136023209601120110171101.721377.125

Hi,

 

Share the link from where i can download your PBI file.  Please ensure that the file size is only a couple of MB's.


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

@Ashish_Mathur Hello, I cannto share the PBI file - it is connected to a large data model with sensitive data.

Hi Ashish

 

Thanks a lot. That´s the solution. The clause works in the following way

 

= AVERAGEX(SUMMARIZE('Calendar',Calendar[YearMonth],"MonthlyQtty",IF(ISBLANK(SUM(Sales[SalesQuantity])),0,SUM(Sales[SalesQuantity]))),[MonthlyQtty])

 

Like you suggested, it´s imperative to invoque the Calendar Table instead the Sales one.

 

Again, Thank you very much for your interest and your help

You are welcome.


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

Hello , 

 

You can try the below formula:

 

SalesAvrg = AVERAGEX(SUMMARIZE(Sales,SalesTime[YearMonth],Sales[Quantity],"MonthlyQtty",IF(ISBLANK(Sales[Quantity]),0,Sales[Quantity])),[MonthlyQtty])

 

 

Please let me know if you are not able to achieve what is needed.

Hi rajushah,

 

i've tried your if clause suggestion before mi posting, but it didn't work. Your formula is still exluding blanks and i think is because the aggregation work of the summarize expression. Do you have another suggestion?

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.