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

Get certified in Microsoft Fabric—for free! For a limited time, get a free DP-600 exam voucher to use by the end of 2024. Register now

Reply
Adhavan
Frequent Visitor

Calculate Average Order frequency

Hi All,

I have dataset like below, I want to show =Average order frequency.

 

Like, we are reciving the order for every X minutes. 

 

I have tried different calculation as suggested by in the forum. But, couldn't get the expected result.

 

653DA77F-22F8-4834-A6C4-F83DB13092DC.jpeg

2 ACCEPTED SOLUTIONS

Hi @Adhavan ,

 

Here I suggest you to try this code to create a calculated column.

Diff column =
VAR _LASTORDER =
    CALCULATE ( MIN ( 'Table'[order id] ), ALLEXCEPT ( 'Table', 'Table'[date] ) )
VAR _DIFF =
    [Datetime]
        - MAXX (
            FILTER ( 'Table', 'Table'[order id] = EARLIER ( 'Table'[order id] ) - 1 ),
            [Datetime]
        )
RETURN
    IF ( 'Table'[order id] = _LASTORDER, BLANK (), _DIFF )

Result is as below.

RicoZhou_0-1665040267276.png

 

Best Regards,
Rico Zhou

 

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

mangaus1111
Solution Sage
Solution Sage

Hi @Adhavan ,

it is a best practice to avoid using EARLIER to make the code easier to author and maintain, using variable (VAR), like in this example:
 
Diff column =
VAR FirstOrder_of_the_Day =
    CALCULATE (
             MIN ( 'Table'[Order ID] ),
             ALLEXCEPT ( 'Table', 'Table'[Date] )
             )

VAR Order_ID = 'Table'[Order ID]

VAR Diff =
    [DateTime]
        - MAXX (
                FILTER ('Table',
                        'Table'[Order ID] = Order_ID - 1
                       ),
                [DateTime]
        )
RETURN
    IF (
        'Table'[Order ID] = FirstOrder_of_the_Day,
        BLANK(),
        Diff
    )

 

 

View solution in original post

5 REPLIES 5
mangaus1111
Solution Sage
Solution Sage

Hi @Adhavan ,

it is a best practice to avoid using EARLIER to make the code easier to author and maintain, using variable (VAR), like in this example:
 
Diff column =
VAR FirstOrder_of_the_Day =
    CALCULATE (
             MIN ( 'Table'[Order ID] ),
             ALLEXCEPT ( 'Table', 'Table'[Date] )
             )

VAR Order_ID = 'Table'[Order ID]

VAR Diff =
    [DateTime]
        - MAXX (
                FILTER ('Table',
                        'Table'[Order ID] = Order_ID - 1
                       ),
                [DateTime]
        )
RETURN
    IF (
        'Table'[Order ID] = FirstOrder_of_the_Day,
        BLANK(),
        Diff
    )

 

 

amitchandak
Super User
Super User

@Adhavan , In case Order ID are incremental only 

 

a new column

 

Datetime= [Date]+ [Time]

 

Diff column =

[Datetime] - maxx(filter(Table, [Order_id] = earlier([Order_id]) -1) ,[Datetime] )

 

a Measure

 

Average(Table[Diff column])

 

 

 

Join us as experts from around the world come together to shape the future of data and AI!
At the Microsoft Analytics Community Conference, global leaders and influential voices are stepping up to share their knowledge and help you master the latest in Microsoft Fabric, Copilot, and Purview.
️ November 12th-14th, 2024
 Online Event
Register Here

@amitchandak ,

 

Its provided the result at each column. But, it calculate wrong average value due to following reason, which I couldn't elimate nor find the solution.

 

1. It calculates 18 hours for Jan' 1st alone.

2. It showing 12+ hours for every first order of the day (calculating from previous day)

 

I have attached the reference image. Could you help me to rid out of that.

 

C9E8D84B-2E9E-4037-8C61-495E15D8B83C.jpegE0CC8A69-8107-4B7B-9BC4-B580EA4A86EE.jpeg 

Hi @Adhavan ,

 

Here I suggest you to try this code to create a calculated column.

Diff column =
VAR _LASTORDER =
    CALCULATE ( MIN ( 'Table'[order id] ), ALLEXCEPT ( 'Table', 'Table'[date] ) )
VAR _DIFF =
    [Datetime]
        - MAXX (
            FILTER ( 'Table', 'Table'[order id] = EARLIER ( 'Table'[order id] ) - 1 ),
            [Datetime]
        )
RETURN
    IF ( 'Table'[order id] = _LASTORDER, BLANK (), _DIFF )

Result is as below.

RicoZhou_0-1665040267276.png

 

Best Regards,
Rico Zhou

 

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

@v-rzhou-msft 

 

Thanks a lot.

 

It shows the exact result I want.

 

Thank,

Mani

 

Helpful resources

Announcements
November Carousel

Fabric Community Update - November 2024

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

Live Sessions with Fabric DB

Be one of the first to start using Fabric Databases

Starting December 3, join live sessions with database experts and the Fabric product team to learn just how easy it is to get started.

Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Early Bird pricing ends December 9th.

Nov PBI Update Carousel

Power BI Monthly Update - November 2024

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