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
karthickpbi
Helper I
Helper I

Inbound and Outbound calculation in Dax

Hi Friends,

Could you please help me to  acheve the below scenario for resulted column(Resulted1, Resulted Inbound date, Result count of direction for Inbound), 

I have tried multiple time, but its showing worng value, it would be great if you can help on this.

 

Can we achieve below Scenarion with Calculated column or Measure.

Note:1. Inbound will not be consider if we don't have outbound

Note:2. first Outbound will be consider if we have one or more  Inbound

Note 3. Resulted inbound date is respective of first Inbound date and Time. 

 

 

Report Screenshot.PNG

Case IDDate and TimeDirectionResult1Result Inbound Date Result: Count Inbound for Each Case
10101-01-2022:09:00:00INBOUND1 1
10101-01-2022 09:20:00OUTBOUND001-01-2022:09:00:00 
10101-01-2022 09:30:00OUTBOUND   
10101-01-2022 14:50:00INBOUND1 1
10101-01-2022 09:50:00INBOUND1 1
10101-01-2022 01:00:00INBOUND1 1
10101-01-2022 10:20:00OUTBOUND001-01-2022 14:50:00 
10102-01-2022 08:45:00OUTBOUND   
10102-01-2022 09:35:00OUTBOUND   
10102-01-2022 09:00:00OUTBOUND   
10102-01-2022 10:00:00INBOUND1 1
10202-01-2022 11:40:00Outbound002-01-2022 10:00:00 
10202-01-2022 12:20:00INBOUND1 1
10203-01-2022 09:00:00INBOUND1 1
10203-01-2022 09:15:00OUTBOUND002-01-2022 12:20:00 
10203-01-2022 11:00:00INBOUND1 1
10204-01-2022 09:10:00OUTBOUND003-01-2022 11:00:00 
10204-01-2022 18:35:00OUTBOUND   
10204-01-2022 11:00:00OUTBOUND   
10204-01-2022 11:20:00INBOUND1 

 

 

 

 

Thanks in Advance.

3 REPLIES 3
karthickpbi
Helper I
Helper I

Even I iried to Implement with Case Wise , Getting Error.

var _a = CALCULATE(MAX('Table'[Index]),FILTER('Table',[Index]<EARLIER('Table'[Index]) && 'Table'[Case ID] = EARLIER('Table'[Case ID]) &&[Result 1]=BLANK()))
karthickpbi
Helper I
Helper I

HI @v-jianboli-msft ,

 

Thanks for sharing the Solution, when I'm Implementing the solution getting below error.

"<pi>There's not enough memory to complete this operation. Please try again later when there may be more memory available.</pi>"

v-jianboli-msft
Community Support
Community Support

Hi @karthickpbi ,

 

Please try:

First, create a column for index:

vjianbolimsft_0-1659681318432.png

Here is the M code:

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("nZK7CsMwDEV/RXhOQVIcSD2WLl2SpZmCl9K5XZr/rxvqJH5hJ2BjY3yupCuNoyAkUQmkk1mMzArPCtEs83rrLv3QXc3t9wfmU1chA4bhP9MPdwthUhiSMnUoA8uOQiRVcyDf/Qzt94Uw78u2AKdEXkO3SjZlvrBj5hEICzvA2yrzzrDHkJI20PR5vKfX0zoTFYaEDC8GZ0LXsRr3MBSa6ae7JgNxGSqZoZmRTujEDEWFIS5Dbck4+BCVjEMIpXsitP4C", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Case ID" = _t, #"Date and Time" = _t, Direction = _t, Result1 = _t, #"Result Inbound Date " = _t, #"Result: Count Inbound for Each Case" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Case ID", Int64.Type}, {"Date and Time", type text}, {"Direction", type text}, {"Result1", Int64.Type}, {"Result Inbound Date ", type text}, {"Result: Count Inbound for Each Case", Int64.Type}}),
    #"Removed Columns" = Table.RemoveColumns(#"Changed Type",{"Result1", "Result Inbound Date ", "Result: Count Inbound for Each Case"}),
    #"Added Index" = Table.AddIndexColumn(#"Removed Columns", "Index", 1, 1, Int64.Type)
in
    #"Added Index"

 

Then create these cloumns:

 

Result 1 =
SWITCH (
    [Direction],
    "INBOUND", 1,
    "OUTBOUND",
        IF (
            MAXX (
                FILTER ( 'Table', [Index] = EARLIER ( 'Table'[Index] ) - 1 ),
                [Direction]
            ) = "INBOUND",
            0,
            BLANK ()
        )
)

Result Inbound Date =
VAR _a =
    CALCULATE (
        MAX ( 'Table'[Index] ),
        FILTER (
            'Table',
            [Index] < EARLIER ( 'Table'[Index] )
                && [Result 1] = BLANK ()
        )
    )
VAR _b =
    CALCULATE (
        MIN ( 'Table'[Index] ),
        FILTER ( 'Table', [Index] > _a && [Index] < EARLIER ( 'Table'[Index] ) )
    )
RETURN
    IF (
        [Result 1] = 0,
        CALCULATE ( MAX ( 'Table'[Date and Time] ), FILTER ( 'Table', [Index] = _b ) ),
        BLANK ()
    )


Result: Count Inbound for Each Case =
VAR _a =
    CALCULATE (
        MIN ( [Index] ),
        FILTER (
            'Table',
            [Direction] = "OUTBOUND"
                && [Index] > EARLIER ( 'Table'[Index] )
        )
    )
RETURN
    IF ( [Direction] = "INBOUND", IF ( _a = BLANK (), BLANK (), 1 ), BLANK () )

 

Final output:

vjianbolimsft_1-1659683771059.png

 

Best Regards,

Jianbo Li

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

 

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.

Top Solution Authors