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
Jmenas
Advocate III
Advocate III

Calculate Sum of orders until the last week Sunday.

Hi All,

 

I am trying to add a measure that can calculate a Measure (or column) that allows me to calculate the sum of orders from the first of September to the last Sunday (10th of September). The idea that it does it every week the same (dynamic). 

As you can see my data is like this:

 

 

dates mtd.PNG

 

My current Formula, although looks right on the table (22032), is giving me the right value (14M).

Formula:

 

Monthly Orders = CALCULATE(
SUM('Dailyorders'[orders]);
DATESBETWEEN(Datetable[Date];
STARTOFMONTH(Datetable[Date]);
(LASTDATE(Datetable[Date])-WEEKDAY(LASTDATE(Datetable[Date]);2))))

 

Not sure what is missing.

 

Best,
J

1 ACCEPTED SOLUTION
v-jiascu-msft
Employee
Employee

@Jmenas,

 

Hi J,

 

The cause is that the third parameter "end_date" bigger than the second parameter "start_date", which cause the "Datesbetween" return null.

Try this formula please:

Monthly Orders 2 =
VAR endOfLastWeek =
    LASTDATE ( 'Date'[Date] ) - WEEKDAY ( LASTDATE ( 'Date'[Date] ), 2 )
RETURN
    CALCULATE (
        SUM ( 'Sales'[Quantity] ),
        DATESBETWEEN (
            'Date'[Date],
            STARTOFMONTH ( 'Date'[Date] ),
            IF (
                STARTOFMONTH ( 'Date'[Date] ) > endOfLastWeek,
                endOfLastWeek + 7,
                endOfLastWeek
            )
        )
    )

Calculate Sum of orders until the last week Sunday.jpg

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Best Regards!

Dale

Community Support Team _ Dale
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

3 REPLIES 3
v-jiascu-msft
Employee
Employee

@Jmenas,

 

Hi J,

 

The cause is that the third parameter "end_date" bigger than the second parameter "start_date", which cause the "Datesbetween" return null.

Try this formula please:

Monthly Orders 2 =
VAR endOfLastWeek =
    LASTDATE ( 'Date'[Date] ) - WEEKDAY ( LASTDATE ( 'Date'[Date] ), 2 )
RETURN
    CALCULATE (
        SUM ( 'Sales'[Quantity] ),
        DATESBETWEEN (
            'Date'[Date],
            STARTOFMONTH ( 'Date'[Date] ),
            IF (
                STARTOFMONTH ( 'Date'[Date] ) > endOfLastWeek,
                endOfLastWeek + 7,
                endOfLastWeek
            )
        )
    )

Calculate Sum of orders until the last week Sunday.jpg

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Best Regards!

Dale

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

Hi @v-jiascu-msft,

I just try the calculation. I adapted but still, I get the 14M. I guess the idea will be to limit the measure just for this month. For me, the date period before is not relevant.

 

I will edit the code as soon as I  have a solution.

 

Best,
J.

TomMartens
Super User
Super User

Hey,

 

if you use the measure that works on the table from a card the following happens.

 

Due to the fact, that there is no active filter from your datetable, this is the difference in contrast to your table visual, the DAX function LASTDATE() will return the last member of your datetable, whereas the function STARTOFMONTH() will return the 1st date of your calendar table.

 

Regards

Tom

 

 



Did I answer your question? Mark my post as a solution, this will help others!

Proud to be a Super User!
I accept Kudos 😉
Hamburg, Germany

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.