Forum Discussion
Calculating percent using data from two tables
- 4 years ago
Ok, First create a Calendar Table using the following code in a new table (under Modeling in the ribbon)
Calendar Table = ADDCOLUMNS ( CALENDAR ( MIN ( 'Attendance Table'[Date] ), MAX ( 'Attendance Table'[Date] ) ), "MonthNum", MONTH ( [Date] ), "Month", FORMAT ( [Date], "MMM" ), "Year", YEAR ( [Date] ) )Next create relationships between the Location field in the Employee table and the Date field in the Calendar table witht he corresponding fields in the Attendance table. The model looks like this:
Next create the measures:
Employees by Location = SUM('Employee Table'[Employee Count])Employee Attendance = SUM('Attendance Table'[Daily Attendance])As for the %, you need to decide which value you would like to compute.
What is the % of attendance for the workforce, including locations with no attendance?
% Attendance of workforce = VAR _Days = DISTINCTCOUNT ( 'Attendance Table'[Date] ) VAR TWF = CALCULATE ( [Employees by Location], ALL ( 'Employee Table' ) ) VAR WF = IF ( ISINSCOPE ( 'Calendar Table'[Date] ), [Employees by Location], TWF * _Days ) RETURN DIVIDE ( [Employee Attendance], WF )What is the % of attendance of the workforce in locations with attendance only?
% Attendance by location = VAR WF = SUMX ( ADDCOLUMNS ( SUMMARIZE ( 'Attendance Table', 'Calendar Table'[Date], 'Employee Table'[Location] ), "Calc", CALCULATE ( IF ( ISBLANK ( [Employee Attendance] ), 0, [Employees by Location] ) ) ), [Calc] ) RETURN DIVIDE ( [Employee Attendance], WF )What is the average % of attendance
Average % Attended = AVERAGEX ( SUMMARIZE ( 'Attendance Table', 'Employee Table'[Location], 'Calendar Table'[Date] ), CALCULATE ( DIVIDE ( [Employee Attendance], [Employees by Location] ) ) )Set up the visuals using the Location field from the Employee table and the date field from the Calendar table
I've attached the sample PBIX
It's important that all data shared is non confidential. Ideally we need sample from the relevant tables. You can edit any confidential fields in Excel.
So the first sample you posted is an example of the attendance table and the last sample is the employee table? If so we can work with what you have already posted.
Here is the daily attendance sample for two days of data:
| Location | Date | Daily Attendance | |
| LEXINGTON | 2/1/2022 0:00 | 2 | |
| NAPA | 2/1/2022 0:00 | 3 | |
| ATLANTA | 2/1/2022 0:00 | 4 | |
| AVON | 2/1/2022 0:00 | 4 | |
| CEDAR RAPIDS | 2/1/2022 0:00 | 4 | |
| KANSAS CITY | 2/1/2022 0:00 | 4 | |
| MIAMI | 2/1/2022 0:00 | 4 | |
| SCOTTSDALE | 2/1/2022 0:00 | 4 | |
| CALABASAS | 2/1/2022 0:00 | 5 | |
| RED BANK | 2/1/2022 0:00 | 5 | |
| CANONSBURG | 2/1/2022 0:00 | 8 | |
| CHICAGO | 2/1/2022 0:00 | 8 | |
| FORT WASHINGTON | 2/1/2022 0:00 | 13 | |
| INDIANAPOLIS | 2/1/2022 0:00 | 16 | |
| OVERLAND PARK | 2/1/2022 0:00 | 24 | |
| NEW YORK CITY | 2/1/2022 0:00 | 71 | |
| RED BANK | 2/2/2022 0:00 | 3 | |
| SCOTTSDALE | 2/2/2022 0:00 | 3 | |
| CEDAR RAPIDS | 2/2/2022 0:00 | 4 | |
| LEXINGTON | 2/2/2022 0:00 | 4 | |
| OVERLAND PARK | 2/2/2022 0:00 | 5 | |
| CALABASAS | 2/2/2022 0:00 | 6 | |
| MIAMI | 2/2/2022 0:00 | 6 | |
| CANONSBURG | 2/2/2022 0:00 | 8 | |
| ATLANTA | 2/2/2022 0:00 | 9 | |
| NAPA | 2/2/2022 0:00 | 10 | |
| AVON | 2/2/2022 0:00 | 11 | |
| FORT WASHINGTON | 2/2/2022 0:00 | 14 | |
| INDIANAPOLIS | 2/2/2022 0:00 | 15 | |
| NEW YORK CITY | 2/2/2022 0:00 | 106 |
This is the employee table:
| Location | Employee Count |
| ATLANTA | 11 |
| AVON | 16 |
| CALABASAS | 16 |
| CEDAR RAPIDS | 6 |
| CHICAGO | 27 |
| FORT WASHINGTON | 13 |
| INDIANAPOLIS | 62 |
| KANSAS CITY | 6 |
| LEXINGTON | 16 |
| MINEOLA | 8 |
| NAPA | 20 |
| NEW YORK CITY | 153 |
| OVERLAND PARK | 181 |
| PITTSBURGH | 7 |
| RED BANK | 6 |
| SCOTTSDALE | 7 |
| COON RAPIDS | 4 |
| LAKE MARY | 16 |
| SAN FRANCISCO | 2 |
| VANCOUVER | 8 |
| WESTLAKE | 4 |
| MORRISTOWN | 4 |
I can provide more if this doesn't work. Thank you again for all your help!
- PaulDBrown4 years ago
Community Champion
Ok, First create a Calendar Table using the following code in a new table (under Modeling in the ribbon)
Calendar Table = ADDCOLUMNS ( CALENDAR ( MIN ( 'Attendance Table'[Date] ), MAX ( 'Attendance Table'[Date] ) ), "MonthNum", MONTH ( [Date] ), "Month", FORMAT ( [Date], "MMM" ), "Year", YEAR ( [Date] ) )Next create relationships between the Location field in the Employee table and the Date field in the Calendar table witht he corresponding fields in the Attendance table. The model looks like this:
Next create the measures:
Employees by Location = SUM('Employee Table'[Employee Count])Employee Attendance = SUM('Attendance Table'[Daily Attendance])As for the %, you need to decide which value you would like to compute.
What is the % of attendance for the workforce, including locations with no attendance?
% Attendance of workforce = VAR _Days = DISTINCTCOUNT ( 'Attendance Table'[Date] ) VAR TWF = CALCULATE ( [Employees by Location], ALL ( 'Employee Table' ) ) VAR WF = IF ( ISINSCOPE ( 'Calendar Table'[Date] ), [Employees by Location], TWF * _Days ) RETURN DIVIDE ( [Employee Attendance], WF )What is the % of attendance of the workforce in locations with attendance only?
% Attendance by location = VAR WF = SUMX ( ADDCOLUMNS ( SUMMARIZE ( 'Attendance Table', 'Calendar Table'[Date], 'Employee Table'[Location] ), "Calc", CALCULATE ( IF ( ISBLANK ( [Employee Attendance] ), 0, [Employees by Location] ) ) ), [Calc] ) RETURN DIVIDE ( [Employee Attendance], WF )What is the average % of attendance
Average % Attended = AVERAGEX ( SUMMARIZE ( 'Attendance Table', 'Employee Table'[Location], 'Calendar Table'[Date] ), CALCULATE ( DIVIDE ( [Employee Attendance], [Employees by Location] ) ) )Set up the visuals using the Location field from the Employee table and the date field from the Calendar table
I've attached the sample PBIX
- Anonymous4 years agoNot applicable
Hi,
So I tried using the same methods; not sure if I am making a mistake somewhere. I keep getting this error:
- PaulDBrown4 years ago
Community Champion
At what step does the error occur ?