Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
3 years ago
Solved

Including 0 values when calculating STDEV

Hi,

Let me walk you through a better example.  I am attempting to determine the most recent 6-Month STDEV for sales by product in order to determine variation.  

 

The Sales Data is made up of daily transactions so it contains a   fact_Sales[TransactionDate] and fact_Sales[units] (i.e. sales), but if there are no sales in a given month, then there will be no data for that month. 

 

So, for example, on July 1st, sales for the past 6 months were the following:

 

Jan 100

Feb 125
Apr 140
May 125

Jun 130

 

March is missing because there were no sales.  So, when I calculate STDEV on the data set, it is calculating it over 5 periods, when in fact there were 6, just one happend to be zero.

 

The code(Measure) I am curretly using is:

 

Measure = CALCULATE(STDEV.P('fact_Sales'[units]),DATESINPERIOD('Calendar'[Date],ENDOFMONTH(fact_Sales[TransactionDate]),-6,MONTH))

Instead of using date parameters in the code, I created a calculated column in the date table that gives each Month a unique ID, makes it easier for me.

 

What I need the formula to do is calculate STDEV across the six month period.  If when pulling the monthly sales numbers, it only comes back with 3 periods, then it needs to assume the other 2 periods with a zero value.

 

Thank you for your help with this!!

  • Anonymous Maybe something like this, PBIX is attached below signature. I'm not getting your stated standard deviation values but I added a debugging measure into the PBIX that should help you determine what is going on. With the debugging measure you can see that it is returning 0's for empty months.

    Measure StdDev = 
        VAR __Date = MAX('Table'[Date])
        VAR __BeginDate = EOMONTH(__Date,-6)
        VAR __EndDate = EOMONTH(__Date, -1)
        VAR __Table = FILTER(ALL('Table'),[Date]>=__BeginDate && [Date]<=__EndDate)
        VAR __Count = COUNTROWS(__Table)
        VAR __Missing = 6 - __Count
        VAR __MissingTable = 
            SELECTCOLUMNS(
                ADDCOLUMNS(
                    GENERATESERIES(1,__Missing,1),
                    "rmv",0
                ),
                "rmv",[rmv],
                "Date",[Value]
            )
        VAR __FinalTable = UNION(__Table, __MissingTable)
        VAR __Result = STDEVX.P(__FinalTable,[rmv])
    RETURN
        __Result

     

15 Replies

  • Try

    Standard Dev prev 6 months =
    VAR MaxDate =
        MAX ( 'Date'[Date] )
    VAR StartDate =
        EOMONTH ( MaxDate, -7 ) + 1
    VAR DatesToUse =
        CALCULATETABLE (
            VALUES ( 'Date'[Year month] ),
            DATESBETWEEN ( 'Date'[Date], StartDate, MaxDate )
        )
    VAR Result =
        STDEVX.P ( DatesToUse, COALESCE ( [Sum of sales], 0 ) )
    RETURN
        Result
    

    This assumes that you are including dates in the visual. If you just want to show data as at today then set a visual level filter so that the date is today.

    • Anonymous's avatar
      Anonymous
      Not applicable

      Hi,

      Thanks for replying

       

      I tried same query with different month number {EOMONTH (MaxDate, -7)+1} and for all of them the result is 0.

      Could you please share pbix file.

       

      Thanks

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi,

    Sorry, unable to upload pbix file, pasting the values below.

    Note: I need Standard Deviation for 21 months from which ever date I select. In current scenario selected date is Jan-22
     

    Datermv
    Jun-203
    Sep-2013
    Oct-201
    Nov-205
    Dec-207
    Jan-216
    Feb-213
    Mar-214
    Apr-217
    May-211
    Jun-214
    Jul-213
    Aug-211
    Sep-212
    Oct-211
    Nov-211
    Dec-214
    Jan-227

    for the month of Jan-22, I had calculated Standard Deviation manually and '3.17' as result.

    I used calendar table as well.

    • Greg_Deckler's avatar
      Greg_Deckler
      Community Champion

      Anonymous Maybe something like this, PBIX is attached below signature. I'm not getting your stated standard deviation values but I added a debugging measure into the PBIX that should help you determine what is going on. With the debugging measure you can see that it is returning 0's for empty months.

      Measure StdDev = 
          VAR __Date = MAX('Table'[Date])
          VAR __BeginDate = EOMONTH(__Date,-6)
          VAR __EndDate = EOMONTH(__Date, -1)
          VAR __Table = FILTER(ALL('Table'),[Date]>=__BeginDate && [Date]<=__EndDate)
          VAR __Count = COUNTROWS(__Table)
          VAR __Missing = 6 - __Count
          VAR __MissingTable = 
              SELECTCOLUMNS(
                  ADDCOLUMNS(
                      GENERATESERIES(1,__Missing,1),
                      "rmv",0
                  ),
                  "rmv",[rmv],
                  "Date",[Value]
              )
          VAR __FinalTable = UNION(__Table, __MissingTable)
          VAR __Result = STDEVX.P(__FinalTable,[rmv])
      RETURN
          __Result

       

      • Anonymous's avatar
        Anonymous
        Not applicable

        Thanks a ton Greg_Deckler ,

        Tweaked the query a bit according to my scenario.