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
vfx661
Advocate I
Advocate I

return table from switch

If I set a variable to be a table row, I can get values from this row like this
(ignore my_level here, it becomes relevant below):

my_level = "B"
my_row = TOPN(1, my_table_B)
my_value = MAXX(my_row, [my_col])


Where my_table_B is a table associated with level = "B".

However, I'd like to switch between multiple tables, like this

my_level = "B"
my_row = SWITCH(
  my_level,
  "A": TOPN(1, my_table_A),
  "B": TOPN(1, my_table_B)
)
my_value = MAXX(my_row, [my_col])


This doesn't work - DAX doesn't recognise my_row as a table.  In practice I have many levels so doing it the 'hard coded' way would be very messy.

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi @vfx661 ,

 

Maybe I misunderstood. But I think you are complicating the issue. You are using TOPN to get the max or min value and then you need to get that row of data, why don't you just use MAXX or MINX?You can check the following results to get Value or ID:

vtianyichmsft_0-1722404110116.png

vtianyichmsft_1-1722404119585.png

Measure = var _s = SELECTEDVALUE('Table'[Table])
RETURN SWITCH(_s,"A",MAXX('Table A',[Value]),"B",MAXX('Table B',[Value]))

 

An attachment for your reference. Hope it helps!

 

Best regards,
Community Support Team_ Scott Chang

 

If this post helps then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

3 REPLIES 3
Anonymous
Not applicable

Hi @vfx661 ,

 

Maybe I misunderstood. But I think you are complicating the issue. You are using TOPN to get the max or min value and then you need to get that row of data, why don't you just use MAXX or MINX?You can check the following results to get Value or ID:

vtianyichmsft_0-1722404110116.png

vtianyichmsft_1-1722404119585.png

Measure = var _s = SELECTEDVALUE('Table'[Table])
RETURN SWITCH(_s,"A",MAXX('Table A',[Value]),"B",MAXX('Table B',[Value]))

 

An attachment for your reference. Hope it helps!

 

Best regards,
Community Support Team_ Scott Chang

 

If this post helps then please consider Accept it as the solution to help the other members find it more quickly.

bhanu_gautam
Super User
Super User

@vfx661 , In Power BI, the SWITCH function does not return a table, which is why your approach is not working. Instead, you can use a combination of IF statements or SWITCH within a VAR 

 

VAR my_level = "B"
VAR my_row = SWITCH(
TRUE(),
my_level = "A", TOPN(1, my_table_A),
my_level = "B", TOPN(1, my_table_B),
-- Add more conditions here for other levels
BLANK()
)
VAR my_value = MAXX(my_row, [my_col])
RETURN my_value




Did I answer your question? Mark my post as a solution! And Kudos are appreciated

Proud to be a Super User!




LinkedIn






Hi @bhanu_gautam, I've tried your approach with SWITCH and it gave the same error as before.  Perhaps you meant something else?

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!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

Check out the October 2025 Power BI update to learn about new features.

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.