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
Anonymous
Not applicable

Dax formula to find odd Status records in a column

Hello..

I have a finger print system..

I have 2 devices one out the door and one inside the door

 

if an employee want to finger print when he is out in the inside device then he must finger print when he is back in the out device

 

if we said the ideal case will be

 

Emp Name           Mode                        Device          Time

A                Start\End of work Day         Out           1/1/2017 8:30 AM

B                Start\End of work Day         Out           1/1/2017 8:35 AM

A                         Break                           IN            1/1/2017 2:00 PM

A                         Break                          Out           1/1/2017 3:00 PM

A                Start\End of work Day          IN          1/1/2017 5:30 PM

B                Start\End of work Day          IN          1/1/2017 5:35 PM

 

I want to find cases that are a special case.. let's assume employee B finger print for a break when he is out but he forgat to finger print it when he back again and also Employee A finger print a wrong mode when he is  back of the break

 

Emp Name           Mode                        Device          Time

A                Start\End of work Day         Out           1/1/2017 8:30 AM

B                Start\End of work Day         Out           1/1/2017 8:35 AM

A                        Break                            IN            1/1/2017 2:00 PM

B                        Break                           IN            1/1/2017 2:00 PM

A                Start\End of work Day     Out           1/1/2017 3:00 PM

A                Start\End of work Day          IN            1/1/2017 5:30 PM

B                Start\End of work Day          IN             1/1/2017 5:35 PM

 

how can I find in DAX those cases... ?

1 ACCEPTED SOLUTION
v-ljerr-msft
Employee
Employee

Hi @Anonymous,

 

It could be done with DAX in Power BI.

 

1. Use the formula below to add an Index column in your table.

Index = 
VAR currentTime = Table1[Time]
RETURN
    CALCULATE (
        RANK.EQ ( currentTime, Table1[Time], ASC ),
        FILTER (
            ALL ( Table1 ),
            Table1[Emp Name] = EARLIER ( Table1[Emp Name] )
                && DATEVALUE ( Table1[Time] ) = DATEVALUE ( EARLIER ( Table1[Time] ) )
        )
    )

2. Then you should be able to use the formula below to add another calculate column to flag the odd status.

Status = 
VAR nextMode =
    CALCULATE (
        FIRSTNONBLANK ( Table1[Mode], 1 ),
        FILTER (
            ALL ( Table1 ),
            Table1[Emp Name] = EARLIER ( Table1[Emp Name] )
                && Table1[Index]
                    = EARLIER ( Table1[Index] ) + 1
        )
    )
VAR nextDevice =
    CALCULATE (
        FIRSTNONBLANK ( Table1[Device], 1 ),
        FILTER (
            ALL ( Table1 ),
            Table1[Emp Name] = EARLIER ( Table1[Emp Name] )
                && Table1[Index]
                    = EARLIER ( Table1[Index] ) + 1
        )
    )
VAR preMode =
    CALCULATE (
        FIRSTNONBLANK ( Table1[Mode], 1 ),
        FILTER (
            ALL ( Table1 ),
            Table1[Emp Name] = EARLIER ( Table1[Emp Name] )
                && Table1[Index]
                    = EARLIER ( Table1[Index] ) - 1
        )
    )
VAR preDevice =
    CALCULATE (
        FIRSTNONBLANK ( Table1[Device], 1 ),
        FILTER (
            ALL ( Table1 ),
            Table1[Emp Name] = EARLIER ( Table1[Emp Name] )
                && Table1[Index]
                    = EARLIER ( Table1[Index] ) - 1
        )
    )
RETURN
    IF (
        ( ( Table1[Mode] = "Break"
            && Table1[Device] = "IN" )
            && nextDevice = "IN" )
            || ( ( preMode = "Break"
            && preDevice = "IN" )
            && ( Table1[Mode] <> "Break"
            && Table1[Device] = "OUT" ) ),
        "ODD"
    )

c1.PNG

 

Here is the sample pbix file for your reference. Smiley Happy

 

Regards

View solution in original post

1 REPLY 1
v-ljerr-msft
Employee
Employee

Hi @Anonymous,

 

It could be done with DAX in Power BI.

 

1. Use the formula below to add an Index column in your table.

Index = 
VAR currentTime = Table1[Time]
RETURN
    CALCULATE (
        RANK.EQ ( currentTime, Table1[Time], ASC ),
        FILTER (
            ALL ( Table1 ),
            Table1[Emp Name] = EARLIER ( Table1[Emp Name] )
                && DATEVALUE ( Table1[Time] ) = DATEVALUE ( EARLIER ( Table1[Time] ) )
        )
    )

2. Then you should be able to use the formula below to add another calculate column to flag the odd status.

Status = 
VAR nextMode =
    CALCULATE (
        FIRSTNONBLANK ( Table1[Mode], 1 ),
        FILTER (
            ALL ( Table1 ),
            Table1[Emp Name] = EARLIER ( Table1[Emp Name] )
                && Table1[Index]
                    = EARLIER ( Table1[Index] ) + 1
        )
    )
VAR nextDevice =
    CALCULATE (
        FIRSTNONBLANK ( Table1[Device], 1 ),
        FILTER (
            ALL ( Table1 ),
            Table1[Emp Name] = EARLIER ( Table1[Emp Name] )
                && Table1[Index]
                    = EARLIER ( Table1[Index] ) + 1
        )
    )
VAR preMode =
    CALCULATE (
        FIRSTNONBLANK ( Table1[Mode], 1 ),
        FILTER (
            ALL ( Table1 ),
            Table1[Emp Name] = EARLIER ( Table1[Emp Name] )
                && Table1[Index]
                    = EARLIER ( Table1[Index] ) - 1
        )
    )
VAR preDevice =
    CALCULATE (
        FIRSTNONBLANK ( Table1[Device], 1 ),
        FILTER (
            ALL ( Table1 ),
            Table1[Emp Name] = EARLIER ( Table1[Emp Name] )
                && Table1[Index]
                    = EARLIER ( Table1[Index] ) - 1
        )
    )
RETURN
    IF (
        ( ( Table1[Mode] = "Break"
            && Table1[Device] = "IN" )
            && nextDevice = "IN" )
            || ( ( preMode = "Break"
            && preDevice = "IN" )
            && ( Table1[Mode] <> "Break"
            && Table1[Device] = "OUT" ) ),
        "ODD"
    )

c1.PNG

 

Here is the sample pbix file for your reference. Smiley Happy

 

Regards

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.