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

The ultimate Microsoft Fabric, Power BI, Azure AI & SQL learning event! Join us in Las Vegas from March 26-28, 2024. Use code MSCUST for a $100 discount. Register Now

Reply
Vinoth31
New Member

How to find order count for current month & previous month for dates in past - power query or DAX

Hi All,

How to find order count for current month & previous month for dates in past - power query or DAX.

Example i want to do the stats for the past, example i have months 2021 oct, nov,dec. now is nov 2022.

I want to calculate order count  by merchant by month & previous month order count by merchant by month.

 

Can anyone suggest me how & what kind of dax formula or query to use to get to the valuesCapture.PNGCapture1.PNG?

 

any help is highly appreciated.

 

Regards

3 REPLIES 3
jennratten
Super User
Super User

Hello - I think this would best be done with DAX. 

Make sure there is an active, single direction relationship between the date column in your calendar table and the order date column in the Orders table.  You will also need a couple of measures.  Note, to use the measures below, your calendar table must also contain a column named Year Month Number.  You can add this as a calculated column if needed.

Measure 1: # orders for the current month

# Orders:= distinctcount ( Orders[ORDER_ID] )

Measure 2: # orders for the prior month

# Orders PM :=
VAR CurrentYearMonthNumber = SELECTEDVALUE ( 'Calendar'[Year Month Number] )
VAR PreviousYearMonthNumber = CurrentYearMonthNumber - 1
VAR Result =
    CALCULATE (
        [# Orders],
        REMOVEFILTERS ( 'Calendar' ),
        'Calendar'[Year Month Number] = PreviousYearMonthNumber
    )
RETURN
    Result

This was taken from DAX Patterns (Month-Related Calculations): https://www.daxpatterns.com/month-related-calculations/ 

Hi @jennratten , an all,

 

Many thanks for your suggestion. I tried but the Order PM measure doesn't return any value. can you suggest me what went wrong and what i should improve?

 

Order count_ = DISTINCTCOUNT(Orders[ORDER_ID])

 

Orders PM = 
VAR CurrentYearMonthNumber = SELECTEDVALUE ( 'Calender'[Date].[MonthNo] )
VAR PreviousYearMonthNumber = CurrentYearMonthNumber - 1
VAR Result =
    CALCULATE (
        Orders[Order count_],
        REMOVEFILTERS ( 'Calender' ),
        Calender[Date] = PreviousYearMonthNumber
    )
RETURN
    Result

1.PNG2.PNG3.PNG4.PNG

 

Good progress! Here are a couple of notes...

  • YearMonthNumber - if the date is 11/30/2022, the value of this field would be 220211.  It is important to think about the year in conjunction with the month so that you can maintain continuity across years.  For example, the difference between December and January.  Without the year being specified, the counts for December for all years would be retuned and compared against the counts for January across all years.
  • The same column from the calendar table should be used in the CurrentYearMonthNumber variable and the Result variable.  In your script you are referencing two different columns.
  • The relationship between Orders and Calendar should be single direction; yours is bidirectional.  While there are some valid use cases for bidirectional relationships, when possible, model the data so that single direction relationships are used in order to avoid data ambiguity and inefficient models.

Helpful resources

Announcements
Fabric Community Conference

Microsoft Fabric Community Conference

Join us at our first-ever Microsoft Fabric Community Conference, March 26-28, 2024 in Las Vegas with 100+ sessions by community experts and Microsoft engineering.

February 2024 Update Carousel

Power BI Monthly Update - February 2024

Check out the February 2024 Power BI update to learn about new features.

Fabric Career Hub

Microsoft Fabric Career Hub

Explore career paths and learn resources in Fabric.

Fabric Partner Community

Microsoft Fabric Partner Community

Engage with the Fabric engineering team, hear of product updates, business opportunities, and resources in the Fabric Partner Community.

Top Solution Authors
Top Kudoed Authors