Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Earn the coveted Fabric Analytics Engineer certification. 100% off your exam for a limited time only!

Reply
Anonymous
Not applicable

Calculating Number of orders for Previous day

Good morning,

 

Ultimately my goal is to calculate the number of transactions on the previous day excluding weekends and holidays but right now I can't even calculate the transactions of the previous day.

Measure 2 just shows no value. Measure 1 works correctly.

 

Can anyone help?

Thanks

 

Measure 1

AllOrders = DISTINCTCOUNT(Orders[Number])

Measure 2

PreviousDayOrders =
VAR PDate = LASTDATE('Calendar'[Date])

RETURN
CALCULATE ( [AllOrders],
FILTER ( ALL ( 'Calendar' ),
'Calendar'[Date] > PDate - 1 &&
'Calendar'[Date] >= PDate ) )

 

The Orders table is very simple

 

Order                                                                                                          Created

12020-07-30 08:21:07
22020-07-29 08:15:07
32020-07-29 08:12:07
42020-07-30 08:21:072020-07-30 08:21:07 

 

My Calendar table is simple as well. I have it marked as a date table.

Calendar = CALENDAR(MIN(Orders[Created]),MAX(Orders[Created]))
WeekDay = WEEKDAY('Calendar'[Date])
WeekDay / Weekend = IF ( WEEKDAY ( 'Calendar'[Date]) IN { 1, 7 }, "Weekend" , "Weekday" )


Number is datatype Whole number and it has no repeating values.
Created is datatype Date/Time and it has repeating values.

 

7 REPLIES 7
Anonymous
Not applicable

I found an error in my code, Measure 2 is now 

 

PreviousDayOrders =
VAR PDate = LASTDATE('Calendar'[Date])

RETURN
CALCULATE ( [AllOrders],
    FILTER ( ALL ( 'Calendar' ),
        'Calendar'[Date] > PDate - 1 &&
        'Calendar'[Date] <= PDate ) )
 
unfortunately it made no difference though.
Anonymous
Not applicable

well I figured it out. When I switched the date datatype from date / time to just date it started calculating properly.

@Anonymous 
So have you figured out and reached your expected results? The following should also give you previous day value.

 

Measure = 
var previousday = CALCULATE(MAX([Date]),FILTER(ALL('Table'),[Date]<MAX('Table'[Date])))
Return CALCULATE(DISTINCTCOUNT('Table'[Column 2]),FILTER(ALL('Table'),[Date]=previousday))

 

 

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

Anonymous
Not applicable

I'm getting pretty close. When I add this measure to a visualization and try to group by sales agent it gives the same number for each user.

 

I know this is by design because calculate ignores context but how do I fix it? I tried wrapping the calculate in a summarize but that didnt work. I can't find anything on this topic online.

 

Thanks.

amitchandak
Super User
Super User
Anonymous
Not applicable

@amitchandak   Thanks I will give that approach a try.

 

Currently I'm following this youtube video which is where my current approach is from - https://www.youtube.com/watch?v=B0pC1B4w5U0

 

@Anonymous , you formula would be very similar to last week

https://community.powerbi.com/t5/Community-Blog/Week-Is-Not-So-Weak-WTD-Last-WTD-and-This-Week-vs-Last-Week/ba-p/1051123

 

You have to use that working day rank.

You might have to choose non continuous Rank , one with null on weekend

Helpful resources

Announcements
April AMA free

Microsoft Fabric AMA Livestream

Join us Tuesday, April 09, 9:00 – 10:00 AM PST for a live, expert-led Q&A session on all things Microsoft Fabric!

March Fabric Community Update

Fabric Community Update - March 2024

Find out what's new and trending in the Fabric Community.