Forum Discussion

gavinma2022's avatar
gavinma2022
Regular Visitor
3 years ago

Power BI: Groupby using a Calculated Table

Hi community members, I have searched for two days but can't find anything... please help. 

 

The data:

  • Salary table: [EmployeeCode, MonthlyUSD, DateStart, DateEnd]
  • A record applies to a_date, if a_date is between DateStart and DateEnd. 
  • Same EmployeeCode may have multiple record applies: ie.., given a date, one EmployeeCode could have multiple records with different MonthlyUSD.   

 

My intention:

  • Filter the table with a date (MaxContextDate): end of each month
  • Group by EmployeeCode, and Sum MonthlyUSD: get the sum USD of each employee. 
  • Get the Median of [Sum Monthly USD] from the different employees 

     

The measure: 

MedianSalaryEmp =
VAR ContextDate = [MaxContextDate] //current context date
VAR TempTab = CALCULATETABLE(
Salary,
FILTER(ALL(Salary[DateStart]), Salary[DateStart] <= ContextDate ),
FILTER(ALL(Salary[DateEnd]), Salary[DateEnd] >= ContextDate || ISBLANK(Salary[DateEnd]) )
)
VAR GroupbyByTab = GROUPBY(
CALCULATETABLE(
Salary,
FILTER(ALL(Salary[DateStart]), Salary[DateStart] <= ContextDate ),
FILTER(ALL(Salary[DateEnd]), Salary[DateEnd] >= ContextDate || ISBLANK(Salary[DateEnd]) )
), [EmployeeCode], "SumUSD", SUMX(CURRENTGROUP(), [MonthlyUSD])
 
)
VAR Med = Median (GroupbyByTab[SumUSD])
Return Med

 

 

The error: 

Table variable 'GroupbyByTab' cannot be used in current context because a base table is expected.

 

Can anyone guide me how to solve this problem?

 

Thanks!

 

2 Replies