Forum Discussion

anandav's avatar
anandav
Skilled Sharer
8 years ago
Solved

DateAdd function in column not working

Hi,

 

I was trying answer another thread and hit a problem on DateAdd function.

I was trying to calculate previous month sales amount for a given sales date. I have a Date table and created 1:many between Date and Sales table.

 

Original Data:

CompnayDateAmountPrev Month
A01/01/201810 
B01/01/201820 
A01/02/20183001/01/2018
B01/02/20184001/01/2018
A01/03/20185001/02/2018
B01/03/201860

01/02/2018

 

Intended Results:

CompnayDateAmountPrev Month 
A01/01/201810  
B01/01/201820  
A01/02/20183001/01/201810
B01/02/20184001/01/201820
A01/03/20185001/02/201830
B01/03/20186001/02/201840

 

I created a column as below which works:

Prev Month Sales1 =
CALCULATE(

                    MIN(Sheet1[Amount]),

                    FILTER(ALL(Sheet1),

                               Sheet1[Date] = EARLIER(Sheet1[Prev Month]) && Sheet1[Compnay] = EARLIER(Sheet1[Compnay])

                     )

)

 

But when I tried to use DateAdd function it gives me blank.

Prev Month Sales2 =
CALCULATE(MIN(Sheet1[Amount]), DATEADD(Sheet1[Date], -1, MONTH))

 

Why is the DateAdd not working in either as a column or a measure?

 

When I use DateAdd just in a column I get the date in date/time format.

Prev Month From DateAdd =
CALCULATE(DATEADD(Sheet1[Date], -1, MONTH))

  • Hi anandav,

     

    This is a question of context, if you remove the previous month from your table view it will give you the calculation you need.

     

    Be aware that the measure are very sensitive in terms of context and if you don't place a certain column in your measure, when you add that measure with that column the value will be off.

     

    Regards,

    MFelix

8 Replies

  • Hi anandav,

     

    Why are you trying to create a calculated column with previous month to calculate the total sales of previous month?

     

    The best way is to have a calculated measure using the DATEADD or similar.

     

    Your measure should look something like this:

     

    Prev Month Sale = 
    CALCULATE(SUM(Sales[Amount]); DATEADD(Sales[Date]; -1; MONTH))

    Beware that using the DAX formula on columns is different from measures, since the context is given in different way, that's why the Prev. Month sales 1 is working and the second one is not since you are taking context from your DAX formula and the calculation is incorrect.

     

    Regards,

    MFelix

    • anandav's avatar
      anandav
      Skilled Sharer

      Hi MFelix,

      That is exactly what I am strugling with - I have used DateAdd in Prev Month Sale2 and it is just giving me blank. Whether I do it as a column or measure it is blank.

       

      • MFelix's avatar
        MFelix
        Super User
        Hi anandav,

        Since you are using a dates table with a relationship to the sales date in the DATEADD function you should use the dates table column and not the column from the sales table.

        In my tests with your data I didn't made a date table that's why ot worked whit that column.

        Regards,
        MFelix