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
jessicaf31
Helper I
Helper I

How to calculate an average of sales for the last 4 days of a specific day of week

Hi everybody !

 

I hope I will be clear 🙂

I have a database of all the sales in a restaurant per day (one row correspond to one item ordered so I have a lot of rows for one single day).

I have added a column with the weekday and I know my last day (which corresponds to my max(date) of my last actualization, so now my last day is Thursday March, 3rd -> weekday 4)

I want to compare my sum of sales of my last day, which is Thursday (I have already this measure), to the average of sum of sales for my 4 last Thursdays (not always Thursday).

 

What I have done for the moment is dividing the sum of my sales for all my thursdays (here 950K) by the number of thursdays from my database (44) but it is obviously not the good average and not what I want at the end...

 

sum1.PNG

 

 

 

Thanks a lot for your help ! 🙂

 

 

 

 

1 ACCEPTED SOLUTION

@Greg_Deckler @konstantinos thanks a lot for your answers !

 

What I have finally done (and is working) is :

 

  • In my table "Transaction", Group by Date in order to have the sum of amount per day

groupby.PNG

 

 

 

 

It created a new Table "Transaction (2)".

 

transac2.PNG

 

 

 

Once this table is saved and applied, I have added a New Column "DateLess28" which is  = 'Transaction (2)'[Date]-28

 

And finally I have added a new measure : 

 

AverageDayLastDay = AVERAGEX(FILTER(ALL('Transaction (2)');'Transaction (2)'[Date]>=MAX('Transaction (2)'[DateLess28]) && 'Transaction (2)'[Date]<MAX('Transaction (2)'[Date]) && WEEKDAY('Transaction (2)'[Date];2)=[JourDernierJour]);'Transaction (2)'[SumCA])

 

Knowing that my "JourDernierJour" (which means "DayOfTheLastDay") was a measure added and = WEEKDAY(MAXX(ALL('Transaction');'Transaction'[Date]);2)

 

I am not sure it is the best solution but it works 🙂

Thanks again for your time !!

 

Jessica

View solution in original post

5 REPLIES 5
konstantinos
Memorable Member
Memorable Member

@jessicaf31 Correct me if I undesrtand wrong..

 

You need to compare  i.e. Thursday's sales with the Average daily sales of previous 4 Thursdays ( it can be Monday etc ) right..?

Also assume that you have a Date table with relationship with sales table...and that you use Dates as rows

 

Not sure if this the optimal way but let's try since it is interesting.

 

In order to accomplish that you need to create an index  in Dates table for every day in week. First Monday is 1 , Second Monday is 2 etc..

Create a calculate column in Date Table 

 

Index of same day =
VAR DayOfWeeK = Dates[DayInWeek]

VAR RowDate = Dates[Date]

RETURN
    CALCULATE (
        COUNTROWS ( Dates );
        FILTER ( Dates; Dates[DayInWeek] = DayOfWeeK && Dates[Date] <= RowDate )
    )

Now that we have the index we need to calculate the average of the previous 4 same days

 

//  Dates[Index of same day] is the calculated column in Dates 
// Dates[DayInWeek] is the number of day in week i.e Monday is 1
//[Total Sales] is the measure for your sales that we average per same 4 days
 

Average 4 weeks same day =
AVERAGEX (
    FILTER (
        ALL ( Dates );
        Dates[Index of same day]
            >= MAX ( Dates[Index of same day] ) - 4  
            && Dates[Index of same day] < MAX ( Dates[Index of same day] )
            && Dates[DayInWeek] = MAX ( Dates[DayInWeek] )
    );
    [Total Sales]
)

 

We are calculating the average of daily sales (Dates[Date] ) that are between 4 same days before (with  Date

 >= MAX(Dates[Index of same day]) - 4 

 and the date in table row

< MAX ( Dates[Index of same day] )

where all days are the same i.e. Mondays from the DayinWeek = 1 

 

= MAX ( Dates[DayInWeek] )

 

 

Hope that make sense and helps 

 

edit: some syntax errors , no autocorrect

Konstantinos Ioannou

@Greg_Deckler @konstantinos thanks a lot for your answers !

 

What I have finally done (and is working) is :

 

  • In my table "Transaction", Group by Date in order to have the sum of amount per day

groupby.PNG

 

 

 

 

It created a new Table "Transaction (2)".

 

transac2.PNG

 

 

 

Once this table is saved and applied, I have added a New Column "DateLess28" which is  = 'Transaction (2)'[Date]-28

 

And finally I have added a new measure : 

 

AverageDayLastDay = AVERAGEX(FILTER(ALL('Transaction (2)');'Transaction (2)'[Date]>=MAX('Transaction (2)'[DateLess28]) && 'Transaction (2)'[Date]<MAX('Transaction (2)'[Date]) && WEEKDAY('Transaction (2)'[Date];2)=[JourDernierJour]);'Transaction (2)'[SumCA])

 

Knowing that my "JourDernierJour" (which means "DayOfTheLastDay") was a measure added and = WEEKDAY(MAXX(ALL('Transaction');'Transaction'[Date]);2)

 

I am not sure it is the best solution but it works 🙂

Thanks again for your time !!

 

Jessica

@jessicaf31  The approach is similar..

 

There are fortunately many ways to end in the expected result with PowerBI..

 

 

Konstantinos Ioannou
Greg_Deckler
Super User
Super User

@jessicaf31 - Perhaps something like this:

 

Measure = CALCULATE(AVERAGE([Amount]),FILTER(ALL(RestaurantSales),[Date] >= MAX(Dates[MyDate]) && [Date] < MAX(Dates[Date])))

I have a Dates table with the date and a custom column:

 

MyDate = [Date] - 28

@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

@Greg_Deckler when I want to add a custom column in my query, it doesn't work : 

 

ADateError.PNG

 

 

Moreover, I can't do Average([Amount]) because each amount is very small (one row corresponds to one item ordered, so the amount of each row is about 10€, and what I want is the sum of all the items per day)...

 

Thanks a lot !

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.