Forum Discussion

babarbashir's avatar
babarbashir
New Member
9 years ago
Solved

list difference from previous month data

DAXI have a month wise employee data (sample below) and I want to calculate the leavers (removed) and joiners(added) by month. 

 

e.g. Jan 2016, I want to know many entries removed and how many new entries added vs dec 2015, feb 2016 vs jan 2016 and so on in a single query.

 

Any help would be appreciated.

 

Regards,

 

YearMonthBranchEmp NameEmp No
201512B1Emp11
201512B2Emp22
201512B1Emp33
201512B2Emp44
201512B1Emp55
201512B1Emp66
20161B1Emp11
20161B2Emp22
20161B1Emp33
20161B2Emp44
20161B1Emp77
20161B1Emp66
20161B2Emp88
20162B1Emp11
20162B2Emp22
20162B1Emp33
20162B1Emp77
20162B1Emp66
20162B2Emp88
20162B1Emp99
20162B2Emp1010
20162B1Emp1111
20162B2Emp1212
20163B1Emp11
20163B2Emp22
20163B1Emp33
20163B1Emp77
20163B2Emp1010
20163B1Emp1111
20163B2Emp1212
20163B2Emp1313
20163B2Emp1414
20164B1Emp11
20164B2Emp22
20164B1Emp33
20164B1Emp77
20164B2Emp1010
20164B2Emp1414
  • babarbashir

     

    In my opinion, you will need some calculcated columns, a calendar table and some measures in this case.

     

    calculated columns

    Date = DATE(Entries[Year],Entries[Month],1)

    if last month's entry exists =
    SWITCH (
        TRUE (),
        CALCULATE (
            VALUES ( Entries[Emp No] ),
            FILTER (
                ALL ( Entries ),
                EARLIER ( Entries[Branch] ) = Entries[Branch]
                    && EARLIER ( Entries[Emp No] ) = Entries[Emp No]
                    && EARLIER ( Entries[Date] ) = DATEADD ( Entries[Date], +1, MONTH )
            )
        )
            = BLANK (), "N",
        "Y"
    )

    Measures

     

    new entries = CALCULATE(COUNTROWS(Entries),Entries[if last month's entry exists]="N")
    
    removed entires =
    COUNTROWS (
        FILTER (
            ALL ( Entries ),
            MAX ( 'Calendar'[Date] ) = DATEADD ( Entries[Date], +1, MONTH )
        )
    ) // last month's entries
        - ( COUNTROWS ( Entries ) - [new entries] )

     

     

    Check more details in the attached pbix.

1 Reply

  • Eric_Zhang's avatar
    Eric_Zhang
    Microsoft Employee

    babarbashir

     

    In my opinion, you will need some calculcated columns, a calendar table and some measures in this case.

     

    calculated columns

    Date = DATE(Entries[Year],Entries[Month],1)

    if last month's entry exists =
    SWITCH (
        TRUE (),
        CALCULATE (
            VALUES ( Entries[Emp No] ),
            FILTER (
                ALL ( Entries ),
                EARLIER ( Entries[Branch] ) = Entries[Branch]
                    && EARLIER ( Entries[Emp No] ) = Entries[Emp No]
                    && EARLIER ( Entries[Date] ) = DATEADD ( Entries[Date], +1, MONTH )
            )
        )
            = BLANK (), "N",
        "Y"
    )

    Measures

     

    new entries = CALCULATE(COUNTROWS(Entries),Entries[if last month's entry exists]="N")
    
    removed entires =
    COUNTROWS (
        FILTER (
            ALL ( Entries ),
            MAX ( 'Calendar'[Date] ) = DATEADD ( Entries[Date], +1, MONTH )
        )
    ) // last month's entries
        - ( COUNTROWS ( Entries ) - [new entries] )

     

     

    Check more details in the attached pbix.