Forum Discussion
Assign Department from Audit Table
- 6 years ago
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:
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 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 | ||
| DATE | USERID | CLOCK |
| 01/02/2020 | 1234 | 09:00:00 |
| 01/02/2020 | 1234 | 17:00:00 |
| 02/02/2020 | 1234 | 09:00:00 |
| 02/02/2020 | 1234 | 17:00:00 |
| 03/02/2020 | 1234 | 09:00:00 |
| 03/02/2020 | 1234 | 17:00:00 |
| 04/02/2020 | 1234 | 09:00:00 |
| 04/02/2020 | 1234 | 17:00:00 |
| 05/02/2020 | 1234 | 09:00:00 |
| 05/02/2020 | 1234 | 17:00:00 |
| 06/02/2020 | 1234 | 09:00:00 |
| 06/02/2020 | 1234 | 17:00:00 |
| 07/02/2020 | 1234 | 09:00:00 |
| 07/02/2020 | 1234 | 17:00:00 |
| 08/02/2020 | 1234 | 09:00:00 |
| 08/02/2020 | 1234 | 17:00:00 |
| Audit Table | |||
| CHANGEDATE | USERID | CHANGETYPE | NEWVALUE |
| 01/02/2020 | 1234 | DEPARTMENT | Stores |
| 01/02/2020 | 1234 | COSTCODE | Store1 |
| 06/02/2020 | 1234 | DEPARTMENT | Shipping |
| 06/02/2020 | 1234 | COSTCODE | Dock1 |
| Desired Result Table | ||||
| DATE | USERID | CLOCK | DEPARTMENT | COSTCODE |
| 01/02/2020 | 1234 | 09:00:00 | Stores | Store1 |
| 01/02/2020 | 1234 | 17:00:00 | Stores | Store1 |
| 02/02/2020 | 1234 | 09:00:00 | Stores | Store1 |
| 02/02/2020 | 1234 | 17:00:00 | Stores | Store1 |
| 03/02/2020 | 1234 | 09:00:00 | Stores | Store1 |
| 03/02/2020 | 1234 | 17:00:00 | Stores | Store1 |
| 04/02/2020 | 1234 | 09:00:00 | Stores | Store1 |
| 04/02/2020 | 1234 | 17:00:00 | Stores | Store1 |
| 05/02/2020 | 1234 | 09:00:00 | Stores | Store1 |
| 05/02/2020 | 1234 | 17:00:00 | Stores | Store1 |
| 06/02/2020 | 1234 | 09:00:00 | Shipping | Dock1 |
| 06/02/2020 | 1234 | 17:00:00 | Shipping | Dock1 |
| 07/02/2020 | 1234 | 09:00:00 | Shipping | Dock1 |
| 07/02/2020 | 1234 | 17:00:00 | Shipping | Dock1 |
| 08/02/2020 | 1234 | 09:00:00 | Shipping | Dock1 |
| 08/02/2020 | 1234 | 17:00:00 | Shipping | Dock1 |
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:
Best Regards
Allan
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
- pedanticpad6 years agoHelper II
v-alq-msft that worked perfectly! Thanks so much for your help.