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

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Request now

Reply
RRaj_293
Helper III
Helper III

Dax Help for counting blank cells for average sales

 
 

Hi All,

 

I have an issue. I'm tryin gto find the average of sales Units which is equal to sum(sales units) / number of selected months. This average sales unit is used as denominator for another calucaltion which is months cover = sum(stock oh hand)/ avergae sales units.

 

Months cover = sum(stock on hand)/Average sales units

 

Average sales Units = sum(sales units)/number of selected months.

 

This number of months is a dynamic number as the filter for date could be 'Next 6 months'/'Next 3 months' / CY-Month year/Next 12 Months and so on. These are options available in the date table. here's the current dax which works as expected -

 

VAR totlsales =  CALCULATE(
    SUM ('Sales - Forecast'[Sales_Units]),
    LASTDATE(
       CALCULATETABLE(VALUES('Date Snapshot'[Date]),'Date Snapshot'[RowsInSalesFC] = TRUE)
     ) )
VAR selmnths =
CALCULATE(
    COUNTROWS('Date'),
     LASTDATE(
            CALCULATETABLE(
                VALUES('Date Snapshot'[Date]), 'Date Snapshot'[RowsInSalesFC] = TRUE )
         ),
         'Date'[Date] IN
         VALUES('Sales - Forecast'[Forecast_Month_Key])
            )
VAR avgSales =
   DIVIDE(totlsales,selmnths,1)

RETURN
DIVIDE('Stock On Hand - Forecast'[Stock On Hand Units (Forecast)],avgsales,0)

 

The Issue with the above calculation is it doesnt not consider the dates which are a blank for sales forecast units. SO with a date filter of next 6 months if a style colour has sales units as below - 

 

CURRENt RESULT -

In the first style (1.) this calculation of avergae sales does a 20/4 = 5 as the Oct and Nov months sales Fcst Units is blank.

 

EXPECTED RESULT -

20/6  = 3.33

 

RRaj_293_0-1747830042747.png

 

 

Note - This condition is used as we have forecasts the at the 1st of every month . 

   'Date'[Date] IN
         VALUES('Sales - Forecast'[Forecast_Month_Key])
 
So Date column have all dates day/month/year vice and relative date filters like current month year (which is equated to 1/1/1990 as shown in screenshot above), Next 6 months/Next 3months . Fact table sales Forecast has a monthly snapshot of all forecast months. Here's an exaample - The current month snapshot will have 1st of the month and the today's snapshot. 
Style Forecast Month Snapshot datesales FCst Units
ABC1/05/202521/05/20250.01
ABC1/06/202521/05/20250.02
ABC1/07/202521/05/20250.03
ABC1/05/20251/05/20250.05
ABC1/06/20251/05/20250.06
ABC1/07/20251/05/20250.07
ABC1/05/20251/04/20250.17
ABC1/06/20251/04/20250.11
 

Please advice how to include the dates irrespective of what values they hold mainly blanks and get the avergae right.

 

 

 

1 ACCEPTED SOLUTION
DataNinja777
Super User
Super User

Hi @RRaj_293 ,

 

The issue with your current DAX measure is that it counts only the months that have non-blank sales forecast values, which leads to an inflated average when some forecast months are blank. To fix this, you need to make sure the denominator counts all the selected months from your date table, regardless of whether sales exist for them or not. This can be done by using ALLSELECTED('Date') which respects slicer filters like "Next 6 Months" or "CY" but includes all selected months even if they have no sales data.

You can keep your totlsales variable as is, since it should only sum actual sales values:

VAR totlsales =
    CALCULATE(
        SUM('Sales - Forecast'[Sales_Units]),
        LASTDATE(
            CALCULATETABLE(
                VALUES('Date Snapshot'[Date]),
                'Date Snapshot'[RowsInSalesFC] = TRUE
            )
        )
    )

Then, redefine the selmnths variable so it counts all the visible dates from the slicer selection, including those with no sales:

VAR selmnths =
    CALCULATE(
        COUNTROWS(VALUES('Date'[Date])),
        ALLSELECTED('Date')
    )

Now calculate the average sales and return your final result:

VAR avgsales = DIVIDE(totlsales, selmnths, 1)
RETURN
DIVIDE('Stock On Hand - Forecast'[Stock On Hand Units (Forecast)], avgsales, 0)

This approach ensures that even if some forecast months have blank values, they are still included in the count, giving you a realistic average that reflects all selected months.

 

Best regards,

View solution in original post

2 REPLIES 2
RRaj_293
Helper III
Helper III

Thank you @DataNinja777 .. Your solutions looks good and almost right except that 'Date'[Date] field when all selected just pull day vice all the dates meaning for next 6 months it brings around 180 dates , so in similar lines I used another field which is CY-month start date which is only the first of every month as the forecast data is only at the month level. 

 

Thank you for guiding in the right direction. Issue resolved

 

DataNinja777
Super User
Super User

Hi @RRaj_293 ,

 

The issue with your current DAX measure is that it counts only the months that have non-blank sales forecast values, which leads to an inflated average when some forecast months are blank. To fix this, you need to make sure the denominator counts all the selected months from your date table, regardless of whether sales exist for them or not. This can be done by using ALLSELECTED('Date') which respects slicer filters like "Next 6 Months" or "CY" but includes all selected months even if they have no sales data.

You can keep your totlsales variable as is, since it should only sum actual sales values:

VAR totlsales =
    CALCULATE(
        SUM('Sales - Forecast'[Sales_Units]),
        LASTDATE(
            CALCULATETABLE(
                VALUES('Date Snapshot'[Date]),
                'Date Snapshot'[RowsInSalesFC] = TRUE
            )
        )
    )

Then, redefine the selmnths variable so it counts all the visible dates from the slicer selection, including those with no sales:

VAR selmnths =
    CALCULATE(
        COUNTROWS(VALUES('Date'[Date])),
        ALLSELECTED('Date')
    )

Now calculate the average sales and return your final result:

VAR avgsales = DIVIDE(totlsales, selmnths, 1)
RETURN
DIVIDE('Stock On Hand - Forecast'[Stock On Hand Units (Forecast)], avgsales, 0)

This approach ensures that even if some forecast months have blank values, they are still included in the count, giving you a realistic average that reflects all selected months.

 

Best regards,

Helpful resources

Announcements
November Power BI Update Carousel

Power BI Monthly Update - November 2025

Check out the November 2025 Power BI update to learn about new features.

Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.