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

Calling all Data Engineers! Fabric Data Engineer (Exam DP-700) live sessions are back! Starting October 16th. Sign up.

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
amitchandak
Super User
Super User

@gavinma2022 , refer if these approaches can help

 

Power BI Dax Measure- Allocate data between Range: https://youtu.be/O653vwLTUzM
https://community.powerbi.com/t5/Community-Blog/How-to-divide-distribute-values-between-start-date-o...

 

https://community.powerbi.com/t5/Community-Blog/HR-Analytics-Active-Employee-Hire-and-Termination-tr...

 

 

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

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
FabCon Global Hackathon Carousel

FabCon Global Hackathon

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.