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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
colacan
Resolver II
Resolver II

Selecting rows with highest value within group form virtual Table

Hi,

Curetnly I have ID (duplicated) column only (Group column does not matter) . Starting from this column I want to create a table as below

 

ID

Group

Rand_Num

Max_Rand_ID

1

A

0.12

0.66

1

B

0.66

0.66

2

A

0.52

0.98

2

A

0.98

0.98

3

B

0.21

0.45

3

A

0.45

0.45

3

A

0.16

0.45

 

The process I tried is,
1. Generate a 'Rand_Num' column not adding a Colculated column but as DAX variable because I don’t want to increase the file size
2. Create 'Max_Rand by ID' (which is highest Rand_Num within each ID)
3. Iterate the table and filter only Rand_Num = Max_Rand_ID

 

I've tried it in many ways but I couldn't achieve the desired reault.

For example, 

 

1        EVALUATE
2        VAR Table_rand =
3            ADDCOLUMNS ( Table1, "Rand_Num", RAND () )
4        VAR Table_rand_filtered =
5            FILTER (
6                Table_rand,
7                VAR Ran_Number = [Rand_Num]
8                RETURN
9                    Ran_Number
10                        = CALCULATE (
11                            MAXX(Table_rand,[Rand_Num]),
12                            ALLEXCEPT ( Table1, Table1[ID] )
13                        )
14            )
15        RETURN
16            Table_rand_filtered

 

most of error happends above 10~12 rows. 

It gives either only 1 row-table with highest 'Rand_num' or it give message that can't access [Rand_Num]

It seems it can't access [Rand_Num] whenever I use Calculate()  function while I need it to calculate the highest [Rand_Num] for each ID

 

For me, it is too complecated using a virtual table which is always confusing. I will appretiate it if you let me know how to solve this problem. (also any articles I can study handling virtual tables)

Thanks.

 

2 REPLIES 2
wdx223_Daniel
Super User
Super User

@colacan 

wdx223_Daniel_0-1606696802037.png

Table_rand is a variance, when it was calculated, it is fixed, any function can change its value. so CALCULATE in your formula is invalid. it did not deploy any filter on Table_rand. MAXX alway works on the all rows of Table_rand.

Nathaniel_C
Super User
Super User

Hi @colacan ,
So the way the Rand() works, I was not able to use it in a measure and select that value as highest. I tried it in Power Query as well, but that did not work either.  So I created a table similiar to yours then added a Calculated Column using DAX, finally creating two measures, one that fills in every row with the highest value for each ID, and the second which fills in only if it is the highest value for that ID.
r.PNG

r1.PNG

r3.PNG

Max Rand =
VAR _curID =
    MAX ( 'rand'[ID] )
VAR _calc =
    CALCULATE (
        MAX ( 'rand'[RANDOM] ),
        FILTER ( ALLEXCEPT ( 'rand', 'rand'[ID] ), MAX ( 'rand'[ID] ) = _curID )
    )
RETURN
    _calc
=============================
Max Rand with highest value only =
VAR _curID =
    MAX ( 'rand'[ID] )
VAR _curRand =
    MAX ( 'rand'[RANDOM] )
VAR _calc =
    CALCULATE (
        MAX ( 'rand'[RANDOM] ),
        FILTER ( ALLEXCEPT ( 'rand', 'rand'[ID] ), MAX ( 'rand'[ID] ) = _curID )
    )
RETURN
    IF ( _calc = _curRand, _curRand )

 


Let me know if you have any questions.

If this solves your issues, please mark it as the solution, so that others can find it easily. Kudos 👍are nice too.
Nathaniel





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

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