Forum Discussion

ssbagley's avatar
ssbagley
Helper III
3 years ago
Solved

Lookup Date in one table, return value from another; Direct Import

 

Table 1 contains all expense data for my company - direct connection to table on SQL server

Table 2 contains all time data for my company - direct connection to table on SQL server.  

 

How can I look up the date on the Expense Table and return the total time for that day from the Time Table, with the ability to filter by that value and or the "Type" (see example)? There can be multiple rows for each date on both Expense and Time tables for each employee ID.

 

There has to be an easier way than what I'm currently doing. Keeping Direct Import is preferred, since I've currently downloaded over 10 million rows just for the last quarter. 

 

Example Data (very simplified):

 

Expense Data   
EmpIDDateExpense Type Amount Total HoursType (Absence)
1Tuesday, August 1, 2023Dinner $    25.0085
1Tuesday, August 1, 2023Lunch $    18.0085
2Thursday, August 3, 2023Breakfast $    10.0088
3Thursday, August 3, 2023Taxi $    11.0080
3Thursday, August 3, 2023Lunch $    20.0080
3Thursday, August 3, 2023Dinner $    50.0080
4Saturday, August 5, 2023Taxi $    27.0000

 

Time Data  
EmpIDDate Hours  Type 
1Tuesday, August 1, 20235 Absence 
1Tuesday, August 1, 20232 External 
1Tuesday, August 1, 20231 Internal 
1Wednesday, August 2, 20238 External 
1Thursday, August 3, 20238 Internal 
1Friday, August 4, 20238 Internal 
1Saturday, August 5, 20230 
2Tuesday, August 1, 20238 Internal 
2Wednesday, August 2, 20238 External 
2Thursday, August 3, 20238 Absence 
2Thursday, August 3, 20232 Internal 
2Friday, August 4, 20238 Internal 
2Saturday, August 5, 20230 
3Tuesday, August 1, 20238 Internal 
3Wednesday, August 2, 20238 External 
3Thursday, August 3, 20234 External 
3Thursday, August 3, 20234 Internal 
3Friday, August 4, 20238 Internal 
3Saturday, August 5, 20230 
4Tuesday, August 1, 20238 Internal 
4Wednesday, August 2, 20238 External 
4Thursday, August 3, 20238 External 
4Friday, August 4, 20238 Internal 
4Saturday, August 5, 20230 
  • If you are looking for a measure DAX syntax, add this measure to your Expense Data table.

     

    Total Hours by Type Absence = 
    var _curValue =  CALCULATE( SUM('Time Data'[ Hours ]), 
                       FILTER( 'Time Data', 
                       'Time Data'[EmpID] = SELECTEDVALUE('Expense Data'[EmpID]) 
                       && 'Time Data'[Date] = SELECTEDVALUE('Expense Data'[Date])
                       && 'Time Data'[ Type ] = " Absence"
                       ))
    
    RETURN IF ( HASONEVALUE('Expense Data'[EmpID]) &&  HASONEVALUE('Expense Data'[Date])
    , IF (ISBlank(_curValue), 0, _curValue)
    , BLANK())

     

    Adjust the formula, I see / got spaces when I copy pasted your sample data.

     

     

    Hope it helps!

     

14 Replies

  • If you are looking for a measure DAX syntax, add this measure to your Expense Data table.

     

    Total Hours by Type Absence = 
    var _curValue =  CALCULATE( SUM('Time Data'[ Hours ]), 
                       FILTER( 'Time Data', 
                       'Time Data'[EmpID] = SELECTEDVALUE('Expense Data'[EmpID]) 
                       && 'Time Data'[Date] = SELECTEDVALUE('Expense Data'[Date])
                       && 'Time Data'[ Type ] = " Absence"
                       ))
    
    RETURN IF ( HASONEVALUE('Expense Data'[EmpID]) &&  HASONEVALUE('Expense Data'[Date])
    , IF (ISBlank(_curValue), 0, _curValue)
    , BLANK())

     

    Adjust the formula, I see / got spaces when I copy pasted your sample data.

     

     

    Hope it helps!

     

    • ssbagley's avatar
      ssbagley
      Helper III

      Thank you so much for the help and replay.  Unfortunately, results are blank (even for days when I know there should be Absence time).  Any thoughts? 

      • sevenhills's avatar
        sevenhills
        Super User

        Share the data where and when it happens, we can take a look at it!

         

        (Remove sensitive info)

  • Thank you for the guidance! I will try your suggestions on the morning!

  • sevenhills One more question - could this code be modified to include a total for absence hours AND include dates with no hours? By looking, I think it can but I'm not sure what to tweak.

      • ssbagley's avatar
        ssbagley
        Helper III

        I'm struggling with the "Show items with no data".  I've enabled that for all of the necessary fields, but am still not getting the expenses on dates with no hours.  There's no option to "show items with no data" for the measure. Any suggestions?