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
NH
Advocate II
Advocate II

Count new sale order ID and existing sale order from past 1 and 2 month

Hi

 

Hope someone can help to advise me on who to write the Dax formulae to check whether the  monthly Sale order is a new order or repeated order from previous month  (mth-1) and   also from the month before (mth-2) where mth = current month.

Example of my sale order table as below.

 

Sale DateSale Order IDQty
02-Jan-1811
04-Jan-1822
06-Jan-1835
25-Jan-18910
03-Feb-1842
08-Feb-1814
10-Feb-1826
28-Feb-1858
04-Mar-1812
10-Mar-1362
10-Mar-1822
25-Mar-1842
29-Mar-1882

 

 

Result:

 

Mth_YrNew Sale Order ID countPrevious Month Sale Order ID CountSale Order ID count happent to in Mth -1 & Mth-2
Feb-1822 
Mar-18231

 

Thanks

1 ACCEPTED SOLUTION
v-shex-msft
Community Support
Community Support

Hi @NH,

 

I think you can try to use EXCEPT and INTERSECT functions to get the specific order count.

Measure formula:

Spoiler
New ID =
VAR current_Date =
    MAX ( 'Sales month'[Sale Date] )
VAR current_id =
    CALCULATETABLE (
        VALUES ( 'Sales month'[Sale Order ID] ),
        FILTER (
            ALLSELECTED ( 'Sales month' ),
            FORMAT ( [Sale Date], "mm/yyyy" ) = FORMAT ( current_Date, "mm/yyyy" )
        )
    )
VAR previous_id =
    CALCULATETABLE (
        VALUES ( 'Sales month'[Sale Order ID] ),
        FILTER (
            ALLSELECTED ( 'Sales month' ),
            [Sale Date] < DATE ( YEAR ( current_Date ), MONTH ( current_Date ), 1 )
        )
    )
RETURN
    COUNTROWS ( EXCEPT ( current_id, previous_id ) )


Previous ID =
VAR current_Date =
    MAX ( 'Sales month'[Sale Date] )
VAR current_id =
    CALCULATETABLE (
        VALUES ( 'Sales month'[Sale Order ID] ),
        FILTER (
            ALLSELECTED ( 'Sales month' ),
            FORMAT ( [Sale Date], "mm/yyyy" ) = FORMAT ( current_Date, "mm/yyyy" )
        )
    )
VAR previous_id =
    CALCULATETABLE (
        VALUES ( 'Sales month'[Sale Order ID] ),
        FILTER (
            ALLSELECTED ( 'Sales month' ),
            FORMAT ( [Sale Date], "mm/yyyy" )
                = FORMAT (
                    DATE ( YEAR ( current_Date ), MONTH ( current_Date ) - 1, 1 ),
                    "mm/yyyy"
                )
        )
    )
RETURN
    COUNTROWS ( EXCEPT ( previous_id, current_id ) )


Previous 2 ID =
VAR current_Date =
    MAX ( 'Sales month'[Sale Date] )
VAR current_id =
    CALCULATETABLE (
        VALUES ( 'Sales month'[Sale Order ID] ),
        FILTER (
            ALLSELECTED ( 'Sales month' ),
            FORMAT ( [Sale Date], "mm/yyyy" ) = FORMAT ( current_Date, "mm/yyyy" )
        )
    )
VAR previous_id =
    CALCULATETABLE (
        VALUES ( 'Sales month'[Sale Order ID] ),
        FILTER (
            ALLSELECTED ( 'Sales month' ),
            FORMAT ( [Sale Date], "mm/yyyy" )
                = FORMAT (
                    DATE ( YEAR ( current_Date ), MONTH ( current_Date ) - 1, 1 ),
                    "mm/yyyy"
                )
        )
    )
VAR previous_2_id =
    CALCULATETABLE (
        VALUES ( 'Sales month'[Sale Order ID] ),
        FILTER (
            ALLSELECTED ( 'Sales month' ),
            FORMAT ( [Sale Date], "mm/yyyy" )
                = FORMAT (
                    DATE ( YEAR ( current_Date ), MONTH ( current_Date ) - 2, 1 ),
                    "mm/yyyy"
                )
        )
    )
RETURN
    COUNTROWS (
        INTERSECT (
            INTERSECT ( previous_id, current_id ),
            INTERSECT ( current_id, previous_2_id )
        )
    )

Result:

6.PNG

 

Regards,

Xiaoxin Sheng

Community Support Team _ Xiaoxin
If this post helps, please consider accept as solution to help other members find it more quickly.

View solution in original post

3 REPLIES 3
v-shex-msft
Community Support
Community Support

Hi @NH,

 

I think you can try to use EXCEPT and INTERSECT functions to get the specific order count.

Measure formula:

Spoiler
New ID =
VAR current_Date =
    MAX ( 'Sales month'[Sale Date] )
VAR current_id =
    CALCULATETABLE (
        VALUES ( 'Sales month'[Sale Order ID] ),
        FILTER (
            ALLSELECTED ( 'Sales month' ),
            FORMAT ( [Sale Date], "mm/yyyy" ) = FORMAT ( current_Date, "mm/yyyy" )
        )
    )
VAR previous_id =
    CALCULATETABLE (
        VALUES ( 'Sales month'[Sale Order ID] ),
        FILTER (
            ALLSELECTED ( 'Sales month' ),
            [Sale Date] < DATE ( YEAR ( current_Date ), MONTH ( current_Date ), 1 )
        )
    )
RETURN
    COUNTROWS ( EXCEPT ( current_id, previous_id ) )


Previous ID =
VAR current_Date =
    MAX ( 'Sales month'[Sale Date] )
VAR current_id =
    CALCULATETABLE (
        VALUES ( 'Sales month'[Sale Order ID] ),
        FILTER (
            ALLSELECTED ( 'Sales month' ),
            FORMAT ( [Sale Date], "mm/yyyy" ) = FORMAT ( current_Date, "mm/yyyy" )
        )
    )
VAR previous_id =
    CALCULATETABLE (
        VALUES ( 'Sales month'[Sale Order ID] ),
        FILTER (
            ALLSELECTED ( 'Sales month' ),
            FORMAT ( [Sale Date], "mm/yyyy" )
                = FORMAT (
                    DATE ( YEAR ( current_Date ), MONTH ( current_Date ) - 1, 1 ),
                    "mm/yyyy"
                )
        )
    )
RETURN
    COUNTROWS ( EXCEPT ( previous_id, current_id ) )


Previous 2 ID =
VAR current_Date =
    MAX ( 'Sales month'[Sale Date] )
VAR current_id =
    CALCULATETABLE (
        VALUES ( 'Sales month'[Sale Order ID] ),
        FILTER (
            ALLSELECTED ( 'Sales month' ),
            FORMAT ( [Sale Date], "mm/yyyy" ) = FORMAT ( current_Date, "mm/yyyy" )
        )
    )
VAR previous_id =
    CALCULATETABLE (
        VALUES ( 'Sales month'[Sale Order ID] ),
        FILTER (
            ALLSELECTED ( 'Sales month' ),
            FORMAT ( [Sale Date], "mm/yyyy" )
                = FORMAT (
                    DATE ( YEAR ( current_Date ), MONTH ( current_Date ) - 1, 1 ),
                    "mm/yyyy"
                )
        )
    )
VAR previous_2_id =
    CALCULATETABLE (
        VALUES ( 'Sales month'[Sale Order ID] ),
        FILTER (
            ALLSELECTED ( 'Sales month' ),
            FORMAT ( [Sale Date], "mm/yyyy" )
                = FORMAT (
                    DATE ( YEAR ( current_Date ), MONTH ( current_Date ) - 2, 1 ),
                    "mm/yyyy"
                )
        )
    )
RETURN
    COUNTROWS (
        INTERSECT (
            INTERSECT ( previous_id, current_id ),
            INTERSECT ( current_id, previous_2_id )
        )
    )

Result:

6.PNG

 

Regards,

Xiaoxin Sheng

Community Support Team _ Xiaoxin
If this post helps, please consider accept as solution to help other members find it more quickly.

Hi XiaoXin,

 

I need to get the distinct count of the monthly sale order ID for current mth, mth-1 and mth-2 . How can I do it in DAX?

 

Thanks.

 

NH

Hi XiaoXin,

 

Thanks for your help. I will try it out and update you the result.

 

:).

 

NH

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.