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
Molotch
Helper II
Helper II

Group table by key and extract one row per group

The unability to inner join unrelated and calculated tables makes it very hard or maybe impossible to do certain table calculations.

 

I have the need to group a table per a key column and then extract one entire row per grouping by the max value in one column. I've tried everything I can think of but haven't found a way to do it.

In theory you think you should be able to scope the value column to the Key and MIN(Date) through an outer ADDCOLUMNS with a calculate filter context switch. No such luck, this brings in all Value rows from the Key grouping, the MIN(Date) column isn't used in the scoping (probably because it's an expression) so the selected value calculation in the outer scope only works if distinct(Values) returns one value per Key grouping. As soon as a key grouping has several values it returns BLANK.

ADDCOLUMNS(
            SUMMARIZECOLUMNS(
                Table[Key];
                "Date"; MIN(Table[Date])
            );
           "Value"; CALCULATE(SELECTEDVALUE(Table[Value]))
)

Any ideas on how to accomplish the above? My aim is to filter one row according to some value per grouping. Getting the max value for one column works, but I would like to use that row reference in some way to bring in the other columns from the same row.

1 ACCEPTED SOLUTION
Zubair_Muhammad
Community Champion
Community Champion

Hi @Molotch

 

Try this one

 

=
ADDCOLUMNS (
    SUMMARIZECOLUMNS ( 'Table'[Key], "Date", MIN ( 'Table'[Date] ) ),
    "Value", CALCULATE (
        SELECTEDVALUE ( 'Table'[Value] ),
        'Table'[Date] = EARLIER ( [Date] )
    )
)

Regards
Zubair

Please try my custom visuals

View solution in original post

4 REPLIES 4
Zubair_Muhammad
Community Champion
Community Champion

Hi @Molotch

 

Try this one

 

=
ADDCOLUMNS (
    SUMMARIZECOLUMNS ( 'Table'[Key], "Date", MIN ( 'Table'[Date] ) ),
    "Value", CALCULATE (
        SELECTEDVALUE ( 'Table'[Value] ),
        'Table'[Date] = EARLIER ( [Date] )
    )
)

Regards
Zubair

Please try my custom visuals
Anonymous
Not applicable

Hi Zubair,

I'm trying to apply your solution but I get the following error message regarding the Value column: "The Column "Value" cannot be found or may not be used in this expression".

 

When you write in your solution : 

 

SELECTEDVALUE ( 'Table'[Value] )

 

I undersdant that the word 'Table' references the original table which has the [Key] and [Date] columns in the original example, right?

 

Because I'm replacing 'Table' with the name of my table and I get the above error message.

@Zubair_Muhammad Magic, an EARLIER in the calculate made the trick. Why didn't I think of that?!? 🙂 Thanks alot.

 

@nickchobotar Thanks for the M calculation. Might need it in the future.

@Molotch

Here how this is done in M

This is the script to pull the max value

= Table.AddColumn(
   #"Grouped Rows", 
     "Custom", 
         each List.Max(Table.Column([Group], "Values")
  ))

 

MaxValue.gif

 

Nick -

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.