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
jsteffe
Helper III
Helper III

Calculate sales of previous day as a calculated column

Hello,

I get the following table named "ventes":

id_produitid magasinid_clientnb_articlesDate_venteprixCA
1111004/01/20195,8301826158,3018261
1121504/01/20198,180047586122,7007138
1131804/01/201910,70471882192,6849388
1311405/01/201911,61213052162,5698273
1323505/01/20194,747032468166,1461364

 

I would like to add a calculated column with the amount osfsales (field "CA") for the previous day.

I created a table calendar with all the dates because it seems that DAX DATE functions require this table.

lI created a relationship between my tables ventes and calendar.

 

I created the following measure :

CALCULATE(SUM(ventes[CA]);PREVIOUSDAY('calendar'[Date]))

I can put in in a report but i want to put in as a calculated column in my table.

 

Any help would be appreciated.

Thanks,

Jérôme

2 ACCEPTED SOLUTIONS
v-lili6-msft
Community Support
Community Support

hi  @jsteffe 

You could try this formula:

Column 1 = 
VAR _Yesterday =DATEADD ( 'calendar'[Date], -1, DAY )
RETURN
    CALCULATE ( SUM ( ventes[CA] ),
        FILTER (
            ventes,
                ventes[id_produit] = EARLIER ( ventes[id_produit] )
                && ventes[id_client] = EARLIER ( ventes[id_client] )
                && ventes[Date_vente] = _Yesterday
        )
    )

or

Column 2 =
VAR _Yesterday = DATEADD ( 'calendar'[Date], -1, DAY )
RETURN
    CALCULATE (  SUM ( ventes[CA] ),
        FILTER (
            ventes,
               ventes[id_produit] = EARLIER ( ventes[id_produit] )
                && ventes[Date_vente] = _Yesterday
        )
    )

Result:

1.JPG

 

If not your case, What is your expected output, could you please show the expected output in a table forus.

 

and here is sample pbix file, please try it.

 

Regards,

Lin

Community Support Team _ Lin
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

Ashish_Mathur
Super User
Super User

Hi,

This calculated column formula works

=CALCULATE(SUM(Data[CA]),FILTER(Data,Data[Date_vente]=EARLIER(Data[Date_vente])-1))

Hope this helps.

Untitled.png


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

View solution in original post

8 REPLIES 8
Ashish_Mathur
Super User
Super User

Hi,

This calculated column formula works

=CALCULATE(SUM(Data[CA]),FILTER(Data,Data[Date_vente]=EARLIER(Data[Date_vente])-1))

Hope this helps.

Untitled.png


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/
v-lili6-msft
Community Support
Community Support

hi  @jsteffe 

You could try this formula:

Column 1 = 
VAR _Yesterday =DATEADD ( 'calendar'[Date], -1, DAY )
RETURN
    CALCULATE ( SUM ( ventes[CA] ),
        FILTER (
            ventes,
                ventes[id_produit] = EARLIER ( ventes[id_produit] )
                && ventes[id_client] = EARLIER ( ventes[id_client] )
                && ventes[Date_vente] = _Yesterday
        )
    )

or

Column 2 =
VAR _Yesterday = DATEADD ( 'calendar'[Date], -1, DAY )
RETURN
    CALCULATE (  SUM ( ventes[CA] ),
        FILTER (
            ventes,
               ventes[id_produit] = EARLIER ( ventes[id_produit] )
                && ventes[Date_vente] = _Yesterday
        )
    )

Result:

1.JPG

 

If not your case, What is your expected output, could you please show the expected output in a table forus.

 

and here is sample pbix file, please try it.

 

Regards,

Lin

Community Support Team _ Lin
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Thanks a lot for your solution.

I really appreciated your help ...

 

Tahreem24
Super User
Super User

@jsteffe ,

 

Try the below DAX approach:

Measure 3 = CALCULATE(Sum('Table[CA]),DATEADD('Table[Date_vente],-1,DAY))
 
Don't forget to give thumbs up 👍 and accept this as a solution if it helped you.
Don't forget to give thumbs up and accept this as a solution if it helped you!!!

Please take a quick glance at newly created dashboards : Restaurant Management Dashboard , HR Analytics Report , Hotel Management Report, Sales Analysis Report , Fortune 500 Companies Analysis , Revenue Tracking Dashboard

I created my measure as you wrote :

Measure 1 = CALCULATE(Sum(ventes[CA]);DATEADD(ventes[Date_vente];-1;DAY))

 

But How can I put it into a calculated coloumn ? Becaus I have to use again this result to do other calculations.

 

Thanks for your help ...

 

@jsteffe ,

 

You can use this measure into new Custom fields without using any Aggregation function. Directly use the name of this measure in your new calculated column or measure.

 

Don't forget to give thumbs up 👍 and accept this as a solution if it helped you.

Don't forget to give thumbs up and accept this as a solution if it helped you!!!

Please take a quick glance at newly created dashboards : Restaurant Management Dashboard , HR Analytics Report , Hotel Management Report, Sales Analysis Report , Fortune 500 Companies Analysis , Revenue Tracking Dashboard

If I use this measure in my calculated column, I only get blank values.

Inside a report, this measure give the right values when I use it with the Date from table Calendar but give blank values if I use it with the SaleDate from table Sales ...

amitchandak
Super User
Super User

In a table, you can have when you have data at the day level. or based on some unique id. Looking at what you share you have use measure.

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.