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
pedanticpad
Helper II
Helper II

Assign Department from Audit Table

Hi,

 

I have employee data from a time management system with clock in and out per day laid out in a table like the below;

DATEUSERIDCLOCK
25/02/2020123408:00:00
25/02/20201234

17:00:00

 

There is a Department and CostCode table but that only holds the current Department and CostCode, nothing historic.  There is an audit table laid out like the below.

CHANGEDATEUSERIDCHANGETYPENEWVALUE
01/01/20191234DEPARTMENTWarehouse
01/05/20191234DEPARTMENTStores
01/05/20191234COSTCODEStore1
25/02/20201234DEPARTMENTShipping
25/02/20201234COSTCODEDock1

 

What I need to be able to do is assign the cost code of the time to the clock in/out data, like the below.

DATEUSERIDCLOCKDEPARTMENTCOSTCODE
24/02/2020123409:00:00StoresStore1
24/02/2020123418:00:00StoresStore1
25/02/2020123408:00:00ShippingDock1
25/02/2020123417:00:00ShippingDock1

 

Any thoughts?

 

Thanks in advance!

1 ACCEPTED SOLUTION

Hi, @pedanticpad 

 

Based on your data, you may create two calculated columns in 'Clock' table as follows.

 

COSTCODE = 
CALCULATE (
    MAX ( 'Audit'[NewValue] ),
    FILTER (
        'Audit',
        'Audit'[UserID] = 'Clock'[UserID]
            && 'Audit'[ChangeType] = "COSTCODE"
            && 'Audit'[ChangeDate]
                = CALCULATE (
                    MAX ( 'Audit'[ChangeDate] ),
                    FILTER (
                        'Audit',
                        'Audit'[ChangeDate] <= 'Clock'[Date]
                            && 'Audit'[UserID] = 'Clock'[UserID]
                    )
                )
    )
)

DEPARTMENT = 
CALCULATE (
    MAX ( 'Audit'[NewValue] ),
    FILTER (
        'Audit',
        'Audit'[UserID] = 'Clock'[UserID]
            && 'Audit'[ChangeType] = "DEPARTMENT"
            && 'Audit'[ChangeDate]
                = CALCULATE (
                    MAX ( 'Audit'[ChangeDate] ),
                    FILTER (
                        'Audit',
                        'Audit'[ChangeDate] <= 'Clock'[Date]
                            && 'Audit'[UserID] = 'Clock'[UserID]
                    )
                )
    )
)

 

 

Result:

f4.png

 

Best Regards

Allan

 

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

8 REPLIES 8
amitchandak
Super User
Super User

I did not get your logic to get dates in last table. But 2nd table can be tranposed like this 

Table = SUMMARIZE(Sheet1,Sheet1[CHANGEDATE],Sheet1[USERID],"Department",maxx(FILTER(Sheet1,Sheet1[CHANGETYPE]="DEPARTMENT"),Sheet1[NEWVALUE]),"Cost Code",maxx(FILTER(Sheet1,Sheet1[CHANGETYPE]="COSTCODE"),Sheet1[NEWVALUE]))

 

Screenshot 2020-02-29 23.17.45.png

Hi @amitchandak / @v-alq-msft,

 

Sorry, I must not be explaining myself properly.

 

See below tables (note only 1 UserID used as an example, but there are actually about 300 employees).  The Audit Table only records the date a change is made, but I need to assign the Department and CostCode to every clock record per the Desired Result Table below.

 

Clock Table 
DATEUSERIDCLOCK
01/02/2020123409:00:00
01/02/2020123417:00:00
02/02/2020123409:00:00
02/02/2020123417:00:00
03/02/2020123409:00:00
03/02/2020123417:00:00
04/02/2020123409:00:00
04/02/2020123417:00:00
05/02/2020123409:00:00
05/02/2020123417:00:00
06/02/2020123409:00:00
06/02/2020123417:00:00
07/02/2020123409:00:00
07/02/2020123417:00:00
08/02/2020123409:00:00
08/02/2020123417:00:00

 

Audit Table   
CHANGEDATEUSERIDCHANGETYPENEWVALUE
01/02/20201234DEPARTMENTStores
01/02/20201234COSTCODEStore1
06/02/20201234DEPARTMENTShipping
06/02/20201234COSTCODEDock1

 

Desired Result Table   
DATEUSERIDCLOCKDEPARTMENTCOSTCODE
01/02/2020123409:00:00StoresStore1
01/02/2020123417:00:00StoresStore1
02/02/2020123409:00:00StoresStore1
02/02/2020123417:00:00StoresStore1
03/02/2020123409:00:00StoresStore1
03/02/2020123417:00:00StoresStore1
04/02/2020123409:00:00StoresStore1
04/02/2020123417:00:00StoresStore1
05/02/2020123409:00:00StoresStore1
05/02/2020123417:00:00StoresStore1
06/02/2020123409:00:00ShippingDock1
06/02/2020123417:00:00ShippingDock1
07/02/2020123409:00:00ShippingDock1
07/02/2020123417:00:00ShippingDock1
08/02/2020123409:00:00ShippingDock1
08/02/2020123417:00:00ShippingDock1

 

Hopefully that makes more sense?

Hi, @pedanticpad 

 

Based on your data, you may create two calculated columns in 'Clock' table as follows.

 

COSTCODE = 
CALCULATE (
    MAX ( 'Audit'[NewValue] ),
    FILTER (
        'Audit',
        'Audit'[UserID] = 'Clock'[UserID]
            && 'Audit'[ChangeType] = "COSTCODE"
            && 'Audit'[ChangeDate]
                = CALCULATE (
                    MAX ( 'Audit'[ChangeDate] ),
                    FILTER (
                        'Audit',
                        'Audit'[ChangeDate] <= 'Clock'[Date]
                            && 'Audit'[UserID] = 'Clock'[UserID]
                    )
                )
    )
)

DEPARTMENT = 
CALCULATE (
    MAX ( 'Audit'[NewValue] ),
    FILTER (
        'Audit',
        'Audit'[UserID] = 'Clock'[UserID]
            && 'Audit'[ChangeType] = "DEPARTMENT"
            && 'Audit'[ChangeDate]
                = CALCULATE (
                    MAX ( 'Audit'[ChangeDate] ),
                    FILTER (
                        'Audit',
                        'Audit'[ChangeDate] <= 'Clock'[Date]
                            && 'Audit'[UserID] = 'Clock'[UserID]
                    )
                )
    )
)

 

 

Result:

f4.png

 

Best Regards

Allan

 

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

@v-alq-msft that worked perfectly!  Thanks so much for your help.

v-alq-msft
Community Support
Community Support

Hi, @pedanticpad 

 

Based on your description, I assumed that two more rows for 5/1/2019 are needed and  I created data as follows.

Employee data:

e1.png

Department and CostCode:

e2.png

 

You may duplicate query 'Employee data' and rename it as 'ResultTable'. Then you can go to 'Home' ribbon, click 'Merge Queries', configure as follows.

e3.png

e4.png

 

Then you need to expand the last column, remove the unrelated columns and rename other columns.

e5.png

 

Finally you may make 'ChangeType' column selected, go to 'Transform' ribbon, click 'Pivot Column' and configure as below. You will get the result.

e7.png

 

e6.png

 

Best Regards

Allan

 

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

 

Hi @v-alq-msft,

 

When I perform the pivot I get the below error;

 

image.png

 

There are alot of NULL values in the CHANGETYPE and NEWVALUE fields.  I think the merge is only pulling in the data for an exact match and the other dates for each user is left as NULL.

 

I need to apply the department and cost code to every clock date so that for every clock before 05/01/2019 Department = Stores and CostCode = Store1, then for every clock from 02/25/2020 Department =Shipping and CostCode = Dock1.  Any advice?

 

Thanks!

Hi, @pedanticpad 

 

I wonder the result of the last step before pivotting. Here is my result before pivotting.

e8.png

 

If you have null in one column, you can click 'more options', choose what you want to filter.

e9.png

 

Best Regards

Allan

 

Hi @v-alq-msft ,

 

See below step before the pivot;

image.png

 

I tried to filter out the null values but I got the below after pivoting;

 

image.png

 

Error;

image.png

 

Is the issue maybe that I am trying to do this against all employees not just one like in the example I gave?

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.