Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
gavinma2022
Regular Visitor

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

 

gavinma2022_0-1669361889832.png

 

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 2

Hi Amitchandak, 

Thanks for replying.. But I can't understand the solution. My problem is that I don't know how to calculate the median of the column of a temp table.... 

Helpful resources

Announcements
LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.

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.

Top Solution Authors